Overriding Email Templates in Magento 2: A Step-by-Step Guide for Non-Devs
As a newbie to Magento, you might find yourself needing to customize email templates created by third-party extensions like Amitshree. While the learning curve can be steep, I’ll guide you through this process step by step.
Understanding the Template Structure in Magento 2
In Magento 2, all email templates are located under vendor/magento/module-email/view/frontend/templates. Third-party extensions typically store their custom templates within their own modules.
For Amitshree’s extensions, you can usually find them under something like app/code/Amitshree/ModuleName/view/frontend/templates.
Step 1: Locate the Template to Override
First, identify the exact template file you want to override. Let me assume it’s located at this path for now:
app/code/Amitshree/YourExtension/view/frontend/templates/email/sales/order_new.html
Step 2: Create a New File Structure
To override this template, you need to create a new directory structure in your theme.
- Navigate to
app/design/frontend/yourVendor/yourTheme/. - Inside the
Magento_Emailfolder (if it doesn’t exist, create it). - Inside
Magento_Email, create another folder calledtemplates. - Inside
templates, create a folder namedemail. - Finally, inside
email, create a folder for the specific template type you’re overriding. In your case, that would besales.
So now you’ve created this structure:
app/design/frontend/yourVendor/yourTheme/Magento_Email/templates/email/sales/
Step 3: Copy and Override the Template
Now let’s copy Amitshree’s template into your new folder.
- Open the terminal on your machine.
- Navigate to
app/codeusing the command line (you can use a tool like Git Bash or Terminal on Mac). - Use the following command to copy the file from Amitshree’s module:
cp -R app/code/Amitshree/YourExtension/view/frontend/templates/email/sales/order_new.html app/design/frontend/yourVendor/yourTheme/Magento_Email/templates/email/sales/
Step 4: Make Your Changes
Open the copied file in your preferred text editor or IDE (I recommend PhpStorm).
You can now start customizing the HTML, adding new blocks of content, modifying existing ones, etc. Remember to follow proper XHTML/HTML5 standards.
Step 5: Check and Test Your Template
Once you’ve made all your changes:
-
Save the file.
-
Clear your Magento cache using the command line:
php bin/magento cache:clean -
Now, trigger an order new email in your backend to see if your override is working correctly.
Conclusion and Further Reading
Overriding templates in Magento 2 can be a bit complex at first, but with practice, it becomes second nature. The key is understanding the directory structure and how Magento uses the theme hierarchy for template resolution.
If you’re looking to learn more about Magento development best practices, I recommend checking out Laravel Company’s Magento resources. They have excellent guides on customizing layouts and using XML layout files, which work hand in hand with the template overrides you’ve just learned.
Happy coding! Let me know if you need any further assistance.