How to override a template file in module creation in Magento2

you can try with below code in xml 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>
        <referenceContainer name="content">
            <referenceBlock name="product.info.addtocart">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Hello_Custom::product/view/addtocart.phtml</argument>
                </action>
            </referenceBlock>
         </referenceContainer>
    </body>
</page>

There are three methods available to override template files in Magento

For example: Here we override addtocart.phtml file for simple product.

Method 1:

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

Method 2:

<referenceContainer name="content">
    <referenceBlock name="product.info.addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</referenceContainer>

Method 3:

<referenceContainer name="product.info.form.content">
    <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</referenceContainer>

Tags:

Magento2