Replace WooCommerce variable products price range with #39;Up to#39; and the max price(将 WooCommerce 可变产品价格范围替换为“最高和最高价格)
问题描述
我发现以下代码(来自
感谢任何帮助.
以下代码将给出max"变量格式的价格后缀为Up to:"并处理销售价格范围:
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 );函数 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>';返回 $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 );函数 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 );返回 $min_active_price == $max_active_price ?$price_html : sprintf('%s %s', $prefix, $price_html );}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
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.
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.
这篇关于将 WooCommerce 可变产品价格范围替换为“最高"和最高价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 WooCommerce 可变产品价格范围替换为“最高&qu
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01