{"id":2785,"date":"2024-12-31T20:26:31","date_gmt":"2024-12-31T20:26:31","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/programmatically-add-product-attributes-in-shipment-emails-template-in-magento-2\/"},"modified":"2025-03-17T06:00:04","modified_gmt":"2025-03-17T06:00:04","slug":"add-product-attributes-in-shipment-emails-template-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-product-attributes-in-shipment-emails-template-in-magento-2\/","title":{"rendered":"Programmatically Add Product Attributes in Shipment Emails Template in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Do you want to personalize shipment emails in Magento 2? Learn the programmatic method to&nbsp;<em><strong>add product attributes in shipment emails template in Magento 2<\/strong><\/em>.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/config\/sales\/sales-emails\" target=\"_blank\" rel=\"noreferrer noopener\">Order-related emails in Magento 2<\/a>&nbsp;acknowledge the customers and keep them updated on the latest status. But, we all know\u2014in 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/customize-email-templates-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">customize the existing email templates<\/a>&nbsp;under&nbsp;<strong>Marketing &gt; Communications &gt; Email Templates<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default Magento 2 allows you to add store-related variables to the email templates. But what if you want to call &amp; display product attributes in the shipment emails in Magento 2? There is no default functionality to help you in this case. Therefore, you\u2019ll need to follow a programmatic method to do that!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Add Product Attributes in Shipment Emails Template in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say we have a custom attribute named&nbsp;<strong>Warehouse Location<\/strong>&nbsp;in Magento 2, which we want to add to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/configure-shipment-emails-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">shipment emails<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we need to modify the&nbsp;<code>.phtml<\/code>&nbsp;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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Log into your Magento 2 admin.<\/li>\n\n\n\n<li>In the left-side panel, navigate to&nbsp;<strong>Stores &gt; Attributes &gt; Products<\/strong>.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2023\/05\/Go-to-stores-attributes-products.png\" alt=\"Go to stores attributes products\" class=\"wp-image-31925\"\/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>From the list of product attributes, copy the Code of the desired attribute (as shown below.)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2023\/05\/Copy-product-attribute-code.png\" alt=\"Copy product attribute code to add it to shipment emails template\" class=\"wp-image-31926\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Now, you can add this product attribute to shipment emails by adding the following code snippet to the&nbsp;<em><strong>vendor\/magento\/module-sales\/view\/frontend\/templates\/email\/items\/shipment\/default.phtml<\/strong><\/em>&nbsp;file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php if ($product->getShelfLocation()) { ?>\n&lt;p class=\"shelf_location\">&lt;?= $block->escapeHtml(__('Shelf Location')) ?>: &lt;?= $block->escapeHtml($product->getShelfLocation()) ?>&lt;\/p>\n&lt;?php } ?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the attribute code and the content as per your case.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ta-daaa!!   This is how you can add product attributes in the shipment emails template in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me know if you face any issues or have queries regarding the solution. I would be happy to help you!  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you! Please consider sharing this post via social media if you find it helpful.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to personalize shipment emails in Magento 2? Learn the programmatic method to&nbsp;add product attributes in shipment emails template in Magento 2. Order-related&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2785","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2785"}],"version-history":[{"count":1,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2785\/revisions"}],"predecessor-version":[{"id":3794,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2785\/revisions\/3794"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}