2

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?

2 Answers 2

3

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>
2
  • I've been modifying the foundation based theme - luckily it appears to be based on rwd... there was already a definition for the <checkout_cart_index> in my local. all I had to do was add my block to it. Sep 28, 2015 at 12:37
  • good logic.. @fschmengler.. :)
    – Amit Bera
    Sep 28, 2015 at 12:42
6

The requirement cannot be achieved by local.xml file, because is no layout handle exist with name checkout_cart_noItems .

For this case,you need to

  • create a handler on event controller_action_layout_load_before.
  • On observer create that handler on when cart qty 0; name is checkout_cart_noItems.

Observer code:

    <?php
    class [ModuleNameSpace]_[ModuleName]_Model_Observer
    { 
        /**
         * Before load layout event handler
         *
         * @param Varien_Event_Observer $observer
         */
        public function beforeLoadLayout($observer)
        {

            $action = Mage::app()->getFrontController()->getAction();
            if ($action->getFullActionName() == 'cart_index_index') {
                $cart=Mage::getSingleton('checkout/cart')'
                if ($cart->getQuote()->getItemsCount() < 1) {
                    $observer->getEvent()->getLayout()->getUpdate()
                        ->addHandle('checkout_cart_noItems');
                }
           }
       }
   }

Config.xml code

 <global>
        <models>
            <[MyCustomModule_Model_Class_Groupname]>
                <class>[ModuleNameSpace]_[ModuleName]_Model</class>
            </[MyCustomModule_Model_Class_Groupname]>
        </models>
    </global>
       <frontend>
            <events>
                <controller_action_layout_load_before>
                    <observers>
                        <my_current_page_is_observer>
                            <class>[MyCustomModule_Model_Class_Groupname]/observer</class>
                            <method>beforeLoadLayout</method>
                        </my_current_page_is_observer>
                    </observers>
                </controller_action_layout_load_before>
         </events>
       </frontend>

Depends on this handler ,you can set different template block

Also you can do anything layout changes .

 <checkout_cart_noItems>
     <reference name="content">
         <block>whatever</block>
     </reference>
 <checkout_cart_noItems>
1
  • was not able to try this solution as fschmengler's answer happened to work for me. Sep 28, 2015 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.