[英]How can i fix Warning: Division by zero in OpenCart
我為商品添加了以下代碼,用於百分比折扣徽章(它將自動計算新舊價格之間的百分比。)。 因此,它工作正常。
但是,當在產品中添加0.00價格。 所以。 得到錯誤。 警告:在/ home / ......中被零除。
因此,針對此錯誤的任何解決方案。 我該如何解決?
下面的代碼添加到.php文件中
'saving' => round((($product_info['price'] - $product_info['special'])/$product_info['price'])*100, 0)
下面的代碼添加到.tpl文件
<span class="saving">-<?php echo $product['saving']; ?>%</span>
謝謝。
如消息所解釋:不要除以零。 您的價格為零。
選項1:編寫函數
$array = [
'foo' => 'bar',
'donald' => 'duck',
'saving' => GetDiscount($product_info['price'], $product_info['special'])
]
//Get the Discount. If the Price is zero, Discount is 100 %
function GetDiscount($price, $special) {
return $price === 0 ? 100 : round((($price - $special)/$price)*100, 0);
}
選項2:一行
$array = [
'foo' => 'bar',
'donald' => 'duck',
'saving' => $product_info['price'] === 0 ? 100 : round((($product_info['price'] - $product_info['special'])/$product_info['price'])*100, 0)
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.