【发布时间】:2017-02-14 15:28:34
【问题描述】:
我正在尝试在使用 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 ){
$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 . '%' );
}
}
但它不起作用,百分比不适用于价格。
也不在前端。
【问题讨论】:
-
这里没有人会读心术。在问题的正文中描述“仍然无效”的含义。错误信息?日志?有什么事吗?
标签: php wordpress woocommerce variations