1

Product detail page:

enter image description here

Category Product list

enter image description here

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">
            <referenceBlock name="product.info.addtocart" template="Cm_Preorder::product/view/addtocart.phtml" />
            <referenceBlock name="product.info.addtocart.additional" template="Cm_Preorder::product/view/addtocart.phtml" />
        </referenceContainer>
    </body>
</page>

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Cm_Preorder" setup_version="1.0.1" schema_version="1.0.1">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>  
    </module>
</config>

app/code/Cm/Preorder/view/frontend/templates/product/view/addtocart.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Pre order'); ?>
<?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>
        <?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; ?>
3
  • what issue ???? – Rk Rathod May 13 '19 at 7:47
  • nothing comes add to cart button does not change to pre order. its not override – Divya Sekar May 13 '19 at 7:49
  • check my answer and override .xml file like this – Rk Rathod May 13 '19 at 7:50
2

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

OR

<?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" template="VendorName_Catalog::product/view/addtocart.phtml"/>
        <referenceBlock name="product.info.addtocart.additional" template="VendorName_Catalog::product/view/addtocart.phtml"/>
    </body>
</page>

Make sure your module load after Magento_Catalog module. So add sequence in module.xml.

[Update]

Output: enter image description here

8
  • I was follow your ways but it not working prnt.sc/nnta3s – Divya Sekar May 13 '19 at 7:47
  • hi @Sohel. I am adding product from ajax call, but quoteId not getting retrieved, here is my new question, can you help me please,magento.stackexchange.com/questions/274172/… – jafar pinjar May 13 '19 at 7:51
  • Did you add sequence tag in module.xml? – Sohel Rana May 13 '19 at 8:10
  • Check my edited question @sohel – Divya Sekar May 13 '19 at 8:49
  • @divyasekar check other module already overwrite this or not. Working fine with fresh magento installation. Check screenshot. – Sohel Rana May 13 '19 at 9:05
0

module.xml :-

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="VendorName_ModuleName" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

app/code/VendorName/ModuleName/view/frontend/layout/catalog_product_view.xml

Add This Code :-

<?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"> //Product Detail Page
            <action method='setTemplate'>
                <argument name="template" xsi:type="string">VendorName_ModuleName::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional"> //Product Listing Page
            <action method='setTemplate'>
                <argument name="template" xsi:type="string">VendorName_ModuleName::product/list/items.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

For Listing Page Copy This File In To Your product/list/items.phtml:-

vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml

For Product View Page Copy This File In To Your product/view/addtocart.phtml:-

vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml
1
  • i want to override into my custom module – Divya Sekar May 13 '19 at 7:28

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.