1

I have done this but my code is not working for all types of product.

layout file

<?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.stock.sku" remove="true"/>

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

    </body>
</page>

Template

<?php

?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
            <label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       maxlength="12"
                       value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                       title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                       data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <!-- Custom Input Field -->
        <div>
            <input
                type="text"
                name="remarks"
                id="remarks"
                maxlength="255"
                placeholder="Please enter your name"
            />
        </div>
        <!-- Custom Input Field -->
        <br>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
            </button>
            <?php echo $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": false
            }
        }
    }
</script>
<?php endif; ?>
  • You can show us your folder structure? – Khoa TruongDinh Jan 6 '17 at 12:35
  • please show your layout full path? – Rakesh Jesadiya Jan 16 '17 at 7:11
  • I didn't get you? Means – Pinku Jan 16 '17 at 7:18
5

Use following code in your module's catalog_product_view.xml file

<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">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Modulename::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Modulename::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

product.info.addtocart is used for simple products, and product.info.addtocart.additional is used for configurable products.

3

Try to follow code catalog_product_view.xml

<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.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Modulename::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
  • Still This layout is not working for all type of products – Pinku Jan 11 '17 at 6:33
  • are you try to remove layout="1column" in your xml file ? – Hardik Visa Jan 11 '17 at 7:23
  • I'm trying to remove this <referenceBlock name="product.info.stock.sku" remove="true"/> – Pinku Jan 16 '17 at 6:48
  • Thanks - that did the trick for me. Why does product.info.addtocart.additional work, but product.info.addtocart doesn't? What's the difference and why do they exist? – Erfan Jul 13 '17 at 14:06
3

Since the <action> method is deprecated and advised against in the Dev Docs I thought I'd provide the right way to do this.

This is quoted from the Dev Docs:

Example 1:

<referenceBlock name="page.main.title" template="%Namespace_Module::new_template.phtml%"/>

Example 2:

<referenceBlock name="page.main.title">
    <arguments>
        <argument name="template" xsi:type="string">%Namespace_Module::new_template.phtml%</argument>
    </arguments>
</referenceBlock>

Source: https://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#set_template

Explanation:

The trick here is to look in the original module's layout XML to see which method to use.

The product.info.addtocart (Simple Products) and product.info.addtocart.additional (Configurable Products) are defined in Magento_Catalog/layout/catalog_product_view.xml as follows:

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

And:

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

So the right way to override the template file in a module is example 1, or:

Namespace/Module/view/frontend/layout/catalog_product_view.xml:

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <!-- Simple Products -->
            <referenceBlock name="product.info.addtocart" template="Namespace_Module::product/view/addtocart.phtml" />

            <!-- Configurable Products -->
            <referenceBlock name="product.info.addtocart.additional" template="Namespace_Module::product/view/addtocart.phtml" />
        </referenceContainer>
    </body>
</page>

Hope this is helpful for someone.

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.