Oto jak to zrobić poprawnie i bez włamań.
Nie szukałem przypadku użycia OP, ale musiałem móc modyfikować rendery w koszyku. Problem polega na tym, że podobnie jak w przypadku OP Magento_Checkoutmoduł nie udostępnia nazw rendererom, co oznacza, że nie można do nich odwoływać, a ich szablony zmieniać przy użyciu tradycyjnych lub udokumentowanych metod. Jednak po pewnym przemyśleniu odkryłem, jak to zrobić za pomocą narzędzi, które Magento2 zapewnia nam bezpośrednio w układzie XML.
Zauważ, że istnieją inne miejsca, w których działa to samo podejście, na przykład w Magento\Sales\Block\Items\AbstractItemsbloku. Magento_CheckoutI Magento_Salesmoduły są dwie, które wykorzystać maksimum renderujących element, więc ta obejmuje wiele zapytań, które mogłyby prowadzić kogoś do zmieniających się szablonowi bloku bez nazwy. Powodem tego jest fakt, że inni szukają sposobu modyfikowania szablonów renderera w modułach kas lub sprzedaży.
Najpierw przedstawię rozwiązanie, a następnie szczegółowo je wyjaśnię każdemu, kto chce wiedzieć, dlaczego to działa.
Rozwiązanie
Dodaj następujące elementy do checkout_cart_index.xmlpliku układu:
<referenceBlock name="checkout.cart.form">
<arguments>
<argument name="overridden_templates" xsi:type="array">
<item name="default" xsi:type="string">LinusShops_Moneymaker::Magento_Checkout/cart/item/default.phtml</item>
<item name="simple" xsi:type="string">LinusShops_Moneymaker::Magento_Checkout/cart/item/simple.phtml</item>
<item name="configurable" xsi:type="string">LinusShops_Moneymaker::Magento_Checkout/cart/item/configurable.phtml</item>
</argument>
</arguments>
</referenceBlock>
Zauważ, że nazwa modułu i ścieżka muszą zostać zmodyfikowane, aby odzwierciedlić bazę kodu.
Wyjaśnienie
Działa to poprzez wykorzystanie overridden_templatesdanych bloku, które nie są domyślnie zdefiniowane.
W Magento_CheckoutThe checkout_cart_index.xmlfile układ określa następujący blok:
<block class="Magento\Checkout\Block\Cart\Grid" name="checkout.cart.form" as="cart-items" template="cart/form.phtml" after="cart.summary">
<block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.item.renderers" as="renderer.list"/>
<block class="Magento\Framework\View\Element\Text\ListText" name="checkout.cart.order.actions"/>
</block>
Następnie definiuje kilka rendererów w checkout_cart_item_renderers.xmlpliku układu:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="checkout_item_price_renderers"/>
<body>
<referenceBlock name="checkout.cart.item.renderers">
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="cart/item/default.phtml">
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.default.actions" as="actions">
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.default.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>
</block>
</block>
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="cart/item/default.phtml">
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.simple.actions" as="actions">
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.simple.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.simple.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>
</block>
</block>
</referenceBlock>
</body>
</page>
Niestety, nie można odwoływać się przez ich aliasów, defaulti simple, odpowiednio.
Jednak patrząc na Magento\Checkout\Block\Cart\Gridbloku, który jest nazwany checkout.cart.formi jest rodzicem renderujących, to można zauważyć, że nie jest to wywołanie getItemHtmlmetody w dołączonym szablonie cart/form.phtml. Ta metoda następnie wywołuje getItemRenderer. Obie te metody są zdefiniowane w Grid„s klasy dominującej AbstractBlock. Oto gdzie overridden_templateswykorzystywane są dane:
/**
* Retrieve item renderer block
*
* @param string|null $type
* @return \Magento\Framework\View\Element\Template
* @throws \RuntimeException
*/
public function getItemRenderer($type = null)
{
if ($type === null) {
$type = self::DEFAULT_TYPE;
}
$rendererList = $this->_getRendererList();
if (!$rendererList) {
throw new \RuntimeException('Renderer list for block "' . $this->getNameInLayout() . '" is not defined');
}
$overriddenTemplates = $this->getOverriddenTemplates() ?: [];
$template = isset($overriddenTemplates[$type]) ? $overriddenTemplates[$type] : $this->getRendererTemplate();
return $rendererList->getRenderer($type, self::DEFAULT_TYPE, $template);
}
Dzięki tej wiedzy zapełnianie bloku danymi z układu XML jest proste przy użyciu argumentsskładni Magento2 .