Woocommerce “国际商业”;除零“;折扣百分比文本错误
我正在编写这段代码,虽然我已经尽了最大努力使其正常工作,但我无法修复分组产品的存档(商店页面)中出现的“除以零”错误 这是错误消息:Woocommerce “国际商业”;除零“;折扣百分比文本错误,woocommerce,Woocommerce,我正在编写这段代码,虽然我已经尽了最大努力使其正常工作,但我无法修复分组产品的存档(商店页面)中出现的“除以零”错误 这是错误消息: 警告:在 这使得百分比文本显示如下:Save:-$18(-INF%) 错误是指这一行: $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%'; 以下是完整的代码: add_filter( 'woocommerce_get_price_html', '
警告:在
这使得百分比文本显示如下:Save:-$18(-INF%)
错误是指这一行:
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
以下是完整的代码:
add_filter( 'woocommerce_get_price_html', 'display_sale_price_and_percentage_html', 10, 2 );
function display_sale_price_and_percentage_html( $price, $product ) {
// sale products on frontend excluding variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')) {
// product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price();
// price calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// percentage calculation and formatting
$precision = 1; // decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// display the formatted html price including amount and precentage using a span tag which means displaying it on the same row, if you want this on a new row, change the tag into a paragraph
$price .= sprintf( __('<span class="saved-sale"> Save: %s <em>(%s)</em></span>', 'woocommerce' ), $saving_price, $saving_percentage );
}
return $price;
}
add_filter('woocommerce_get_price_html','display_sale_price_和_percentage_html',10,2);
函数显示\销售\价格\和\百分比\ html($price,$product){
//前端销售产品,不包括可变产品
如果($product->is_on_sale()&&!is_admin()&&!$product->is_type('variable')){
//产品价格
$regular_price=(float)$product->get_regular_price();//常规价格
$sale_price=(float)$product->get_price();
//价格计算和格式
$saving_price=wc_price($regular_price-$sale_price);
//百分比计算和格式
$precision=1;//小数
$saving_percentage=round(100-($sale_price/$regular_price*100),1)。“%”;
//使用span标记显示格式化的html价格,包括金额和进度,这意味着将其显示在同一行上,如果希望在新行上显示,请将标记更改为段落
$price.=sprintf($saving\u('Save:%s(%s)'woocommerce'),$saving\u price,$saving\u percentage);
}
返回$price;
}
错误显示在分组产品上。它在简单的产品上运行良好。我的目标是使其适用于所有产品类型(简单、分组、外部和可变)
我需要所有我能得到的帮助。我相信这是因为分组产品没有正常的价格。您应该添加一些条件来检查
$sale\u price
和$regular\u price
是否为非零。您还可以检查您是否不在分组产品上,但检查0也可以防止在任何有免费产品的地方出现被零除的错误
add_filter( 'woocommerce_get_price_html', 'display_sale_price_and_percentage_html', 10, 2 );
function display_sale_price_and_percentage_html( $price, $product ) {
// sale products on frontend excluding variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')) {
// product prices
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_price();
if( $regular_price > 0 && $sale_price > 0 ) {
// price calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// percentage calculation and formatting
$precision = 1; // decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// display the formatted html price including amount and precentage using a span tag which means displaying it on the same row, if you want this on a new row, change the tag into a paragraph
$price .= sprintf( __('<span class="saved-sale"> Save: %s <em>(%s)</em></span>', 'your-textdomain' ), $saving_price, $saving_percentage );
}
}
return $price;
}
add_filter('woocommerce_get_price_html','display_sale_price_和_percentage_html',10,2);
函数显示\销售\价格\和\百分比\ html($price,$product){
//前端销售产品,不包括可变产品
如果($product->is_on_sale()&&!is_admin()&&!$product->is_type('variable')){
//产品价格
$regular_price=(float)$product->get_regular_price();
$sale_price=(float)$product->get_price();
如果($正常价格>0&$销售价格>0){
//价格计算和格式
$saving_price=wc_price($regular_price-$sale_price);
//百分比计算和格式
$precision=1;//小数
$saving_percentage=round(100-($sale_price/$regular_price*100),1)。“%”;
//使用span标记显示格式化的html价格,包括金额和进度,这意味着将其显示在同一行上,如果希望在新行上显示,请将标记更改为段落
$price.=sprintf($saving\u('Save:%s(%s)'your textdomain'),$saving\u price,$saving\u percentage);
}
}
返回$price;
}
不,有时候你只需要用新鲜的眼睛来看看。很高兴它起作用了。