Is there any way to hide the cart block when the cart is empty?

There are a few unanswered mentions of this across the web, including http://www.drupalcommerce.org/node/623 but I've been unable to find answer.

Any help on this, if possible, would be much appreciated.

CommentFileSizeAuthor
#15 disappearing_empty_cart.zip1.41 KBtomrenner

Comments

rszrama’s picture

Status: Active » Fixed

There are no settings for this as is, though it may technically be possible through page altering. It's not something I've ever attempted, though. When we convert this block to be entirely a View, that should take care of it, since I believe you can hide a View display if empty.

darksnow’s picture

Thanks Ryan, I tried to put some PHP on a criteria to show the page, but couldn't work out how to get it to work.

It's not serious and I can live with the shopping cart being there for this project, though I just think it's a little untidy.

Again, thanks for the quick reply, I'll keep an eye on it.

Martin...

rszrama’s picture

Cool, I'll see what we can do about converting those to Views entirely for 1.1. It's not that much work. The only reason I didn't do it earlier was I wasn't aware that Views 3.x was still supporting translatable interface text. Apparently it's just an option you have to choose before exporting the View. The only reason that might not go through would be theme template-ability.

Oh! But I just realized... you don't actually have to use the core cart block at all. You can still use the same View and just add a Block display to it. Disable the core block and voila! Hide when empty will probably work just fine.

darksnow’s picture

Cheers for that Ryan, that worked a treat.

I messed about with the views setup, got myself a Cart block and when it's empty, it doesn't appear.

Problem solved.

rszrama’s picture

Excellent! Glad to hear it.

darksnow’s picture

It should be noted that my testing through up a potential problem here.

You MUST make sure you add Order Status = Shopping Cart to the view filters, or any given user will see everything they've ever ordered in their cart and the remove from cart buttons won't work for those old line items.

skaught’s picture

use a hook, of course:

function THEMENAME_commerce_cart_empty_block() {
  return '<div class="cart-empty-block"></div>';
}

presto, all gone.

urban farmer’s picture

Skaught, this may sound like a stupid question, but I am new to drupal. Where do I place the hook?

koushik12345’s picture

This is not correct. Please use this page (commerce/module/cart/commerce_cart.module)

/**
* Themes an empty shopping cart block's contents.
*/
function theme_commerce_cart_empty_block() {
return '

' . t('Your shopping cart is empty.') . '

';
}

/**
* Themes an empty shopping cart page.
*/
function theme_commerce_cart_empty_page() {
return '

' . t('Your shopping cart is empty.') . '

';
}

darksnow’s picture

@urban farmer

The hook functions go in template.php for your theme, with the function name changed to match your theme name.

If you are a relative Drupal newbie though, then creating a view to display the contents of the cart might be easier for you since views automatically don't display if they get no results (unless you give them an empty text).

skaught’s picture

sorry, a hasty post. In templete.php is correct. i corrected my first code sample.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

pitxels’s picture

I used the hook, but I returned nothing:

return '';

Otherwise the Title of the block is still there, and the region of that block keep occupying space.

stieglitz’s picture

opps

tomrenner’s picture

Title: Hide Cart block when cart is empty » Small Module
StatusFileSize
new1.41 KB

I have created a small module that basically does nothing but add another cart block in the blocks section. The "Disappearing Shopping cart" will return NULL if there is no active order on the logged-in user, thus also no headline for the block.

tomrenner’s picture

Title: Small Module » Hide empty cart block

Just found out: the module needs a slight adjustment for anonymous user:

// Default to an empty cart block message.
$content = theme('commerce_cart_empty_block');

needs to be changed to

// Default to an empty cart block message.
$content = NULL;
dave bruns’s picture

Just chiming in with a few notes on hiding the cart when empty, by cloning the original block display in the view...

When I first did cloned the display, I got some weird memory problems and a very sluggish views display. Then, when I saved the newly cloned block and added the block to a region, I got a memory problem that prevented any page on the site (that would normally contain the shopping cart) from displaying.

Turns out that I needed to adjust the contextual filter to supply a default value of the "current user's cart order ID". This is (apparently, see http://bit.ly/JwXoUl) supplied to the default block through a variable. Without this default, my new block was trying to display all line items from every order on the site. Since this is an Ubercart to Commerce migration site, this was a big problem because there are a lot of previous orders. # 6 above mentions using "order status" as a filter, but I don't think this is needed as long as as the contextual filter is set up properly.

Once the filter was working the new block worked great (i.e. disappeared when empty) and I disabled the original block.

philippejadin’s picture

Using solution #7 and returning nothing as explained in #13 works just fine.

gratefulsk’s picture

  1. Find the default "Shopping cart block" view
  2. Create a new block display
  3. Edit the"Commerce Order:Order ID" contextual filter
  4. Set it to Provide default value > Type = Current user's cart order ID
  5. Then use that new block you have just created in your site. It will not show if the cart is empty
donok’s picture

#19 - thank you that was easy

fyi also added

cart
checkout/*

to the Pages tab in the block's configuration to keep it from showing on those pages

Thanks!

rowbotony’s picture

Thanks #19 - super simple, works great!!

benjas’s picture

Thanks #19 - worked for me as well. Remember to create a new block.

kurtfoster’s picture

#19 is a great solution, the only thing I do differently is I create a new view rather than a new block in the same view as I move it around via features and I find it easier as a separate item.

Shyghar’s picture

I used the hook suggested in comment #7 and it work perfectly!
I simply return false to not display the empty cart block.

function MODULENAME_commerce_cart_empty_block() {
  return FALSE;
}

Thank you SKAUGHT.

Thanks to tomrenner for #9 for theme_commerce_cart_empty_page() hook ^^

hanskuiters’s picture

Issue summary: View changes

A bit old topic, but still relevant.

#19 doesn't work for me. For some reasen I can't get the view display to duplicate. Altering the original display with the suggested changes doesn't work eather. Might be something to do with a newer verison of Commerce?

#7 works fine, I use it with the addiction of #13/#24.

Thanks for sharing.

EDIT:
I didn't read #19 very well. I indead needed to create a NEW display because the view only contains the master display. #19 works fine too.

argiepiano’s picture

#19 works, but remember to also include the filter in #6. Thanks