Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set product sale price programmatically in WooCommerce 3

I have a Woocommerce store set up with various product categories.

I want to apply a 20% discount to all products, which belong to the product category Cuckoo

For now all I'm trying to achieve is set a sale price in my functions.php

It tried as follows:

    /* 
     * For a specific date, 20% off all products with product category as cuckoo clock.
     */
    function cuckoo_minus_twenty($sale_price, $product) { 
        $sale_price = $product->get_price() * 0.8;
        return $sale_price;
    }; 

    // add the action 
    add_filter( 'woocommerce_get_sale_price', 'cuckoo_minus_twenty', 10, 2 ); 

If I var_dump the result of $sale_price after the calculation I get the correct answer, however the price display on the front-end strikes out the regular price and displays the sale price as the regular price.

enter image description here

Is there a hook/filter I can use to achieve this?

I've also tried setting the sale price by doing:

$product->set_sale_price($sale_price); 

to no avail.

like image 978
Marcus Christiansen Avatar asked Feb 13 '18 09:02

Marcus Christiansen


2 Answers

The hook woocommerce_get_sale_price is deprecated since WooCommerce 3 and replaced by woocommerce_product_get_sale_price.

Also Product displayed prices are cached. When sale price is active, regular price is also active.

Try this instead:

// Generating dynamically the product "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 ) {
    $rate = 0.8;
    if( empty($sale_price) || $sale_price == 0 )
        return $product->get_regular_price() * $rate;
    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;

    $price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display(  $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();

    return $price_html;
}

Code goes in function.php file of your active child theme (active theme).

Tested and works on single product, shop, product category and tag archive pages.

The continuation in:
Wrong Woocommerce cart item price after setting programmatically product sale price

like image 158
LoicTheAztec Avatar answered Sep 18 '22 13:09

LoicTheAztec


I realiced, you more or less need all of the following filters to make the HTML work out of the box.

add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
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 );
add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );

And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients if you want to display it correctly for variable products.

You may find the gist i've created for a client useful

https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020

like image 32
Xandl Avatar answered Sep 18 '22 13:09

Xandl