You are here:  » Percentage Discount - Zero division


Percentage Discount - Zero division

Submitted by ChrisNBC on Fri, 2015-01-30 09:29 in

Hi David,

Hope all is going well.

I noticed yesterday that the site I'm working on no longer displays percentage discounts. On further investigation, I noticed that the discount field in my dB is no longer being populated. I checked admin.php and could see the code was missing so I added it back in. Since doing this, when I run either slow or normal imports, a list of errors like the one below are displayed. I can see mention of the zero division isue in node 4470, but have been unable to resolve the issue. I wondered if you might be able to suggest how I might resolve it?

The code I have in place is:

  /* decimalise price */
  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);
  $importRecord["normalprice"] = tapestry_decimalise($importRecord["normalprice"]);
  $importRecord["deliverycost"] = tapestry_decimalise($importRecord["deliverycost"]);
  /* $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]); temp stop replaced by 5 lines below*/
  /*calculate percentage discount and populate discount field */
  if ($importRecord["normalprice"] != $importRecord["price"])
  {
    $importRecord["discount"] = (($importRecord["normalprice"]-($importRecord["price"])) / ($importRecord["normalprice"])*100);
    $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]);
  }

The error displayed when I import feeds is:

Warning: Division by zero in /home/content/43/10000000/html/Sites/mysite/includes/admin.php on line 342

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2015-01-30 11:33

Hi Chris,

The division by zero error would occur whenever normal price is 0.00, so that should be tested for at the same time as checking that it is not equal to the price - have a go with:

  /* decimalise price */
  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);
  $importRecord["normalprice"] = tapestry_decimalise($importRecord["normalprice"]);
  $importRecord["deliverycost"] = tapestry_decimalise($importRecord["deliverycost"]);
  /* $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]); temp stop replaced by 5 lines below*/
  /*calculate percentage discount and populate discount field */
  if (($importRecord["normalprice"] != "0.00") && ($importRecord["normalprice"] != $importRecord["price"]))
  {
    $importRecord["discount"] = (($importRecord["normalprice"]-($importRecord["price"])) / ($importRecord["normalprice"])*100);
    $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]);
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2015-01-30 15:12

Thanks David, the above has sorted the issue.

Regards
Chris