Magento 2:製品タイプ別のブロッククラスをオーバーライドします
-
29-09-2020 - |
質問
申し訳ありませんが、私のQuetionを定義する方法がわからない。
マゼント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>
.
カートページの上書きのためのものです。
今私のクエリは、 magento 2 で同じことを達成する方法です。
ありがとうございました。
解決
マゼントの場合、レンダラーはcheckout_cart_item_renderers.xml
の下に定義されています。
-
app/code/Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml
用デフォルトおよびシンプル製品レンダラー - Bundle Products用
app/code/Magento/Bundle/view/frontend/layout/checkout_cart_item_renderers.xml
レンダラー - Virtual Products用
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
}
}
.
しかし、クラスレベル全体ではメソッドレベルで作業したい場合は、プラグインを使用する必要があります。
それは非常に具体的になることができるので、私があなたに例を与えるのは難しいですが、ここにプラグインについての役に立つリソースを見つけることができます:
所属していません magento.stackexchange