20th Mar '23
/
2 comments

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024

Looking to conditionally render an element depending on whether the user’s cart is empty or not when using WooCommerce?

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

// Function to check if WooCommerce cart is empty.
function bl_is_cart_empty() {
	if ( function_exists( 'WC' ) ) {
		return WC()->cart->is_empty();
	}

	return true;
}

Then whitelist it:

add_filter( 'bricks/code/echo_function_names', function() {
    return [
        'bl_is_cart_empty',
    ];
} );

and use it with a Dynamic data condition of Bricks like this:

{echo:bl_is_cart_empty}

The above will output the element on which that condition has been applied if the cart is empty.

To output the element if the cart is NOT empty, set the value to check against to 0.

Remember that 1 is true and 0 is false.

Note

This result of the condition (or rather, the custom function that it uses) won’t reflect on the front end immediately after all the products have been removed from the cart due to AJAX. But it will work after the user navigates to another page or reloads the page.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

2 comments

Leave a Reply to Sridhar Katakam (Cancel Reply)

 

Related Tutorials..

Pro
Showing a Popup conditionally in Bricks based on Query Parameter

Showing a Popup conditionally in Bricks based on Query Parameter

Here's two ways a Bricks Popup can be shown on page load depending on URL parameter value.
Categories:
Tags:
Pro
WooCommerce Product Carousel in Bricks

WooCommerce Product Carousel in Bricks

How to set up a carousel that shows WooCommerce products with the product title, price and Add to Cart button for each.
Categories:
Side-by-side WooCommerce Quantity and Add to Cart in Bricks

Side-by-side WooCommerce Quantity and Add to Cart in Bricks

Looking to position the quantity input field and Add to cart button next to each other instead of one below the other in Bricks? Add…
Categories:
Check if a given category has at least one post

Check if a given category has at least one post

There could be situations where you want to check if a given category (by its slug) has at least 1 post assigned to that category.…
Categories:
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Pro
Condition for checking Bricks content

Condition for checking Bricks content

Only outputting Bricks content when a Page is built with Bricks, otherwise display WP content.
Categories:
JetEngine Checkbox Condition in Bricks

JetEngine Checkbox Condition in Bricks

How elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox field.
Categories:
Tags: