Asked  7 Months ago    Answers:  5   Viewed   59 times

IN WooCommerce, I need to multiply all product prices by a number. So I have used the following (via a plugin):

add_filter('woocommerce_get_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_price', array( $this, 'my_custom_price'), 99);

function my_custom_price( $original_price ) {
  global $post, $woocommerce;

  //Logic for calculating the new price here
  $new_price = $original_price * 2;

  //Return the new price (this is the price that will be used everywhere in the store)
  return $new_price;
 }

But, that doesn't work for variation products. I have tried the following hooks with no luck:

add_filter('woocommerce_get_variation_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_variation_price', array( $this, 'my_custom_price'), 99);

The only one that works half way is this one:

add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);

But that just changed the overall price, not the selected variation price. See the image below, price is BsF. 200 and the overall price is right, 200 x 2 = 400, but the variation price when selected still shows 200:

Note: I need it to actually change, so display html hooks wont work.

Variation Price

Is there anything I'm missing, or something wrong?

 Answers

37

Update (December 2020)

  • 2 code versions for themes and plugins (works in Woocommerce 3.3.x too)
  • Cached variations prices in Woocommerce 3 (Update and addition):
    Now using woocommerce_get_variation_prices_hash filter hook much more efficient, instead of wc_delete_product_transients()… See this related thread
  • Added product price filter widget hooks (see at the end).

1) Plugin version with a constructor function:

The hooks that you are using are deprecated in WooCommerce 3+

To make it work for all products prices, including variations prices, you should use this:

## The following goes inside the constructor ##

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
// Variations 
add_filter('woocommerce_product_variation_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_variation_get_price', array( $this, 'custom_price' ), 99, 2 );

// Variable (price range)
add_filter('woocommerce_variation_prices_price', array( $this, 'custom_variable_price' ), 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', array( $this, 'custom_variable_price' ), 99, 3 );

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_price_multiplier_to_variation_prices_hash' ), 99, 3 );


## This goes outside the constructor ##

// Utility function to change the prices with a multiplier (number)
public function get_price_multiplier() {
    return 2; // x2 for testing
}

public function custom_price( $price, $product ) {
    return (float) $price * get_price_multiplier();
}

public function custom_variable_price( $price, $variation, $product ) {
    return (float) $price * get_price_multiplier();
}

public function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
    $price_hash[] = get_price_multiplier();
    return $price_hash;
}

The code tested and perfectly works (only) in WooCommerce 3+.


2) For theme version: functions.php file on active child theme (or active theme):

// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
    return 2; // x2 for testing
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
// Variations
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
    return (float) $price * get_price_multiplier();
}

// Variable (price range)
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
function custom_variable_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    // wc_delete_product_transients($variation->get_id());

    return (float) $price * get_price_multiplier();
}

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 );
function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
    $price_hash[] = get_price_multiplier();
    return $price_hash;
}

Tested and works on woocommerce 3+


For products in sale you have those hooks:

  • woocommerce_product_get_sale_price (Simple, grouped and external products)
  • woocommerce_variation_prices_sale_price (Variable products (min-max))
  • woocommerce_product_variation_get_sale_price (Products variations)

Cached prices and woocommerce 3:

The 3 filters hooks involved in variations cached prices are:

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

Introduced in Woocommerce 3, woocommerce_get_variation_prices_hash filter hook will allow to refresh variations cached prices in a much more efficient way, without deleting related transients anytime that this hooks are executed.

So performances will stay boosted (Thanks to Matthew Clark that pointed this better way)

See: Caching and dynamic pricing – upcoming changes to the get_variation_prices method


For filtering product prices with a widget (min and max price), use the following hooks:

  • woocommerce_price_filter_widget_min_amount that has one argument $price
  • woocommerce_price_filter_widget_max_amount that has one argument $price
Wednesday, March 31, 2021
 
exxed
answered 7 Months ago
96

I have revisited your code… I have merged the pricing updates in a separated utility function…

To avoid your problems is necessary to define the product ID (checking the product type before).
Then we set this correct defined product ID in WordPress has_term() conditional function.

Now your prices will work on cart and checkout pages too…

Your revisited code:

// Utility pricing function
function filtering_product_prices( $price, $product ) {
    // Get the product ID
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for Woocomerce Product Tag "ama"
    if ( ! has_term( 'ama', 'product_tag', $product_id ) ) return $price; // Exit

    if ( $price < 5 ) {
        $price *= 2.5;
    } elseif ( $price >=  5 && $price < 10 ) {
        $price *= 2;
    } elseif ( $price >= 10 && $price < 20 ) {
        $price *= 1.75;
    } elseif ( $price >= 20 && $price < 40 ) {
        $price *= 1.5;
    } elseif ( $price >= 40 && $price < 60 ) {
        $price *= 1.35;
    } elseif ( $price >= 60 && $price < 80 ) {
        $price *= 1.25;
    } elseif ( $price >= 80 && $price < 1000 ) {
        $price *= 1.20;
    }
    return ceil($price + 0.01) - 0.01;
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );

function custom_variation_price( $price, $variation, $product ) {
    return filtering_product_prices( $price, $product );
}

// Variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );

function custom_price( $price, $product ) {
    return filtering_product_prices( $price, $product );
}

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

Related answer: Conditional product prices cart issue in WooCommerce 3

Wednesday, March 31, 2021
 
akohout
answered 7 Months ago
42

To make it work, you will better target the WC_Product method get_tax_class() through dedicated related composite hooks, this way:

add_filter('woocommerce_product_get_tax_class', 'switch_product_tax_class', 100, 2 );
add_filter('woocommerce_product_variation_get_tax_class', 'switch_product_tax_class', 100, 2 );
function switch_product_tax_class( $tax_class, $product ){
    if( isset($_COOKIE["customerType"]) && $_COOKIE["customerType"] == 'business' ){
        return "Zero Rate";
    }
    return $tax_class;
}

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


Based on WC_Customer is_vat_exempt property, you could also try to use the following instead:

add_action( 'template_redirect', 'vat_exempt_b2b_customers' );
function vat_exempt_b2b_customers() {
    if( isset($_COOKIE["customerType"]) && $_COOKIE["customerType"] === 'business' 
    && ! WC()->customer->is_vat_exempt() ){
        WC()->customer->set_is_vat_exempt( true );
    }
}

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

Wednesday, March 31, 2021
 
SubniC
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
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