woocommerce 闪购 - 警告除以零
woocommerce flash sale - warning division by zero
自更新 woocommerce 以来,我收到警告:使用以下函数除以零错误。
我不明白为什么以前没有出现这个错误,目前即使关闭 wp debug 仍然显示错误。
function 6516_product_sale_flash( $output, $post, $product ) {
global $product;
if($product->is_on_sale()) {
if($product->is_type( 'variable' ) )
{
$regular_price = $product->get_variation_regular_price();
$sale_price = $product->get_variation_price();
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
$percent_off = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
return '<span class="onsale">' . 'Save<br>' . round($percent_off) . '%</span>';
}
}
关于为什么现在这是一个问题有什么想法吗?
谢谢
设法解决了这个问题。
使用未打折的复合产品时,正常价格为零。所以我用解决了问题的 if 语句包装了出错的行;
function 6516_product_sale_flash( $output, $post, $product ) {
global $product;
if($product->is_on_sale()) {
if($product->is_type( 'variable' ) )
{
$regular_price = $product->get_variation_regular_price();
$sale_price = $product->get_variation_price();
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
if ($regular_price > 0) {
$percent_off = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
return '<span class="onsale">' . 'Save<br>' . round($percent_off) . '%</span>';
}
else {
return null;
}
}
}
自更新 woocommerce 以来,我收到警告:使用以下函数除以零错误。
我不明白为什么以前没有出现这个错误,目前即使关闭 wp debug 仍然显示错误。
function 6516_product_sale_flash( $output, $post, $product ) {
global $product;
if($product->is_on_sale()) {
if($product->is_type( 'variable' ) )
{
$regular_price = $product->get_variation_regular_price();
$sale_price = $product->get_variation_price();
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
$percent_off = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
return '<span class="onsale">' . 'Save<br>' . round($percent_off) . '%</span>';
}
}
关于为什么现在这是一个问题有什么想法吗?
谢谢
设法解决了这个问题。
使用未打折的复合产品时,正常价格为零。所以我用解决了问题的 if 语句包装了出错的行;
function 6516_product_sale_flash( $output, $post, $product ) {
global $product;
if($product->is_on_sale()) {
if($product->is_type( 'variable' ) )
{
$regular_price = $product->get_variation_regular_price();
$sale_price = $product->get_variation_price();
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
if ($regular_price > 0) {
$percent_off = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
return '<span class="onsale">' . 'Save<br>' . round($percent_off) . '%</span>';
}
else {
return null;
}
}
}