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:
Any help is appreciated.
解决方案
以下代码将给出后缀为“Up to:”的“max”可变格式价格,并处理促销价格范围:
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 文件中。测试和工作。
如果您不想处理促销价格范围,您将使用:
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 文件中。测试和工作。
推荐阅读
- python - 是否可以在 Python 中与一个固定系列进行运行关联?
- mysql - SQL-如何获取每行的总和并在条件存在时将其放入,如果不存在则将其放入最后一行
- javascript - 转换大约 2 GB 的巨大 CSV 文件
- c# - 如何在 C# 中修复正则表达式
- r - 在 R 中使用 for 循环将名称存储到对象中时遇到问题
- javascript - ES6方式 - 按键从嵌套数组中获取唯一值
- outlook - 用户尝试在共享日历中创建约会时引发异常
- go - fmt 包中不同的打印功能有什么区别?
- linux - 在 ec2 linux AMI 2 (c6g) 上安装 dotnet core
- java - 如何解决此错误 E/MultiWindowProxy: getServiceInstance failed!(安卓工作室)