Modifying Magento Order Slip for Custom Product Attribute Display
Introduction
Displaying custom product attributes on the Magento Order Slip PDF can be a crucial requirement for businesses working with multiple SKUs from different suppliers or manufacturers. This allows distributors to quickly identify specific items within their inventory.
In this guide, we’ll walk you through the steps to display an additional custom attribute from a product on the Order Slip PDF in Magento.
Identifying the Required Changes
Understanding the Magento Architecture
The Magento order slip is generated using the Mage_Sales_Model_Order_Pdf_Invoices class. This class extends the Zend_Pdf library to create the PDF and includes data from the sales order model.
Key Files Involved
app/code/core/Mage/Sales/Model/Order/Pdf/Invoices.phpapp/design/frontend/<your_package>/<your_theme>/template/sales/order/pdf/invoice.phtml
Adding Custom Product Attribute to the Order Slip PDF
Step 1: Define Your Custom Attribute
Ensure you have already created a custom product attribute in Magento that you want to display on the order slip. For example, let’s say we have an attribute named distributor_sku.
Step 2: Modify the Invoice PHP File
Open app/code/core/Mage/Sales/Model/Order/Pdf/Invoices.php and find the _drawHeader() function.
private function _drawHeader($page)
{
// ...
$this->getOrder()->setIsGroupedMode(false);
$this->_setFontRegular($page, 10);
$top = $this->getY();
// ...
// Add the new line for the custom attribute
$page->drawText('Distributor SKU', 35, $top - 40, 'UTF-8');
}
Step 3: Modify the Invoice PHTML File
Open app/design/frontend/<your_package>/<your_theme>/template/sales/order/pdf/invoice.phtml.
Find the line where order items are being looped. It should look something like this:
<?php foreach ($this->getOrder()->getAllItems() as $item): ?>
<tr>
// ...
</tr>
<?php endforeach; ?>
Step 4: Add the Custom Attribute to the Loop
Add the custom attribute to the loop.
<?php foreach ($this->getOrder()->getAllItems() as $item): ?>
<tr>
// ... (existing code)
<td width="15%" align="left" style="border-left: 0 !important;">
<?php echo $this->htmlEscape($item->getDistributorSku()) ?>
</td>
// ... (remaining code)
</tr>
<?php endforeach; ?>
Conclusion
Displaying a custom product attribute on the Magento Order Slip PDF involves modifying core files. Always remember to backup your changes and test thoroughly before implementing them in a live environment.
The steps outlined above should help you add any additional attributes you need to display, allowing your distributors to quickly identify products by their supplier or manufacturer SKUs.
For more advanced customizations and extensions for Magento 2, consider exploring the services and resources available at Laravel Company.