Overriding “base” layout not working
سؤال
I'm reading the documentation here. My goal is to remove the "Customize and Add to Cart" button, for a bundled product
The path for the layout can be found here /vendor/magento/module-gift-registry/view/frontend/layout/catalog_product_view_type_bundle.xml
. I want to remove the first action
<action method="setGiftRegistryTemplate">
<argument name="blockName" xsi:type="string">customize.button</argument>
<argument name="template" xsi:type="string">Magento_GiftRegistry::product/customize.phtml</argument>
</action>
which meets the criteria in the documentation for overriding a layout file
Examples of customizations that involve overriding layouts:
- Suppressing method invocation.
Note there is no method to cancel the prior invocation in the Block class itself. So I have added this file in my custom theme
/app/design/frontend/vendor/theme-name/Magento_GiftRegistry/layout/override/base/catalog_product_view_type_bundle.xml
, duplicating the stock layout file and omitting the first action (referenced above).
There is no change on the frontend, the button is still there. I found this thread, however the original question was not answered, and I'm looking for an answer to that very question now.
So what am I doing wrong?
المحلول 3
In the end I found that both the layout override and extension were needed. Per both Satish and Roman, the layout extension
<referenceBlock name="customize.button" remove="true" />
is needed in
app/design/frontend/vendor/theme-name/Magento_Bundle/layout/catalog_product_view_type_bundle.xml
However the layout override in
app/design/frontend/vendor/theme-name/Magento_GiftRegistry/layout/override/base/catalog_product_view_type_bundle.xml
is also needed, because without it Magento tries to call setGiftRegistryTemplate
on a block that does not exist!
نصائح أخرى
Briefly, you're overwriting wrong layout :)
"Customize and add to cart" button is originally created in Bundle module:
vendor/magento/module-bundle/view/frontend/layout/catalog_product_view_type_bundle.xml:76
<referenceContainer name="product.info.main"> <block class="Magento\Catalog\Block\Product\View" name="customize.button" as="customize_button" template="Magento_Bundle::catalog/product/view/customize.phtml" after="product.info.price" /> </referenceContainer>
It shows button text as:
<span><?= $block->escapeHtml(__('Customize and Add to Cart')) ?></span>
GiftRegistry module merely replaces above block template if condition is met
return $this->getRequest()->getParam('options') == self::FLAG; // flag = "giftregistry"
Replaced template
Magento_GiftRegistry::product/customize.phtml
has another button text:<button id="bundle-slide" class="action primary customize" type="button"><span><?= $block->escapeHtml(__('Customize and Add to Gift Registry')) ?></span></button>
So your code actually works, it disables gift registry change, but original Bundle module button is not affected.
Test with such URL: https://example.com/catalog/product/view/id/52?options=giftregistry
As mentioned before, you don't need override in this case, <referenceBlock name="customize.button" remove="true" />
works (tested).
P.S. You can peek at layout merge/override process using debug here:
Magento\Framework\View\File\Collector\Base
<modules>/view/base/layout/*.xml
<modules/view/frontend/layout/*.xml
Magento\Framework\View\File\Collector\ThemeModular
<theme>/*_*/layout/*.xml
Magento\Framework\View\File\Collector\Override\Base
<theme>/*_*/layout/override/base/*.xml
Magento\Framework\View\File\Collector\Override\ThemeModular
<theme>*_*/layout/override/theme/*/*/*.xml
Source: magento2-exam-notes
You can create an XML file in your theme same as the given path:
app/design/frontend/vendor/theme-name/Magento_Bundle/layout/catalog_product_view_type_bundle.xml
and put below code in that file.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customize.button" remove="true" />
</body>
</page>