Question

I'm trying to add a custom template just after ADD To Cart Button in product's page. In catalog_product_view.xml its defined as bellow:

<block class="Magento\Catalog\Block\Product\View"
    name="product.info.addtocart.additional" as="product.info.addtocart"
template="Magento_Catalog::product/view/addtocart.phtml"/>

Then in my module I do:

<referenceBlock name="product.info.addtocart">
    <block name="myBlock"/>
</referenceBlock>

In Reference block when name is: product.info.addtocart (alias) my block is rendered only for Simple products, when it is product.info.addtocart.additional(name) renders only for configurable product.

Debugging Magento_Catalog::product/view/addtocart.phtml, when I evaluate expression $block->getNameInLayout() confirm this behavior, if I'm loading simple product shows alias, if configurable name.

Can someone explain why its happening? Is that a Magento bug or am I doing something wrong? I'm using version 2.4.2

Was it helpful?

Solution

I have got the solution by core layout reference, I have displayed my custom template just before adding to cart button in all types of product by below code

app/code/Namespace/MyModule/view/frontend/layout/catalog_product_view.xml

 <?xml version=""1.0""?>
    <page layout=""1column"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:noNamespaceSchemaLocation=""urn:magento:framework:View/Layout/etc/page_configuration.xsd"">
        <body>
            <referenceBlock name=""product.info.addtocart"">
                <block class=""Magento\Catalog\Block\Product\View"" name=""product.custom.simple"" template=""Namespace_ModuleName::product/view/custom.phtml"" />
            </referenceBlock>
            <referenceBlock name=""product.info.addtocart.additional"">
                <block class=""Magento\Catalog\Block\Product\View"" name=""product.custom.additional"" template=""Namespace_ModuleName::product/view/custom.phtml"" />
            </referenceBlock>
        </body>
    </page>

app/code/Namespace/MyModule/view/frontend/layout/catalog_product_view_type_bundle.xml

<?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=""product.info.addtocart.bundle"">
            <block class=""Magento\Catalog\Block\Product\View"" name=""product.custom.bundle.additional"" template=""Namespace_ModuleName::product/view/custom.phtml"" />
        </referenceBlock>
    </body>
</page>

OTHER TIPS

this one should work. may be you are doing something wrong. I used below code for my one of site.

<referenceBlock name="product.info.addtocart">
    <block class="Dcs\Productenquiry\Block\Productenquiry" before="managedprint.button" name="productenquiry.button" template="enquire-button.phtml" cacheable="false"/>
</referenceBlock>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top