Jigoshop – body class dependent on cart state

In one of recent projects, we needed to add an extra styling if the shopping cart contained any products. Below is the snippet, that add extra class on body: “cart-has-items” if there is nothing in the cart and “cart-has-no-items” if there are products in the cart. We use standard filter:”body_class“. The snipped can be added to your theme’s functions.php file


[php]
//extra class when cart has items
add_filter('body_class','cartCheck');

function cartCheck($classes = '') {
if(!empty(jigoshop_cart::$cart_contents_count)){
$classes[] = 'cart-has-items';
}
else {
$classes[] = 'cart-has-no-items';
}
return $classes;
}
[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *