0

WooCommerce enables automatically .logged-in class when a user is logged in. This allows to create a page with different content without any PHP coding.

Is there something similar when the shopping cart is empty? I am NOT seeking any PHP solution. It should be solved only with a CSS class so I can hide/show parts of a page depending on the fact this CSS class does/does not exist.

1 Answer 1

1

You'll need php for that... try this in your functions.php:

Detect if cart is empty and add class to body if is:

add_filter( 'body_class','my_custom_class' );
function my_custom_class( $classes ) {
    if ( WC()->cart->get_cart_contents_count() == 0 ) {
        $classes[] = 'my-emptycart-class';
    }

    return $classes;

}

Now you can use .my-emptycart-class in your CSS.

3
  • If my solution doesn't work: please, put a look here: stackoverflow.com/a/45118800/12282455
    – Sam
    Commented Nov 22, 2019 at 15:15
  • Your solution doesn't work. It looks like the if condition where you check if the SC is empty cannot be executed there. An exception is thrown when I tried to save functions.php. I had to use the one you offered me the link. Thx for the link! Commented Nov 23, 2019 at 15:47
  • Okay, I was not sure I could calculate the number of products outside like that. I updated my code so that you could mark that my answer helped you.
    – Sam
    Commented Nov 23, 2019 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.