Opens in a new tab
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 633 Bricks code tutorials with BricksLabs Pro

2 comments

Leave your comment

 

Related Tutorials..

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:
Pro
Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

On product pages, we show all the FAQs associated with the current WooCommerce product's product categories.
Categories:
Pro
WooCommerce Products Live Search in Bricks

WooCommerce Products Live Search in Bricks

This Pro tutorial provides the steps to set up a live search for WooCommerce products in Bricks. Note: Bricks' live search works with any type…
Categories:
Pro
[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

Setting up a sticky section that fades in when scrolling down and fades away when scrolled to the top.
Categories:
Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Creating a condition to ensure that a section only gets output if at least one row of data is filled in.
Categories:
Pro
Conditional CSS Classes based on Bricks Query Count

Conditional CSS Classes based on Bricks Query Count

In a Bricks project I am currently working on, there are nested query loops - team members inside departments on a Page. Each team member…