php - woocommerce 闪购 - 警告除以零
问题描述
由于更新 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;
}
}
}
推荐阅读
- python - 一起工作时间最长的一对员工——Python/Pandas
- reactjs - 如何使用 Material-UI 中的 useTheme 钩子?
- symfony4 - 图像字段在编辑表单提交时为空
- racket - 使用替换模型的 eval 函数中的 DrRacket 类型不匹配错误
- c# - C# ASP 目录存在时为真
- python - 在 lambda 表达式中应用多个正则表达式模式
- ios - SwiftUI 视图,如 UIKit UIView,如何更改图像的大小
- java - 有没有人用 Java 为 ADFS 创建了声明信任提供程序?
- r - 列表被添加到数据框
- android - ImageView 不以编程方式显示图像集