Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Variation Price woocommerce when all prices are equal

So I'm working on a store in woocommerce and we want the variation price (that displays just above the add cart button) to be the only one on the page, and we've discovered if a variable product has the same price for all variations it won't show the price down there, does anyone have a solution for this?

like image 451
user1917147 Avatar asked Dec 11 '22 05:12

user1917147


1 Answers

I know this question was asked almost a year ago, but this has been a "problem" in WooCommerce for much longer, so I'm posting this answer for reference.

In actual fact this is meant to be a performance/memory enhancement feature to avoid rendering and processing duplicated variation data in the HTML data attribute. But it ends up causing issues because it results in the HTML output of variations not being consistent from product to product. Really this whole issue is due to bad practices being used by WooCommerce to render the dynamic content for the variations, but what can you do...

This! (in PHP 5.3+, which you really should be using these days)

add_filter('woocommerce_available_variation', function($available_variations, \WC_Product_Variable $variable, \WC_Product_Variation $variation) {
    if (empty($available_variations['price_html'])) {
        $available_variations['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    }

    return $available_variations;
}, 10, 3);

For those who don't know, you simply need to add this to your theme's functions.php file, or any other PHP file that loads before the template files.

like image 94
slightlyfaulty Avatar answered Jun 18 '23 19:06

slightlyfaulty