Customizing Magento Order Print Page: A Practical Guide
In Magento, customizing the order print page involves modifying the appropriate layout file and template files to add or modify the displayed content. Let’s break down how you can achieve this step-by-step.
Understanding the Order Print Process
Magento uses a dedicated controller action (Mage_Sales_OrderController::printAction) to handle the order printing process. This action retrieves the order data using the provided order ID and then passes it to a view block, which renders the HTML content for the print page.
The key files involved in this process are:
- Layout File:
app/design/frontend/{package}/{theme}/layout/sales_order_print.xml - Block File:
app/code/core/Mage/Sales/Block/Order/Print.php - Template File:
app/design/frontend/{package}/{theme}/template/sales/order/print.phtml
Locating the Relevant Files
You’ve already found the crucial template file responsible for rendering the order print page: app/design/frontend/{package}/{theme}/template/sales/order/print.phtml.
This file contains a foreach loop that iterates over each item in the order and renders it using the getItemHtml() method. The question now is, where does this getItemHtml() method come from?
Understanding getItemHtml()
The getItemHtml() method is called within the template file but isn’t defined there directly. It’s actually a method of the block class responsible for rendering the order items.
In Magento, blocks are PHP classes that handle the business logic and generate the HTML content for a given view. In this case, the relevant block is Mage_Sales_Block_Order_Item_Renderer.
Adding Product Descriptions to Order Items
To add product descriptions to each row on the print page, you need to modify how Magento renders individual order items.
Here’s a step-by-step approach:
-
Create a custom template: Make a copy of
app/design/frontend/{package}/{theme}/template/sales/order/item/renderer.phtmland save it ascustom_renderer.phtmlin your theme directory (app/design/frontend/{package}/{theme}/template/sales/order/item/). -
Modify the custom renderer template: Open the newly created
custom_renderer.phtmlfile. -
Find the product description line: Within this file, locate the line that outputs the product name and/or SKU.
-
The relevant part of the code should look something like this:
<td> <?php echo $this->escapeHtml($_item->getName()) ?> </td>
-
-
Add the product description: Immediately after the line above, add the following code to output the product’s short description:
<td> <?php echo $this->escapeHtml($_item->getDescription()); ?> </td> -
Update the print.phtml file: Open
app/design/frontend/{package}/{theme}/template/sales/order/print.phtmlagain. -
Replace the renderer block reference: Find the line that calls
$this->getItemHtml($_item). Replace it with a call to your custom block using the following code:<?php echo $this->getChildHtml('custom_renderer', 'order_item.renderer', $_item) ?> -
Update the layout file: Open
app/design/frontend/{package}/{theme}/layout/sales_order_print.xml. -
Add a reference to your custom block: Add the following XML code inside the
<sales_order_print>node:<reference name="order.print"> <block type="core/text_list" name="custom_renderer" as="custom_renderer"> <action method="setTemplate"> <template>sales/order/item/custom_renderer.phtml</template> </action> </block> </reference> -
Flush the cache: After saving all changes, flush Magento’s cache to ensure your modifications take effect.
Remember that you should always work in a child theme or use the “copy and modify” approach to make sure your changes survive updates.
Conclusion
Customizing Magento’s order print page involves understanding the layout and template system. By following this guide, you can successfully add product descriptions to the printable order view without touching core files or risking compatibility issues with upgrades.
For more advanced customizations and best practices in Magento theme development, check out our comprehensive guides on Laravel Company.
Happy coding!