Search code examples
phpwordpresswoocommerceproduct

Displaying price only when the variation is selected and discount in percentage relatively to the regular price


I've this problem. Let me show pictures so I can explain better.

Variation product selected but because all the variations have the same price the price do not show in the bottom: Variation product selected but because all the variations have the same price the price do not show in the bottom.

Variation product selected, because they have different PROMO prices they show on top and the regular-promo price after selection: Variation product selected, because they have different PROMO prices they show on top and the regular-promo price after selection

What I need is that only after selected the variations the price is show in the bottom like the second image and calculation the discount between the promo variation price and the regular variation price. I need the same behavior in the two cases.

I've searched a lot, but none of the things have solved this behavior. Here some answer that are very close:


Solution

  • After some search, there this simple dedicated filter hook woocommerce_show_variation_price that will make exactly what you are expecting:

    add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
    function filter_show_variation_price( $condition, $product, $variation ){
        if( $variation->get_price() === "" ) return false;
        else return true;
    }
    

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

    Tested and work… This will display the selected variation price even if all variations prices are the same…