Hiding Empty Shopping Carts in Drupal 7 Commerce

Wed, 02/22/2012 - 06:55 -- James Sinkiewicz

Shopping Cart EmptyI am just starting to get familiar with Drupal 7's Commerce modules. The first thing I noticed after enabling the standard Commerce modules was that doing so created a Shopping Cart Block that could be added to any Region. However, placing the default shopping cart block into a region caused the block to be shown at all times, even if it was empty. While some may define this always visibable (and often empty) shopping cart as a subconscious "call to action", I find it simply annoying.

I confess that in several day dreaming sessions, Acquia has puts me up for a few weeks to teach me how to code Drupal to accomplish my requirements. But in the real world, I have to look for non-coding ways to accomplish what I want by using modules and methods already provided by people who are a lot more knowledgeable about such things. In researching this issue/desire, I came across a threaded discussion in which Ryan Szrama, the author of Drupal Commerce, wrote "...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." Several posters after which continued to discuss coding fixes to hide empty shopping carts.

Crickets sounds.....

So off I went to check out the Views that were created by enabling the Commerce module. I navigated over to "admin/structure/views" and found this possibility - "Shopping cart block" View. 

Commerce shopping cart block view

Now I'm not one to go around messing with just anybody's View, especially Ryan Szrama's, so I immediately cloned the View and gave it a new name. I gave this cloned View the remarkably unique name of "Clone of Shopping cart block". I then proceeded to add a block to the cloned View to display on my pages as he suggested. The first thing I needed to do of course was to add a block to the cloned View to use instead of the default "Master" View. This seemed simple enough; I just clicked on the "Add" link and selected "Block". The word "Master" changed to "Block" and I was in business! Saving my Block, I quickly visited the "admin/structure/block" page to place my newly created, Ryan Szrama approved, block into my page region. I added it to the same region as the default Commerce module Shopping Cart block so I could compare and contrast the two. I was happy that it worked, but not so happy with the result. It seemed the cloned block was listing everything that was ever purchased before, not what was in the current (and quite empty) cart.Orignal Shopping Cart block above Cloned Block which shows all previous orders.

So off the to do some more searching at drupal.org. My wanderings through the Commerce module issue queue  led me to discover that the contextual filter Order ID used by the original Shopping Cart block View was really a variable (order_id) being set programmatically by the Cart module (commerce_cart.module) when the View was embedded with the "commerce_cart_block_view" function. I know what you are thinking, but these people who know how to write Drupal code like to do stuff like that. The Contextual filter called "Commerce Order: Order ID" was set to "Display all results for the specified field". I discovered that I needed to change this to "Provide default value" and set that default value to "Current user's cart order ID" and save my changes.

After refreshing my home page, I was back to where I started! I could see one block informing me that my shopping cart was empty. But then I remembered that I still had the original Commerce module Shopping Cart block set to display in the region and what I was seeing was that block. Since the new "Cloned" block did not contain any line items, it was not being displayed. To check this I simply added something to my shopping cart. 

Original shopping cart block above cloned block with a line item

The results were exactly what I expected. Two duplicate shopping carts; one being the original Commerce shopping cart, and the other my cloned View with the block added. All that was left to do was remove the Drupal Commerce default shopping cart block from the region so that only my new cloned View Shopping Cart block was the only Shopping Cart block set to display in the region. Since Views automatically took care of showing or hiding the block depending on whether the shopping cart contained any items, I was off the "hook" so to speak in needing to take care of that myself.

So there you have it. If I remove the Line Item from my cart, the block is hidden. When I add an item to the cart, the shopping cart block is displayed. I'm sure this is just one non-coding method of removing the Shopping Cart block for empty carts. I hope those that have other solutions will add comments and instructions. I am always looking for better ways to accomplish my goals!

Note: At the time of this article, I had installed Drupal 7.12 and Commerce 7.x-1.2. Ryan Szrama has indicated his intention to convert the Shopping Cart block to completely be a standard View (removing the need for this solution) as soon as he determines the best way to handle the upgrade path. Look for that change in future releases.

PS. This is my first article utilizing the Colorbox module and plugin. I like it!




Submitted by travelertt on

Small side criticism. Please reduce the width of your articles, it makes it very hard to read. Something like 40-55 CPL (characters per line) is optimal, but you can also get away with 100 CPL if the text is easy to read. That's a paragraph width of about 500px-600px.

Thanks and keep them coming!

Submitted by James Sinkiewicz on

That should make it a bit more readable for you and others. Thanks!

Submitted by Ryan on

Excellent article! Kudos to you for sticking with it to figure out the solution from the documentation available. It makes me happy. : )

The issue to watch for the fully Views-based cart block / page displays is http://drupal.org/node/1030128.

Submitted by James Sinkiewicz on

Thanks for the link Ryan and for a group of terrific modules!

Submitted by JongNintrja on

I like what I see so ntrow i am folloywing you. Look forward to going over your web page repeatedly.

Add new comment