I'm trying to override Magento's built-in addtocart.phtml file, but nothing I try seems to be working.
I Have a module named QuantityDropdown under the Rethink namespace. As far as I can see it's installed properly as it displays in the list when I run php bin/magento setup:upgrade and my Setup/InstallSchema.php file properly creates a database table.
I've copied the original file from:
/vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml
...to my local version at:
/app/code/Rethink/QuantityDropdown/view/frontend/templates/product/view/addtocart.phtml
I then created a new XML file to point to my module's phtml file:
/app/code/Rethink/QuantityDropdown/view/frontend/layout/catalog_product_view.xml
which contains:
<?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">Rethink_QuantityDropdown::product/view/addtocart.phtml</argument>
 </action>
 </referenceBlock>
 </referenceContainer>
 </body>
</page>
I also updated my module file at /app/code/Rethink/QuantityDropdown/etc/module.xml file to include Magento_Catalog in the sequence:
<?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="Rethink_QuantityDropdown" setup_version="0.0.1">
 <sequence>
 <module name="Magento_Catalog"/>
 </sequence>
 </module>
</config>
I've also tried several different XML methods in my catalog_product_view.xml but nothing seems to be working.
Are there any other changes I should be making in the admin panel or are changes just based on the XML files? I feel like I must be missing something.
1 Answer 1
Just use below code in your catalog_category_view.xml file, remove cache and check,
<?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">Rethink_QuantityDropdown::product/view/addtocart.phtml</argument>
 </action>
 </referenceBlock> 
 <referenceBlock name="product.info.addtocart.additional">
 <action method="setTemplate">
 <argument name="template" xsi:type="string">Rethink_QuantityDropdown::product/view/addtocart.phtml</argument>
 </action>
 </referenceBlock> 
 </body>
</page>
 Explore related questions
See similar questions with these tags.