0

My variable products show two price sections, one is the price range for all the products and the other is the price for the chosen product if one is selected. But if the price for the products is the same it only shows one price.

What I want is when an option is selected if the price of the products ranges I want to replace the price range with the price of the selected variation. And if no option is selected I want the price range to show. I haven't been able to find the correct filter or hook for this, can you help?

Here is a couple images to show you what I am talking about;

enter image description here

enter image description here

Yagnesh Makwana's code could be used to make this functionality work but, I need to be able to get the price of the currently selected variation. Look in the if statement below $variation_price needs to be set equal to the current selected variation price;

add_filter( 'woocommerce_variable_sale_price_html', 'detect_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'detect_variation_price_format', 10, 2 );

function detect_variation_price_format( $price, $product ) {

    // get min and max price of varaitions and store in $prices array
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    //if min and max price are not the same
    if ($prices[0] !== $prices[1]) {
        $variation_price = get selected variation price;
        $price = sprintf( __( $variation_price, 'woocommerce') );
    }   

    return $price;
}
Reece
  • 2,581
  • 10
  • 42
  • 90

1 Answers1

1

Hello @Reece below code usefull for you for remove price range in woocommerce.

You can use given hooks for remove price range.

//Remove Price Range
add_filter( 'woocommerce_variable_sale_price_html', 'detect_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'detect_variation_price_format', 10, 2 );

function detect_variation_price_format( $price, $product ) {

    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    if ($prices[0] !== $prices[1]) {
        $price = $prices[0] !== $prices[1] ? sprintf( __( ”, ‘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 = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }
    return $price;
}

You can change in function based on your theme by template overriding of woo-commerce plugin.

For further detail please see link: https://annaschneider.me/hide-the-price-range-for-woocommerce-variable-products/

Yagnesh Makwana
  • 301
  • 2
  • 11
  • Thanks for your help but I am getting a couple errors from this code. Notice: Use of undefined constant ” - assumed '”' and Notice: Use of undefined constant ‘woocommerce’ - assumed '‘woocommerce’' its to do with the line of code in the first if statement – Reece Sep 12 '18 at 10:40
  • also I don't believe this is exactly what I am looking for. I only want to remove the price range if an option is selected. But I may be able to modify this code. Thanks – Reece Sep 12 '18 at 10:42
  • I have fixed the function. Now I'll try make it do what I want it too – Reece Sep 12 '18 at 10:45
  • @Reece I have written above for change in function based on your functionality but if you face any issue then let me know – Yagnesh Makwana Sep 12 '18 at 11:02
  • I have edited my question it may give you a better idea of what I am after. Take a look if you have time. thanks – Reece Sep 12 '18 at 11:28
  • Please check if this will help to you https://stackoverflow.com/questions/44912300/replace-the-variable-price-range-by-the-chosen-variation-price-in-woocommerce-3 – Yagnesh Makwana Sep 12 '18 at 11:47