Uncaught DivisionByZeroError: Division by zero in /home/site/webapps/site/web/app/themes/bricks/includes/woocommerce.php:756
I believe the problem is that some products have weird prices or discounts. But this isn’t a reason to throw such an error, and it should be handled with some defaults or ignored.
Error Details:
Type:DivisionByZeroError
Message: Division by zero
Location:woocommerce.php in the theme’s WooCommerce integration module
I’m not really sure what my client did there. Maybe they had a product with price 0? Not sure. I didn’t spent the time to look for the specific product with the problem.
I tried to reproduce this again, but it didn’t show up. So maybe they updated the product because I told them that this error was caused by a 0 regular price somewhere. So I’m not sure.
Unfortunately, I can’t reproduce it either with the regular or the sale price. So, I don’t see any need for action at the moment. Please get in touch if you know how to reproduce the problem, and we’ll be happy to take a look at it
Looking at the page structure in inspect I can see that the SALE badge for the previous item showed up, while the next one crashed. The images are both Bundle items, both with a discounted price, and both had stock values and prices correctly set. So it doesn’t have anything to do with the price division by 0 as I previously thought. Or maybe the bundle has a price of 0? let me check…
Also, why do the badges show differently? Some have “SALE” text and some have actual percentage reduction? It seems to crash only for percentage badges because only then it tries to calculate the price.
Hi Cristian,
How did you add the products? Probably imported, right? Normally it doesn’t seem to be possible to specify only a sale price. If I leave out the regular price, the sale price field empties automatically (independent of Bricks).
Have you tried it temporarily with a default theme? You should get the same problem.
Presumably because they are different item types. Simple products show the percentage discount because the price is explicit. SALE is displayed for variable or grouped products.
Also, I confirm I can save the page without a regular price since this is a bundled product. However, I am not the one who adds these products; the owner of the site does. I told them to be careful and add a regular price, but it seems they often miss it.
Can you please provide a temp admin access to your website so I can test with a potential fix?
Please email to help@bricksbuilder.io and include this forum thread as a reference.
After more testing it should not be possible to set a sales price without a regular price, however you can have products with no regular price for bundles as the price is calculated from the bundled products themselves.
I am not sure how the sales price got filled up in the bundled products as it was not put there manually. It is impossible as the validation would fail at save. So that could only be put there by Woocommerce itself most likely in error or by some “cache” mechanism maybe? Not sure.
I noticed even new bundles after a few days would get a sales price set.
For now I fixed the products by forcefully removing the prices from the DB and allowing the bundles to get calculated automatically.
Product bundles saves the price in a custom meta “_wc_pb_base_sale_price” not in the “_sales_price” meta as normal products are. There is also “_wc_pb_base_regular_price” and “_wc_pb_base_price”. So these meta fields are the ones that are suddenly filled by some mysterious code or cache maybe. Not sure.
The fix for now was to remove all those sales prices from the DB using a list of bundled products and WP CLI. For reference here are the commands I used:
wp db query "SELECT DISTINCT bundle_id FROM wp_woocommerce_bundled_items WHERE bundle_id IS NOT NULL;" --skip-column-names > bundle_ids.txt
bundle_ids=$(cat bundle_ids.txt | tr '\n' ',' | sed 's/,$//')
echo $bundle_ids > formatted_bundle_ids.txt
product_ids=$(wp db query "SELECT DISTINCT ID FROM wp_posts WHERE ID IN ($(cat formatted_bundle_ids.txt)) AND post_type = 'product';" --skip-column-names)
echo $product_ids | tr '\n' ' ' > product_ids.txt
for id in $(cat product_ids.txt); do
wp post meta delete $id _wc_pb_base_sale_price
done
Thanks for the detailed info.
What we can do is just skip the calculation of the percentage discount if the regular price is zero.
Can you provide admin access for me to apply a potential fix on your site? (without running the WP CLI)
It looks like the prices keep coming back for some reason - I removed all of them yesterday and today I see 1 product has crashed again due to the sales price being set and the regular one not. I am not sure what is causing this, most likely some code in Woo or Bundle plugin itself.
I will e-mail support shortly with a dev clone of the website so you can test.