1

I have two requirements

  1. Add template below addtocart button.
  2. Replace the core template of quantity and addtocart button with my own template. How to achieve this

My layout file is below:-

catalog_product_view.xml

<referenceContainer name="product.info.main">
    <block class="Vendor\Modulename\Block\Product\View\Stock"
        name="product.view.stock"
        template="Vendor_Modulename::product/view/stock.phtml"
        after="product.info.addtocart" ifconfig="form_stock/form_group_general/status">
    </block>
</referenceContainer>

By using this template. I am getting my template on the product detail page below the product name. but my requirement has different which is what I have shown above. If anybody knows then please guide me.

1

To add template on product page exactly below the Add to Cart button:

<?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.options.wrapper.bottom">
            <block
                class="[ProjectName]\[ModuleName]\Block\Catalog\Product\[BlockName]"
                name="product.info.[block_name]"
                template="[ProjectName]_[ModuleName]::catalog/product/[block_name].phtml"
                after="product.info.addtocart"/>
        </referenceBlock>
    </body>
</page>

To replace Quantity input and Add to Cart button template (both are on same 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="product.info.options.wrapper.bottom">
            <block
                name="product.info.addtocart"
                template="[ProjectName]_[ModuleName]::catalog/product/[block_name].phtml"/>
        </referenceBlock>
    </body>
</page>

The default template is located in vendor/magento/module-catalog/view/frontend/tempates/product/view/addtocart.phtml

0

Here is the way to add a template on product page using your module's catalog_product_view.xml file

<referenceBlock name="product.info.addtocart">
    <block class="Vendorname\Module\Block\Product\View\Extra"
                    name="product.view.extra"
                    template="Vendorname_Module::product/view/extra.phtml"
                    after="-"></block>
</referenceBlock>
<!-- for config products -->
<referenceBlock name="product.info.addtocart.additional">
    <block class="Vendorname\Module\Block\Product\View\Extra"
                    name="product.view.extra"
                    template="Vendorname_Module::product/view/extra.phtml"
                    after="-"></block>
</referenceBlock>

And you can overwrite addtocart.phtml using this

<referenceBlock name="product.info.addtocart">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendorname_Module::catalog/product/view/addtocart.phtml</argument>
    </action>
</referenceBlock>
<referenceBlock name="product.info.addtocart.additional">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendorname_Module::catalog/product/view/addtocart.phtml</argument>
    </action>
</referenceBlock>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.