抱歉,但我不知道如何定义我的问题。让我向您展示代码:

Magento 1:

 <checkout_cart_index>
    <reference name="checkout.cart">
        <action method="addItemRender">
            <type>simple</type>
            <block>mymodule/checkout_cart_item_renderer_simple</block>
            <template>checkout/cart/item/default.phtml</template>
        </action>
     </reference>
</checkout_cart_index>

上面的代码用于覆盖购物车页面中的简单产品条目。

现在我的问题是我怎样才能实现同样的事情 马真托2.

谢谢。

有帮助吗?

解决方案

在 Magento 中,渲染器定义如下 checkout_cart_item_renderers.xml, ,其中有几个:

  • app/code/Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml 对于默认和简单的产品渲染器
  • app/code/Magento/Bundle/view/frontend/layout/checkout_cart_item_renderers.xml 用于捆绑产品渲染器
  • app/code/Magento/Catalog/view/frontend/layout/checkout_cart_item_renderers.xml 用于虚拟产品渲染器
  • app/code/Magento/ConfigurableProduct/view/frontend/layout/checkout_cart_item_renderers.xml 用于可配置产品渲染器
  • app/code/Magento/Grouped/view/frontend/layout/checkout_cart_item_renderers.xml 用于分组产品渲染器
  • app/code/Magento/GiftMessage/view/frontend/layout/checkout_cart_item_renderers.xml 这个很特别,因为它为不同渲染器的礼物消息功能添加了额外的块
  • app/code/Magento/Wishlist/view/frontend/layout/checkout_cart_item_renderers.xml 这个也很特别,因为它为不同的渲染器添加了愿望清单功能的额外块

检查这些渲染器后,您可以找到要覆盖的渲染器。

正如您所说,您想要覆盖简单的产品渲染器,它是 Magento\Checkout\Block\Cart\Item\Renderer.

如果您想覆盖整个块,您应该使用 Magento 2 首选项:

在您的模块中,您可以创建一个 di.xml 文件下的 etc 包含以下内容的文件夹:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Cart\Item\Renderer" type="Vendor\ModuleName\Block\Checkout\Cart\Item\Renderer" />  
</config>

然后你需要创建 app/code/Vendor/Module/Block/Checkout/Cart/Item/Renderer.php 包含以下内容的类:

<?php
namespace Vendor\Module\Block\Checkout\Cart\Item;
class Renderer extends \Magento\Checkout\Block\Cart\Item\Renderer
{
    public function theFunctionYouWantToRewrite()
    {
        // Do your custom stuff
    }
}

但是,如果您想在方法级别而不是整个类级别上工作,则应该使用插件

由于它可能非常具体,我很难给你一个例子,但你可以在这里找到有关插件的有用资源:

许可以下: CC-BY-SA归因
scroll top