How can I set a Drupal Commerce shopping cart block to only display if it contains items? I'd like to hide the cart block when the cart is empty.

  • Please explain your answer so that someone in the community can understand.
    – niksmac
    Commented Nov 25, 2012 at 14:30

3 Answers 3


Here is the solution. Add these lines in template.php file of a theme you`re using. Change THEMENAME on the name of your theme.

function THEMENAME_commerce_cart_empty_block() {
  return '';

For drupal8+ and commerce 2 the answer is

function mytheme_preprocess_block(&$variables) {
  if ($variables['plugin_id'] == 'commerce_cart') {
    if ($variables['content']['#count'] == 0) {
      $variables['attributes']['class'][] = 'visually-hidden';

The solution that worked for me is not just hiding this "your shopping cart is empty" but also hiding all possible long list of items. And you can accomplish this using views:

  • create two fields of text links, one for view cart and another for checkout.
  • Add another field to display the total amount. Look for "add commerce order: order total" I got this idea from this
  • Filter criteria: commerce order to >=1. This will make block disappear when the order is greater than 1.
  • Apply some css.

drupal commerce: create block to display total order with links

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.