Programmatically Add Product Attributes in Shipment Emails Template in Magento 2
Do you want to personalize shipment emails in Magento 2? Learn the programmatic method to add product attributes in shipment emails template in Magento 2. 😀
Order-related emails in Magento 2 acknowledge the customers and keep them updated on the latest status. But, we all know—in eCommerce, personalization is the key! The default email notifications in Magento 2 can be customized in including attributes to offer a more personalized experience to the customers.
You can customize the existing email templates under Marketing > Communications > Email Templates.
The default Magento 2 allows you to add store-related variables to the email templates. But what if you want to call & display product attributes in the shipment emails in Magento 2? There is no default functionality to help you in this case. Therefore, you’ll need to follow a programmatic method to do that!
Recently, I faced a similar situation where the client wanted to display product attributes in the shipment emails. In this blog post, I will share the complete method that I used to add product attributes to shipment emails in Magento 2. 👇
How to Add Product Attributes in Shipment Emails Template in Magento 2
Let’s say we have a custom attribute named Warehouse Location in Magento 2, which we want to add to shipment emails.
Here, we need to modify the .phtml
file of the default template to add product attributes in the shipment emails template in Magento 2. For this, we first need the code of the attribute we want to add. To get it, follow these steps:
- Log into your Magento 2 admin.
- In the left-side panel, navigate to Stores > Attributes > Products.
- From the list of product attributes, copy the Code of the desired attribute (as shown below.)
Now, you can add this product attribute to shipment emails by adding the following code snippet to the vendor/magento/module-sales/view/frontend/templates/email/items/shipment/default.phtml file:
1 2 3 |
<?php if ($product->getShelfLocation()) { ?> <p class="shelf_location"><?= $block->escapeHtml(__('Shelf Location')) ?>: <?= $block->escapeHtml($product->getShelfLocation()) ?></p> <?php } ?> |
Replace the attribute code and the content as per your case.
In the above code, the first line checks whether the attribute has a non-empty value. If the value exists, then it is added to the shipment email.
Ta-daaa!! 🎉 This is how you can add product attributes in the shipment emails template in Magento 2.
Let me know if you face any issues or have queries regarding the solution. I would be happy to help you! 🙂
Thank you! Please consider sharing this post via social media if you find it helpful. 🍀
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
Install Now: Magento 2 Patch APSB23-35 to Keep Your Store Safe
Magento 2.4.7-beta2 Release Available! – Learn What’s New
Next