I 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.
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.
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.
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.
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!