用“最多"和最高价格替换WooCommerce可变产品的价格范围 [英] Replace WooCommerce variable products price range with 'Up to' and the max price

查看:239
本文介绍了用“最多"和最高价格替换WooCommerce可变产品的价格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了以下代码(来自此处),使我能够在WooCommerce的可变价格产品上展示:"发件人:10英镑"(在10英镑至50英镑的产品上).我想有效地扭转这种状况,并显示"最高50英镑".

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 );

这就是我想要的样子:

感谢您的帮助.

推荐答案

以下代码将给出以"max"变量格式的价格,后缀"Up to:"并处理销售价格范围:

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);
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

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 );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

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

这篇关于用“最多"和最高价格替换WooCommerce可变产品的价格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆