【问题标题】:Not able to change checkout/cart.phtml through layout update无法通过布局更新更改 checkout/cart.phtml
【发布时间】:2014-01-27 11:42:54
【问题描述】:

我正在尝试通过模块布局文件(即 mymodule.xml)中的布局更新来更改 checkout/cart.phtml

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

但它不起作用。有什么线索吗?

【问题讨论】:

  • 您可以将 cart.phtml 放在设计包的结帐文件夹中并进行必要的更改。
  • 但我希望它保存在我的模块文件夹中。我不想替换默认的。
  • 标签 必须在 local.xml 中的 标签之外
  • 我没有在 xml 中使用 标签。

标签: magento checkout


【解决方案1】:

Ankita,我要写的是实际的方式来得到你想要的。虽然 John Hickling 的官方回答会起作用,但这并不是 Magento 打算修改主购物车模板的方式。

Magento 故意选择使用不同的方法来设置购物车模板,即setCartTemplatesetEmptyTemplate。它们可以在 Magento 自己的app/design/frontend/base/default/layout/checkout.xml 中看到。这样做是为了可以管理两个模板,每个模板都可以处理自己的情况。第一个条件是有物品的购物车,而第二个条件是没有物品的购物车。通过使用常见的setTemplate 方法,这种区别将会消失:带有物品的购物车和不带物品的购物车都将显示相同的模板。这不好。

你离得太近了。您尝试使用 setCartTemplate 方法是正确的。这就是你应该使用的。但是,您错过了一个允许 Magento 甚至考虑使用它的基本方法调用:您忘记包含 chooseTemplate 方法调用。注意 Magento 自己的checkout.xml 文件:

<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"/>

查看最后一个方法调用,chooseTemplate。如果您查看app/code/core/Mage/Checkout/Block/Cart.php,您会看到以下方法,其中调用了那些熟悉的setCartTemplatesetEmptyTemplate 方法,但是由于它们是魔术方法,因此在Magento 的源中不容易搜索到,这对于一个很多人:

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

你错过了chooseTemplate 方法调用。这是您自己的布局 XML 文件的样子:

<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>

如果代码仍在您的控制之下,我建议您更新代码。这就是 Magento 打算更新购物车模板的方式。常见的setTemplate 方法对于这项任务来说破坏性太大。粒度是 Magento 的意图,因此更新应该保持这种粒度。我还建议您将此标记为正确答案。

【讨论】:

    【解决方案2】:

    方法是setTemplate而不是setCartTemplate,像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多