Php 将折扣百分比添加到销售的可变产品中
我正在尝试在一个使用WooCommerce的网站中添加一个折扣百分比 我已将此脚本应用于标准价格和销售价格:Php 将折扣百分比添加到销售的可变产品中,php,wordpress,woocommerce,variations,Php,Wordpress,Woocommerce,Variations,我正在尝试在一个使用WooCommerce的网站中添加一个折扣百分比 我已将此脚本应用于标准价格和销售价格: // Add save percentage next to sale item prices. add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 ); function adventure_tours_sales_price( $price, $product ){ $per
// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
上面的剧本很好用
在前端,我有价格百分比
现在,我想将相同的脚本应用于产品变体价格
我已经检查了产品变体选项,并尝试了以下方法:
// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
if( $product->is_type( 'variable' ) ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}else{
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
}
但它不起作用,百分比不适用于价格
也不是在前端
针对WooCommerce版本3+更新;不推荐的替换版本
- 将“woocommerce\u variable\u sale\u price\u html”替换为“woocommerce\u variable\u get\u price\u html”
- 将“woocommerce\u sale\u price\u html”替换为“woocommerce\u get\u price\u html”
- 将“woocommerce\u price()”替换为“wc\u price()”
- 将
价格属性替换为WC\u产品
价格方法WC\u产品
对于可变产品而言,更为复杂,因为您有两个不同的位置具有价格,第一个位置显示最小和最大价格(当您有多个变体时),第二个位置显示所选变体的价格。我已经改变了很多你原来的代码 下面是显示折扣百分比周围自定义动态标签的正确代码:
add_filter('woocommerce_variable_get_price_html','adventure_tours_sales_price', 10, 2 );
add_filter('woocommerce_get_price_html','adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
// Variables initialisation
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$save_text = __('Save', 'woocommerce') . ' ';
if(!empty($sale_price)) {
// Percentage calculation
$percentage = '<span class="save-percent"> ' .$save_text . round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ) . '%</span>';
$price = '<del class="strike">' . wc_price( $regular_price ) . '</del>
<ins class="highlight">' . wc_price( $sale_price ) . $percentage . '</ins>';
} else {
$price = '<ins class="highlight">'.wc_price( $regular_price ).'</ins>';
}
return $price;
}
add_filter('woocommerce_variable_get_price_html', 'adventure_tours_sales_min_max_prices', 20, 2);
function adventure_tours_sales_min_max_prices( $price, $product) {
// Variables initialisation
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_max_reg_price = $product->get_variation_regular_price('max', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
$variation_max_sale_price = $product->get_variation_sale_price('max', true);
$percentage_min = '';
$percentage_max = '';
$save_text = __('Save', 'woocommerce') . ' ';
// Percentage calculations
if($variation_min_reg_price != $variation_min_sale_price)
$percentage_min = '<span class="save-percent-min"> (' .$save_text . round( ( ( $variation_min_reg_price - $variation_min_sale_price ) / $variation_min_reg_price ) * 100 ) . '%)</span>';
if($variation_max_reg_price != $variation_max_sale_price)
$percentage_max = '<span class="save-percent-max"> (' .$save_text . round( ( ( $variation_max_reg_price - $variation_max_sale_price ) / $variation_max_reg_price ) * 100 ) . '%)</span>';
if (($variation_min_reg_price != $variation_min_sale_price) || ($variation_max_reg_price != $variation_max_sale_price)) {
$price = '<del class="strike">' . wc_price($variation_min_reg_price) . '-' . wc_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . wc_price($variation_min_sale_price) . $percentage_min . ' - ' . wc_price($variation_max_sale_price) . $percentage_max . '</ins>';
}
return $price;
}
add_filter('woocommerce_variable_get_price_html','adventure_tours_sales_price',10,2);
添加过滤器('woocommerce\u get\u price\u html'、'adventure\u tours\u sales\u price',10,2);
功能探险旅游销售价格($price,$product){
//变量初始化
$regular_price=$product->get_regular_price();
$sale_price=$product->get_sale_price();
$save_text=uuu('save','woocommerce');
如果(!空($sale_price)){
//百分比计算
$percentage='.$save_text.round(($regular_price-$sale_price)/$regular_price)*100)。'%;
$price=''.wc_价格(常规价格)。'
“.wc_价格($sale_价格)。$percentage.”;
}否则{
$price=''.wc_价格($regular_价格)。'';
}
返回$price;
}
添加过滤器('woocommerce\u variable\u get\u price\u html'、'adventure\u tours\u sales\u min\u max\u prices',20,2);
功能探险旅游销售最低最高价格($price,$product){
//变量初始化
$variation\u min\u reg\u price=$product->get\u variation\u regular\u price('min',true);
$variation\u max\u reg\u price=$product->get\u variation\u regular\u price('max',true);
$variation\u min\u sale\u price=$product->get\u variation\u sale\u price('min',true);
$variation\u max\u sale\u price=$product->get\u variation\u sale\u price('max',true);
$percentage_min='';
$percentage_max='';
$save_text=uuu('save','woocommerce');
//百分比计算
if($variation\u min\u reg\u price!=$variation\u min\u sale\u price)
$percentage_min='('.$save_text.round(($variation_min_reg_price-$variation_min_sale_price)/$variation_min_reg_price)*100)。';
如果($variation\u max\u reg\u price!=$variation\u max\u sale\u price)
$percentage_max='('.$save_text.round(($variation_max_reg_price-$variation_max_sale_price)/$variation_max_reg_price)*100)。';
if($variation_min_reg_price!=$variation_min_sale_price)| |($variation_max_reg_price!=$variation_max_sale_price)){
$price=''.wc_价格($variation_min_reg_price)。'-'.wc_价格($variation_max_reg_price)。'
“.wc_价格($variation_min_sale_price)。$percentage_min.-”.wc_价格($variation_max_sale_price)。$percentage_max.”;
}
返回$price;
}
代码位于活动子主题(或主题)的functions.php文件或任何插件文件中
在Woocommerce版本3上测试并运行+
有关答案: