How to restore price suffix for variable product in WooCommerce

WooCommerce does not show price suffix for variable products if the suffix contains placeholder variables. The reason is explained in the comment of WC_Product_Variable::get_price_html method:

Note: Variable prices do not show suffixes like other product types. This is due to some things like tax classes being set at variation level which could differ from the parent price. The only way to show accurate prices would be to load the variation and get IT’s price, which adds extra overhead and still has edge cases where the values would be inaccurate.

If you still want to show the price suffix just like for normal products, do as Mike Jolley laconically says in this issue report:

Use the filter in that method if you want to force it programmatically.

So, here is my take on what that filter would look like:

add_filter('woocommerce_get_price_suffix', function ( $html, $product, $price, $qty ) {
     if ( ! $html && $product instanceof WC_Product_Variable) {
         // Copied from plugins/woocommerce/includes/abstracts/abstract-wc-product.php#get_price_suffix
         if ( ( $suffix = get_option( 'woocommerce_price_display_suffix' ) ) 
             && wc_tax_enabled() 
             && 'taxable' === $product->get_tax_status() 
         ) {
             $replacements = array(
                 '{price_including_tax}' => wc_price( wc_get_price_including_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ),
                 '{price_excluding_tax}' => wc_price( wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ),
             );
             $html = str_replace( array_keys( $replacements ), array_values( $replacements ), ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' );
         }
     }
 
     return $html;
}, 10, 4);

 

8 Replies to “How to restore price suffix for variable product in WooCommerce”

    1. Hi Diana,
      if you have a custom theme you can add it in functions.php of your theme. Otherwise you need to create a custom child theme or plugin and place it there, but that’s a lot more work, I’m afraid.

      1. You can also add that code snippet directly to you active theme (function.php file) with the consequence that it will dissapear when updated.

        You can add it to functions.php (not update safe)
        You can create a child theme like you mention (update safe)
        You can even create a custom plugin (update safe if you place it in the correct folder)
        – Woocommerce has a easy-to-follow guide on this 🙂

        Anyways, thx for the code 😉

  1. Hi!
    This code works beautifully! Only problem is that I want to use it in conjunction with a snippet that removes the variable price range and replaces it with “From: XX” and when doing so, your code stops working. Any idea how to get this working?

    Thanks for the tutorial!

    Niklas

    1. Hi Niklas,
      hard to say… Maybe your snippet replaces the caller of this filter and this one doesn’t get called any more. In that case you could perhaps modify your snippet to call this filter or do the same thing directly. I’m sorry I cannot help more 🙁

  2. Hi Jarno,

    The snippet works great when all the variations are the same price so we get something like this…

    €32.30 ex.Vat | €39.73 inc.VAT

    So far so good… but if the variations have different prices we get…

    €71.89 – €94.10 ex.Vat | €88.42 inc.VAT

    Any thoughts on how we could get both the inc.VAT prices to display?

    Cheers,

    Eoin

  3. thanks for your code, I have tried it on wordpress 5.0.3 and woocommerce 3.5.4 and I put it into the snippets plugin, but it makes no difference to the variations price. It does not show the vat incl price. I wonder has the name of the filter changed?
    thanks
    Breda

    1. Hi Breda,

      I don’t remember which WC version I had at the time, but it’s been over a year, so could be that something is changed there. I don’t work with WP any more, so I cannot help with that, sorry!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.