Domanda

Scusa ma non so come definire la mia quetion.let me mostra il tuo codice:

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

Sopra il codice è per sovrascrivere la semplice voce del prodotto nella pagina Carrello.

Ora la mia query è come posso raggiungere la stessa cosa in Magento 2 .

Grazie.

È stato utile?

Soluzione

In Magento, i rendering sono definiti sotto checkout_cart_item_renderers.xml, ci sono molti di essi:

    .
  • app/code/Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml per rendering di prodotti predefiniti e semplici
  • app/code/Magento/Bundle/view/frontend/layout/checkout_cart_item_renderers.xml per rendering di prodotti bundle
  • app/code/Magento/Catalog/view/frontend/layout/checkout_cart_item_renderers.xml per rendering di prodotti virtuali
  • app/code/Magento/ConfigurableProduct/view/frontend/layout/checkout_cart_item_renderers.xml per rendering di prodotti configurabili
  • app/code/Magento/Grouped/view/frontend/layout/checkout_cart_item_renderers.xml per rendering di prodotti raggruppati
  • app/code/Magento/GiftMessage/view/frontend/layout/checkout_cart_item_renderers.xml Questo è speciale in quanto aggiunge un blocco aggiuntivo per la funzione del messaggio regalo ai diversi rendering
  • app/code/Magento/Wishlist/view/frontend/layout/checkout_cart_item_renderers.xml Anche questo è speciale in quanto aggiunge un blocco aggiuntivo per la funzione Wishlist ai diversi rendering

Dopo aver controllato quei rendering, è possibile trovare il rendering che si desidera sovrascrivere.

Come hai detto, vuoi sovrascrivere il semplice rendering del prodotto che è un Magento\Checkout\Block\Cart\Item\Renderer.

Se si desidera sovrascrivere l'intero blocco, dovresti utilizzare le preferenze Magento 2:

Nel modulo, è possibile creare un file di.xml sotto la cartella etc con il seguente contenuto:

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

Quindi è necessario creare la classe app/code/Vendor/Module/Block/Checkout/Cart/Item/Renderer.php con il seguente contenuto:

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

Tuttavia, se si desidera lavorare sui livelli del metodo e non su tutto il livello di classe, è necessario utilizzare Plugin

Come può essere molto specifico, è difficile per me darti un esempio ma puoi trovare risorse utili sui plugin qui:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top