Asked  7 Months ago    Answers:  5   Viewed   104 times

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.

 Answers

45

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

Wednesday, March 31, 2021
 
hohner
answered 7 Months ago
100

I have revisited code and simplified it (for single products only):

add_filter( 'woocommerce_get_price_html', 'custom_product_price_html', 10, 2 );
function custom_product_price_html( $price, $product ) {
    // For simple products only
    if ( $product->is_type('simple') ) {

        $unit_divider = 6;
        $suffix_box   = ' '. __('(per box)', 'woocommerce');
        $suffix_unit  = ' '. __('(per bottle)', 'woocommerce');

        $active_price_unit  = wc_get_price_to_display( $product, array( 'price' => ( $product->get_price() / $unit_divider ) ) );

        if($product->is_on_sale() )
        {
            $regular_price_unit   = wc_get_price_to_display( $product, array( 'price' => ( $product->get_regular_price() / $unit_divider ) ) );
            $formatted_price_unit = wc_format_sale_price( $regular_price_unit, $active_price_unit );

            $price .= $suffix_box . '<br><span class="unit-price">' . $formatted_price_unit . $suffix_unit . '</span>';
        }
        else
        {
            $price .= $suffix_box . '<br><span class="unit-price">' . wc_price($active_price_unit) . $suffix_unit . '</span>';
        }
    }
    return $price;
}

Code goes in function.php file of your active child theme (or theme). Tested and works.


Related:

  • Display on shop pages the unit price and the wholesale price on product pages
  • Show a price suffix only on all WooCommerce single products
Wednesday, March 31, 2021
 
Null
answered 7 Months ago
72

To Set only specific product sale price (the products IDs are defined in the 1st function), try this:

// HERE below in the array set your specific product IDs
function specific_product_ids(){
    return array(37, 43, 57); //  <===  <===  <===  <===  Your Product IDs
}

// 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 ) && in_array($product->get_id(), specific_product_ids() ) )
        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($regular_price) || $regular_price == 0 ) && in_array($product->get_id(), specific_product_ids() ) )
        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;

    if( in_array($product->get_id(), specific_product_ids() ) ) {
        $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).

The continuation in:
Set only for specific products the generated sale price in woocommerce cart

Saturday, May 29, 2021
 
TMichel
answered 5 Months ago
64

As this is a filter and as the data is cached, you can't get any output with print_r().

The correct way to make it work is the following:

add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
function custom_shipping_costs( $rates, $package ) {
    // New shipping cost (can be calculated)
    $new_cost = 1000;
    $tax_rate = 0.2;

    foreach( $rates as $rate_key => $rate ){
        // Excluding free shipping methods
        if( $rate->method_id != 'free_shipping'){

            // Set rate cost
            $rates[$rate_key]->cost = $new_cost;

            // Set taxes rate cost (if enabled)
            $taxes = array();
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $rates[$rate_key]->taxes[$key] > 0 )
                    $taxes[$key] = $new_cost * $tax_rate;
            }
            $rates[$rate_key]->taxes = $taxes;

        }
    }
    return $rates;
}

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

Tested and works.

Sometimes, you should may be need to refresh shipping methods:
1) Empty cart first.
2) Go to shipping Zones settings, then disable/save and re-enable/save the related shipping methods.

Friday, July 30, 2021
 
ClydeFrog
answered 3 Months ago
27

Update 2 (for simple and variable products + solved the bug on same variations prices)

when products are on sale, you can add custom labels just as you want using a custom function hooked in woocommerce_sale_price_html and woocommerce_variation_sale_price_html filters hooks (for simple and variables products.

For the min / max prices in variables products, we need a different function hooked in woocommerce_variation_sale_price_html filter hook.

Here is that code:

add_filter('woocommerce_variation_sale_price_html','sale_prices_custom_labels', 10, 2 );
add_filter('woocommerce_sale_price_html','sale_prices_custom_labels', 10, 2 );
function sale_prices_custom_labels( $price, $product ){
    if (isset($product->sale_price)) {
        $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price( $product->regular_price ). '</del>
        <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price( $product->sale_price ) . '</ins>';
    }
    else
    {
        $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
    }
    return $price;
}

add_filter('woocommerce_variable_sale_price_html', 'sale_prices_custom_labels_min_max', 20, 2);
function sale_prices_custom_labels_min_max( $price, $product) {

    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_max_reg_price = $product->get_variation_regular_price('max', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    $variation_max_sale_price = $product->get_variation_sale_price('max', true);

    if ( $variation_min_reg_price != $variation_min_sale_price || $variation_max_reg_price != $variation_max_sale_price )
    {
        if($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price ){
            $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
            <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
        elseif($variation_min_reg_price != $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price )
        {
            $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
            <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
        elseif($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price != $variation_max_sale_price )
        {
            $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
            <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
        else
        {
        $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
        <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
    }
    return $price;
}

You can also replace the normal <ins> and <del> html tags by something else and change or add some classes too (if is more convenient for you). At this point everithing is possible.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


Related answers: Conditional custom output around products sale price and regular price

Saturday, July 31, 2021
 
Miguel Ping
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share