Replace WooCommerce variable products price range with 'Up to' and the max price

前端 未结 2 1822
误落风尘
误落风尘 2020-12-07 05:10

I found the following code (from here) which enables me to show on a variable price product on WooCommerce: \'From: £10\' (on a £10 - £50 product). I would

2条回答
  •  春和景丽
    2020-12-07 05:46

    The following code will give the "max" variable formatted price suffixed with "Up to:" and handling on sale price range:

    add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );
    function custom_variable_price_range( $price_html, $product ) {
    
        $prefix = __('Up to', 'woocommerce');
    
        $max_regular_price = $product->get_variation_regular_price( 'max', true );
        $max_sale_price    = $product->get_variation_sale_price( 'max', true );
        $max_active_price  = $product->get_variation_price( 'max', true );
        $min_active_price  = $product->get_variation_price( 'min', true );
    
        $price_html = ( $max_sale_price == $max_regular_price ) ? wc_price( $max_active_price ) :
        '' . wc_price( $max_regular_price ) . ' ' . wc_price( $max_sale_price ) . '';
    
        return $min_active_price == $max_active_price ? $price_html : sprintf('%s %s', $prefix, $price_html);
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    If you don't want to handle on sale price range, you will use instead:

    add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );
    function custom_variable_price_range( $price_html, $product ) {
    
        $prefix = __('Up to', 'woocommerce');
    
        $max_active_price  = $product->get_variation_price( 'max', true );
        $min_active_price  = $product->get_variation_price( 'min', true );
    
        $price_html = wc_price( $max_active_price );
    
        return $min_active_price == $max_active_price ? $price_html : sprintf('%s %s', $prefix, $price_html );
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

提交回复
热议问题