1

I am trying to change checkout/cart.phtml through layout update in my module's layout file i.e. mymodule.xml

<layout>
    <checkout_cart_index>
        <reference name="checkout.cart">
            <action method="setCartTemplate"><value>mymodule/checkout/cart.phtml</value></action>
        </reference>
    </checkout_cart_index>
</layout>

But It is not working. Any clues?

5
  • you can place your cart.phtml inside checkout folder in your design package and do necessary changes. Commented Jan 27, 2014 at 12:12
  • But I want it to keep in my module's folder. I don't want to replace the default one.
    – Ankita P.
    Commented Jan 27, 2014 at 12:30
  • The tag <checkout_cart_index> must be outside <default> tag in your local.xml Commented Jan 27, 2014 at 12:53
  • I am not using <default> tag in xml.
    – Ankita P.
    Commented Jan 27, 2014 at 13:03
  • <action method="setTemplate"><template>mymodule/checkout/cart.phtml</template></action> Commented Jan 27, 2014 at 13:05

2 Answers 2

5

Ankita, What I'm about to write is the actual way to get what you want. While the official answer by John Hickling will work, it is not how Magento intended the main cart template to be modified.

Magento deliberately chose to use different methods for setting the cart templates, namely, setCartTemplate and setEmptyTemplate. They can be seen in Magento's own app/design/frontend/base/default/layout/checkout.xml. This was done so that two templates can be managed, each to handle their own condition. The first condition is for a cart with items, while the second condition is for a cart without items. By using the common setTemplate method, that distinction will be lost: a cart with items and a cart without items will both display the same template. This is no good.

You were so close. You were correct in trying to use the setCartTemplate method. That is what you should be using. However, you were missing one essential method call that would allow Magento to even consider using it: you forgot to include the chooseTemplate method call. Note Magento's own checkout.xml file:

<block type="checkout/cart" name="checkout.cart">
    <action method="setCartTemplate"><value>checkout/cart.phtml</value></action>
    <action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
    <action method="chooseTemplate"/>

Look at that last method call, chooseTemplate. If you look in app/code/core/Mage/Checkout/Block/Cart.php you will see the following method, within which those familiar setCartTemplate and setEmptyTemplate methods are called, but because they are magic methods, they are not easily searchable in Magento's source, which is problematic for a lot of people:

public function chooseTemplate()
{
    $itemsCount = $this->getItemsCount() ? $this->getItemsCount() : $this->getQuote()->getItemsCount();
    if ($itemsCount) {
        $this->setTemplate($this->getCartTemplate());
    } else {
        $this->setTemplate($this->getEmptyTemplate());
    }
}

You were missing that chooseTemplate method call. This is what your own layout XML file should look like:

<checkout_cart_index>
    <reference name="checkout.cart">
        <action method="setCartTemplate"><value>mymodule/checkout/cart.phtml</value></action>
        <action method="setEmptyTemplate"><value>mymodule/checkout/noItems.phtml</value></action>
        <action method="chooseTemplate"/>
    </reference>
</checkout_cart_index>

I recommend you update your code if it is still under your control. This is how Magento intended the cart templates to be updated. The common setTemplate method is too destructive for this task. Granularity was Magento's intention, so updates should maintain that granularity. I also recommend you mark this as the correct answer.

0

The method is setTemplate not setCartTemplate, like so:

<layout>
    <checkout_cart_index>
        <reference name="checkout.cart">
            <action method="setTemplate"><value>mymodule/checkout/cart.phtml</value></action>
        </reference>
    </checkout_cart_index>
</layout>
0

Your Answer

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

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