skip to Main Content

I’m bulding a store on woocommerce and I have variable products and every variation has its unique prices. What i’m trying to do is always show the lowest price, even if no option is selected. And this price should change when the options are selected later (like the one in the bottom after the options, but always displayed).

It’s not important if it’s show after or before the options.

I try to use this:

add_filter( 'woocommerce_show_variation_price', '__return_true' );

But It’s not working in the latest version of woocommerce.
Thanks for your help.

2

Answers


  1. To show the lowest price use below:

    add_filter( 'woocommerce_variable_sale_price_html', 'custom_variation_price_format', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_variation_price_format', 10, 2 );
    
    function custom_variation_price_format( $price, $product ) {
    
        // Main Price
        $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
        $price = $prices[0] !== $prices[1] ? sprintf( __( '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( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
    
        if ( $price !== $saleprice ) {
            $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
        }
        return $price;
    }
    
    Login or Signup to reply.
  2. Please use below function:

    add_filter( 'woocommerce_format_sale_price', 'addweb_solu_sale_price', 20, 3 );
    function addweb_solu_sale_price( $price, $regular_price, $sale_price ) {
        return wc_price( $sale_price );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search