I'm trying to work with a template on a bookstore site (M2.3.6). I added a new configurable product with 2 configurations and when I did, I got this error:

Error filtering template: Warning: Division by zero in /home2/crippenl/public_html/magento/app/design/frontend/Sm/amabook/Sm_RecentlyViewed/templates/slider-v2.phtml on line 101

The code is:

getProductPrice($_product) ?> getPrice(); $finalPrice = $_product->getFinalPrice(); $save_percent = 100 - round(($finalPrice / $defaultPrice) * 100); if ($finalPrice

Any thoughts on this? I've been working on this for hours with no luck. Thank you.

  • Can you check the variable $defaultPrice has some value ? As well as you could either use $block->getProductPrice($_product) or $block->getPrice() in your template file.
    – Haijerome
    Feb 4, 2021 at 21:42

1 Answer 1


Try this

/* returns your product price without tax and before all the rules in your store currency without formatting */
$defaultPrice = $_product->getPrice(); 
$finalPrice = $_product->getFinalPrice(); // returns your product final price 

$save_percent = 100 - round(($finalPrice / $defaultPrice) * 100);

BTW with PHP 7 you can also handle these exceptions

try {
  $save_percent = 100 - round(($finalPrice / $defaultPrice) * 100);
} catch (\DivisionByZeroError $exception) {
} catch (ErrorException $error) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.