Add class to body if Woocommerce cart has items
WordPress
A very simple small snippet of code that will add a class to your site <body> tag, if the Woocommerce shopping cart has one or more items.
// Add class to BODY if cart has items
function mytheme_cart_has_items( $classes ) {
if ( WC()->cart->get_cart_contents_count() >= 1 ) {
$classes[] = 'itemsInCart'; // ‘itemsInCart’ is the class that is added to your site <body> tag
}
return $classes;
}
add_filter( 'body_class', 'mytheme_cart_has_items' );
Add this to your functions.php file, within your theme or child-theme.
Replace ‘mytheme’ with the something to identify your own theme.
‘itemsInCart’ is the class that is added to your site, change this if you wish.
View the Gist: https://gist.github.com/macgraphic/69976d6c79ca4e616af235b8c12bcb49
Photo by: Jomjakkapat Parrueng on Unsplash