我使用此功能,在单个产品页面上工作正常。但是在分类页面上,它一直告诉我:'警告:零分区。谢谢你的帮助!
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
if ($percentage < 0) { return null;}
else {
if (is_product()) {
return $price . sprintf( __('<br><span style="font-size:14px;font-weight:bold; color:#ef4136">-%s DE DESCONTO</span>', 'woocommerce' ), $percentage . '%' );}
else {
return $price . sprintf( __('<span style="font-size:14px;font-weight:bold; color:#ef4136">-%s DE DESCONTO</span>', 'woocommerce' ), $percentage . '%' );}
}
}
答案 0 :(得分:0)
function woocommerce_custom_sales_price( $price, $product ) {
if($product->regular_price > 0 ){ // Do a check if you have set the regular price of product.
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
}
if ($percentage < 0) { return null;}
else {
if (is_product()) {
return $price . sprintf( __('<br><span style="font-size:14px;font-weight:bold; color:#ef4136">-%s DE DESCONTO</span>', 'woocommerce' ), $percentage . '%' );}
else {
return $price . sprintf( __('<span style="font-size:14px;font-weight:bold; color:#ef4136">-%s DE DESCONTO</span>', 'woocommerce' ), $percentage . '%' );}
}
}