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

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.

  • If my solution doesn't work: please, put a look here: stackoverflow.com/a/45118800/12282455 – Sam Nov 22 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! – Al Bundy Nov 23 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 Nov 23 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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