Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

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

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 like to effectively reverse this and show 'Up to: £50'.

I have tried to tweak the code below, but just can't figure it out:

function custom_variable_price_range( $price_html, $product ) {
    $prefix = sprintf('%s: ', __('From', 'woocommerce') );

    $min_regular_price = $product->get_variation_regular_price( 'min', true );
    $min_sale_price    = $product->get_variation_sale_price( 'min', true );
    $max_price         = $product->get_variation_price( 'max', true );
    $min_price         = $product->get_variation_price( 'min', true );

    $price_html = ( $min_sale_price == $min_regular_price ) ? wc_price( $min_regular_price ) :
    '<del>' . wc_price( $min_regular_price ) . '</del>' . '<ins>' . wc_price( $min_sale_price ) . '</ins>';

    return ( $min_price == $max_price ) ? $price_html : sprintf( '%s%s', $prefix, $price_html );
}
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 );

This is how I would like it to look:

Example

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ) :
    '<del>' . wc_price( $max_regular_price ) . '</del> <ins>' . wc_price( $max_sale_price ) . '</ins>';

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...