WAIT: Division by Zero Error in WooCommerce Sale Badge Functionality

In some edge cases I get this error:

Fatal error :

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
  • Line: 756

Full Stacktrace

Fatal error: Uncaught DivisionByZeroError: Division by zero in /home/site/webapps/site/web/app/themes/bricks/includes/woocommerce.php:756 Stack trace: #0 /home/site/webapps/site/web/wp/wp-includes/class-wp-hook.php(324): Bricks\Woocommerce->badge_sale() #1 /home/site/webapps/site/web/wp/wp-includes/plugin.php(205): WP_Hook->apply_filters() #2 /home/site/webapps/site/web/app/plugins/woocommerce/templates/loop/sale-flash.php(27): apply_filters() #3 /home/site/webapps/site/web/app/plugins/woocommerce/includes/wc-core-functions.php(345): include('...') #4 /home/site/webapps/site/web/app/themes/bricks/includes/woocommerce/elements/woocommerce-products.php(364): wc_get_template() #5 /home/site/webapps/site/web/app/themes/bricks/includes/woocommerce/elements/woocommerce-products.php(321): Bricks\Woocommerce_Products->render_fields() #6 /home/site/webapps/site/web/app/themes/bricks/includes/elements/base.php(2292): Bricks\Woocommerce_Products->render() #7 /home/site/webapps/site/web/app/themes/bricks/includes/frontend.php(519): Bricks\Element->init() #8 /home/site/webapps/site/web/app/themes/bricks/includes/elements/container.php(938): Bricks\Frontend::render_element() #9 /home/site/webapps/site/web/app/themes/bricks/includes/elements/base.php(2292): Bricks\Element_Container->render() #10 /home/site/webapps/site/web/app/themes/bricks/includes/frontend.php(519): Bricks\Element->init() #11 /home/site/webapps/site/web/app/themes/bricks/includes/elements/container.php(938): Bricks\Frontend::render_element() #12 /home/site/webapps/site/web/app/themes/bricks/includes/elements/base.php(2292): Bricks\Element_Container->render() #13 /home/site/webapps/site/web/app/themes/bricks/includes/frontend.php(519): Bricks\Element->init() #14 /home/site/webapps/site/web/app/themes/bricks/includes/elements/container.php(938): Bricks\Frontend::render_element() #15 /home/site/webapps/site/web/app/themes/bricks/includes/elements/base.php(2292): Bricks\Element_Container->render() #16 /home/site/webapps/site/web/app/themes/bricks/includes/frontend.php(519): Bricks\Element->init() #17 /home/site/webapps/site/web/app/themes/bricks/includes/frontend.php(618): Bricks\Frontend::render_element() #18 /home/site/webapps/site/web/app/themes/bricks/includes/frontend.php(868): Bricks\Frontend::render_data() #19 /home/site/webapps/site/web/app/themes/bricks/page.php(7): Bricks\Frontend::render_content() #20 /home/site/webapps/site/web/wp/wp-includes/template-loader.php(106): include('...') #21 /home/site/webapps/site/web/wp/wp-blog-header.php(19): require_once('...') #22 /home/site/webapps/site/web/index.php(6): require('...') #23 {main} thrown in
/home/site/webapps/site/web/app/themes/bricks/includes/woocommerce.php
on line
756
1 Like

The fix for me was to check for the $regular_price var, not just for $sale_price.

So instead of this part:

			if ( $sale_price != 0 || ! empty( $sale_price ) ) {
				$percentage = round( 100 - ( $sale_price / $regular_price * 100 ) ) . '%';
			} else {
				return $html;
			}

I replaced inside the if like this:

			if ( $sale_price != 0 && !empty($sale_price) && $regular_price != 0 ) {
				$percentage = round( 100 - ( $sale_price / $regular_price * 100 ) ) . '%';
			} else {
				return $html;
			}
		}
1 Like

Hi Cristian,
Thanks so much for your report!

Can you tell me the edge cases/weird prices/discounts so that I can reproduce the problem?

Best regards,
timmse

Hi Timmse,

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 :v:

1 Like

Hello @timmse ,

I have reencountered this error today, running the latest Bricks Builder version 1.9.7.1.

Please advise on what I should do next to help you fix this issue.

Stacktrace:

Fatal error: Uncaught Error: Division by zero
in /home/eldarom-ro/webapps/eldarom-ro/web/app/themes/bricks/includes/woocommerce.php on line 753

Call stack:

Bricks\Woocommerce::badge_sale()
wp-includes/class-wp-hook.php:324
WP_Hook::apply_filters()
wp-includes/plugin.php:205
apply_filters()
app/plugins/woocommerce/templates/loop/sale-flash.php:27
include()
app/plugins/woocommerce/includes/wc-core-functions.php:345
wc_get_template()
app/themes/bricks/includes/woocommerce/elements/woocommerce-products.php:364
Bricks\Woocommerce_Products::render_fields()
app/themes/bricks/includes/woocommerce/elements/woocommerce-products.php:321
Bricks\Woocommerce_Products::render()
app/themes/bricks/includes/elements/base.php:2298
Bricks\Element::init()
app/themes/bricks/includes/ajax.php:371
Bricks\Ajax::render_element()
app/themes/bricks/includes/builder.php:2230
Bricks\Builder::query_content_type_for_elements_html()
app/themes/bricks/includes/builder.php:2134
Bricks\Builder::builder_data()
app/themes/bricks/includes/builder.php:287
Bricks\Builder::enqueue_scripts()
wp-includes/class-wp-hook.php:324
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:348
WP_Hook::do_action()
wp-includes/plugin.php:517
do_action()
wp-includes/script-loader.php:2262
wp_enqueue_scripts()
wp-includes/class-wp-hook.php:324
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:348
WP_Hook::do_action()
wp-includes/plugin.php:517
do_action()
wp-includes/general-template.php:3052
wp_head()
app/themes/bricks/header.php:7
require_once()
wp-includes/template.php:790
load_template()
wp-includes/template.php:725
locate_template()
wp-includes/general-template.php:48
get_header()
app/themes/bricks/template-parts/builder.php:2
include()
wp-includes/template-loader.php:106
require_once()
wp-blog-header.php:19
require()
index.php:6

Screenshot:

EDIT

Adding more context after a bit of debugging.

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…

EDIT 2

Yes. The bundle had no normal price set. Just the sale price. Hence the “division by 0” error.

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.


ow

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.

Hello @timmse,

Sorry for the delay in my response.

Recently, I activated a cache preload feature on this site, and it seems to find many broken pages we were not aware of.

For example:
https://eldarom.ro/producatori/jbl

This page will crashes at this product:

Looks like there are a lot of products like this, without a regular price.

The page looked like this before adding a regular price. Once a regular price is added the problem is fixed.

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. :face_exhaling:

It is using the official bundle product plugin: https://woocommerce.com/products/product-bundles/

Hey!

Any news on this issue?

We still encounter it from time to time. A permanent fix is appreciated.