Below is code that will set the price in a product’s description and the cart based upon cookies. It can, obviously, be changed to suit whatever is on sale. This can be placed into the functions.php file.
function getDiscountIndex(){
$discounts = array(
array(‘cookie’ => ’24_sale’, ‘product_id’ => ‘15753’, ‘discount’ => 0.5),
array(‘cookie’ => ’24_sale’, ‘product_id’ => ‘15754’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘alien_kids_sale’, ‘product_id’ => ‘15745’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘alien_kids_sale’, ‘product_id’ => ‘15746’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘questions_sale’, ‘product_id’ => ‘15747’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘questions_sale’, ‘product_id’ => ‘15748’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘basic_training_sale’, ‘product_id’ => ‘15743’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘basic_training_sale’, ‘product_id’ => ‘15744’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘never_too_young_sale’, ‘product_id’ => ‘15749’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘never_too_young_sale’, ‘product_id’ => ‘15750’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘roller_coaster_sale’, ‘product_id’ => ‘15751’, ‘discount’ => 0.5,),
array(‘cookie’ => ‘roller_coaster_sale’, ‘product_id’ => ‘15752’, ‘discount’ => 0.5,)
);
return $discounts;
}
//Getting Regular Price
add_filter( ‘woocommerce_product_get_regular_price’, ‘custom_dynamic_regular_price’, 10, 2 );
add_filter( ‘woocommerce_product_variation_get_regular_price’, ‘custom_dynamic_regular_price’, 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating dynamically the product “sale price”
add_filter( ‘woocommerce_product_get_sale_price’, ‘custom_dynamic_sale_price’, 10, 2 );
add_filter( ‘woocommerce_product_variation_get_sale_price’, ‘custom_dynamic_sale_price’, 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
$discount_index = getDiscountIndex();
foreach ($discount_index as $d){
if (isset( $_COOKIE[$d[‘cookie’]] ) && $product -> get_id() == $d[‘product_id’]){
$sale_price = $product -> get_regular_price() * $d[‘discount’];
}
}
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price();
else
return $sale_price;
};
// Displayed formatted regular price + sale price
add_filter( ‘woocommerce_get_price_html’, ‘custom_dynamic_sale_price_html’, 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type(‘variable’) ) return $price_html;
$salePrice = $product->get_sale_price();
$regularPrice = $product->get_regular_price();
if ($salePrice != $regularPrice){
$price_html = wc_format_sale_price(
wc_get_price_to_display(
$product,
array( ‘price’ => $regularPrice )
),
wc_get_price_to_display(
$product,
array( ‘price’ => $salePrice )
)
).$product->get_price_suffix();
}
return $price_html;
}
add_action(‘woocommerce_before_calculate_totals’, ‘changeCartPrices’, 20, 1);
function changeCartPrices($cart_object){
foreach($cart_object -> get_cart() as $cart_item_key => $cart_item){
$salePrice = $cart_item[‘data’] -> get_sale_price();
$regularPrice = $cart_item[‘data’] -> get_regular_price();
if ($salePrice != $regularPrice){
$cart_item[‘data’]->set_price($salePrice);
}
}
}
add_filter( ‘woocommerce_cart_item_price’, ‘cart_price_total’, 10, 3 );
function cart_price_total( $old_display, $cart_item, $cart_item_key ) {
/** @var WC_Product $product */
$product = $cart_item[‘data’];
if ( $product ) {
return $product->get_price_html();
}
return $old_display;
}
add_filter(‘woocommerce_cart_item_subtotal’, ‘cart_sale_total’, 10, 3);
function cart_sale_total($subtotal, $cart_item, $cart_item_key){
$discount_index = getDiscountIndex();
$product = $cart_item[‘data’];
$quantity = $cart_item[‘quantity’];
$regularPrice = $product -> get_regular_price();
$salePrice = $product->get_sale_price();
if ($salePrice != $regularPrice){
$price = wc_format_sale_price(
wc_get_price_to_display(
$product,
array(
‘price’ => $regularPrice,
‘qty’ => $quantity
)
),
wc_get_price_to_display(
$product,
array(
‘price’ => $salePrice,
‘qty’ => $quantity
)
)
).$product->get_price_suffix();
} else {
$price = wc_price( $regularPrice ) . $product->get_price_suffix();
}
return $price;
}
