magento-1.9,layout,cart,blocks,template – How to Add Product Block to Magento 1.9 noItems.phtml Page

blockscartlayoutmagento-1.9template

I'm trying to add product blocks to the noitems [your cart is empty] page in magento 1.9
I kind of figured it would be something like:

<checkout_cart_noItems>

     <reference name="content">

          <block>whatever</block>

     </reference>

</checkout_cart_noItems>

But it's not, comparing to the default checkout.xml layout file I see that a template is set:

<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>

How can I add my product blocks to the noItems.phtml file in my local.xml?

Best Answer

The noItems.phtml template is rendered by the same block as the normal cart.phtml template, depending on the number of items in the cart.

But it can contain a child checkout_cart_empty_widget, which is only rendered on the noItems.phtml cart. It does not exist in the default theme and is an empty container in the default rwd theme (used to attach widgets to it).

If you are using the rwd theme or a theme that is based on the rwd theme, you can add additional blocks to it:

<checkout_cart_index>
    <reference="checkout.cart.empty.widget">
        <block>whatever</block>
    </reference>
</checkout_cart_index>

If not, you can add it, the same way as it's done in the rwd theme:

<checkout_cart_index>
    <reference name="checkout.cart">
        <block type="core/text_list" name="checkout.cart.empty.widget" as="checkout_cart_empty_widget" translate="label">
            <label>Empty Shopping Cart Content Before</label>
        </block>
    </reference>
</checkout_cart_index>

Add block below "No items" message:

This block is rendered before the "You have no items in your shopping cart." message.

If you want to add something below this message, there is another child, called shopping.cart.table.after, which is not created anywhere in the default themes as far as I can see, but it is also rendered in cart.phtml of the rwd theme (not the default theme), so if you want to use this and be compatible with different themes, better create a custom product block that is only displayed if the cart is empty:

protected function _toHtml()
{
    if (Mage::getSingleton('checkout/session')->getQuote()->getItemsCount() == 0) {
        return parent::_toHtml();
    } else {
        return '';
    }
}

The layout XML should be like this, to add the referenced but non-existing container block and add yours as a child:

<checkout_cart_index>
    <reference name="checkout.cart">
        <block type="core/text_list" name="shopping.cart.table.after" as="shopping.cart.table.after" translate="label">
            <label>Shopping Cart Content After</label>

            <block>whatever</block>

        </block>
    </reference>
</checkout_cart_index>