How To Add Custom Information To An Order Item In Order Sale Email In Magento 2.2.4
Introduction
Magento 2.2.4 provides a robust email system that allows you to send customized emails to customers based on various events, such as order placement, order cancellation, and more. One of the most important emails sent to customers is the Order Confirmation email, which includes details about the order, including the order items. However, by default, the Order Items Grid variable in the Order Confirmation Template only displays basic information about each order item. In this article, we will guide you on how to add custom information to the first column of the Order Items Grid in the Order Sale Email in Magento 2.2.4.
Understanding the Order Items Grid Variable
The Order Items Grid variable is a built-in variable in Magento that displays a grid of order items in the Order Confirmation email. The grid typically includes the following columns:
- Product Name: The name of the product.
- Quantity: The quantity of the product ordered.
- Price: The price of the product.
- Row Total: The total cost of the product.
However, the first column of the grid, which displays the product name, is not customizable by default. To add custom information to this column, you need to modify the Order Items Grid variable.
Modifying the Order Items Grid Variable
To add custom information to the first column of the Order Items Grid, you need to create a custom template for the Order Items Grid variable. Here's how you can do it:
Step 1: Create a Custom Template
Create a new file in the app/design/frontend/YourTheme/yourtheme/email/order/items/grid
directory. For example, you can create a file named custom_grid.phtml
.
Step 2: Modify the Custom Template
In the custom_grid.phtml
file, you need to modify the code that displays the first column of the grid. By default, the first column is displayed using the following code:
<?php foreach ($this->getItems() as $item): ?>
<tr>
<td><?php echo $this->escapeHtml($item->getName()) ?></td>
<td><?php echo $this->escapeHtml($item->getQty()) ?></td>
<td><?php echo $this->escapeHtml($item->getPrice()) ?></td>
<td><?php echo $this->escapeHtml($item->getRowTotal()) ?></td>
</tr>
<?php endforeach; ?>
To add custom information to the first column, you need to modify the code that displays the product name. For example, you can add a custom attribute to the product and display it in the first column.
Step 3: Add a Custom Attribute to the Product
To add a custom attribute to the product, you need to create a new attribute in the Magento admin panel. Here's how you can do it:
- Log in to the Magento admin panel.
- Go to System > Attributes > Manage Attributes.
- Click on Add New Attribute.
- Enter the attribute code, label, and select the attribute type (e.g., text, date, etc.).
- Save the attribute.
Step 4: Display the Custom Attribute in the First Column
To display the custom attribute in the first column, you need to modify the code in the custom_grid.phtml
file. For example:
<?php foreach ($this->getItems() as $item): ?>
<tr>
<td><?php echo $this->escapeHtml($item->getAttributeText('your_attribute_code')) ?></td>
<td><?php echo $this->escapeHtml($item->getQty()) ?></td>
<td><?php echo $this->escapeHtml($item->getPrice()) ?></td>
<td><?php echo $this->escapeHtml($item->getRowTotal()) ?></td>
</tr>
<?php endforeach; ?>
Replace your_attribute_code
with the actual code of the custom attribute you created.
Conclusion
Q: What is the Order Items Grid variable in Magento?
A: The Order Items Grid variable is a built-in variable in Magento that displays a grid of order items in the Order Confirmation email. The grid typically includes the following columns: Product Name, Quantity, Price, and Row Total.
Q: Why can't I customize the first column of the Order Items Grid variable?
A: The first column of the Order Items Grid variable is not customizable by default because it is hardcoded in the Magento code. However, you can create a custom template to modify the code that displays the first column.
Q: How do I create a custom template for the Order Items Grid variable?
A: To create a custom template, you need to create a new file in the app/design/frontend/YourTheme/yourtheme/email/order/items/grid
directory. For example, you can create a file named custom_grid.phtml
.
Q: What code do I need to modify in the custom template to add custom information to the first column?
A: You need to modify the code that displays the product name in the first column. By default, the code is:
<?php foreach ($this->getItems() as $item): ?>
<tr>
<td><?php echo $this->escapeHtml($item->getName()) ?></td>
<td><?php echo $this->escapeHtml($item->getQty()) ?></td>
<td><?php echo $this->escapeHtml($item->getPrice()) ?></td>
<td><?php echo $this->escapeHtml($item->getRowTotal()) ?></td>
</tr>
<?php endforeach; ?>
You need to replace the getName()
method with the custom attribute code you created.
Q: How do I add a custom attribute to the product in Magento?
A: To add a custom attribute to the product, you need to create a new attribute in the Magento admin panel. Here's how you can do it:
- Log in to the Magento admin panel.
- Go to System > Attributes > Manage Attributes.
- Click on Add New Attribute.
- Enter the attribute code, label, and select the attribute type (e.g., text, date, etc.).
- Save the attribute.
Q: How do I display the custom attribute in the first column of the Order Items Grid variable?
A: To display the custom attribute in the first column, you need to modify the code in the custom_grid.phtml
file. For example:
<?php foreach ($this->getItems() as $item): ?>
<tr>
<td><?php echo $this->escapeHtml($item->getAttributeText('your_attribute_code')) ?></td>
<td><?php echo $this->escapeHtml($item->getQty()) ?></td>
<td><?php echo $this->escapeHtml($item->getPrice()) ?></td>
<td><?php echo $this->escapeHtml($item->getRowTotal()) ?></td>
</tr>
<?php endforeach; ?>
Replace your_attribute_code
with the actual code of the custom attribute you created.
Q: Can I use this method to add custom information to other columns of the Order Items Grid variable?
A: Yes, you can use this method to add custom information to other columns of the Order Items Grid variable. Simply modify the code that displays the column you want to customize.
Q: Are there any limitations to this method?
A: Yes, there are limitations to this method. For example, you cannot use this method to add custom information to the Order Items Grid variable if you are using a third-party extension that modifies the grid. Additionally, this method may not work if you are using a custom theme that overrides the Order Items Grid variable.