Is WooCommerce Cart Empty Condition in Bricks

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;
}

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.

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

2 comments

Leave your comment