Roshan Zaid
Roshan Zaid

Reputation: 17

WooCommerce - Show Regular and Sale Price for The Product Variation which has Flat Price

The scenario is Simple product, Variable product are woocommerce's two different product types.

What I am trying to achieve is, there are some products which have over 50+ variations which have a different sale and regular prices. A price range for that product is fare.

But there are also products which's variations have same regular and sale prices. No difference in prices. I want the price to be shown just as

enter image description here


I agree the question arises that why shouldn't I go with simple products? Because in simple products I am not allowed to add the colors that are available in drop downs. Any suggestions please?

Upvotes: 0

Views: 3327

Answers (1)

Roshan Zaid
Roshan Zaid

Reputation: 17

I was finding the exact hook to fix the problem with codeNinja snippet! This is how it is fixed:

function wc_ninja_custom_variable_price( $price, $product ) {
    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'Starting From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice ) {
        $price = ' <ins class="highlight"> '. $price.' </ins> <del class="strike"> '.$saleprice .' </del> ';
    }

    return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_ninja_custom_variable_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_ninja_custom_variable_price', 10, 2 );

Upvotes: 1

Related Questions