Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

magento - Not able to change checkout/cart.phtml through layout update

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...