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 child-theme or custom theme.
If you do not have a child-theme, or a custom theme, you could use a plugin such as Code Snippets.
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

