Displaying Custom Product Attributes on Admin Order Items Block in Magento
As a senior developer with extensive experience in e-commerce platforms like Magento, I’ve encountered many scenarios where clients need to display custom product attributes on admin order pages. One of the most common requests is to show points earned for each product right alongside SKU and other information in the Items Ordered block.
To achieve this, we’ll modify the default item renderer and layout XML files to include your custom attribute value. Here’s a step-by-step guide:
Step 1: Create the Custom Item Renderer
First, create a new file named Namespace_Modulename_Block_Adminhtml_Sales_Order_View_Items_Renderer_Default.php in your module directory (usually app/code/local/Namespace/Modulename/Block/Adminhtml/Sales/Order/View/Items/).
In this file, extend the default item renderer class:
<?php
class Namespace_Modulename_Block_Adminhtml_Sales_Order_View_Items_Renderer_Default extends Mage_Adminhtml_Block_Sales_Order_View_Items_Renderer_Default
{
// Add your custom logic here, if needed
}
Step 2: Update the Layout XML
Next, open or create a new file named namespace_modulename.xml in your module’s layout folder (usually app/design/adminhtml/default/your_theme/layout/).
Add the following code inside the <layout> tag:
<adminhtml_sales_order_view>
<reference name="order_items">
<action method="addItemRender">
<type>default</type>
<block>sales/order_item_renderer_default</block>
<template>namespace/modulename/sales/order/items/renderer/default.phtml</template>
</action>
</reference>
</adminhtml_sales_order_view>
Here, we’re adding a new item render for the default type and pointing it to our custom PHTML template.
Step 3: Create the Custom PHTML Template
Now, create a new folder structure app/design/adminhtml/default/default/template/namespace/modulename/sales/order/view/items/renderer/ and inside this folder, create a file named default.phtml.
In this file, add your custom HTML and PHP logic to display the points earned attribute. Assuming the attribute is stored in the sales_flat_order_item table with the name customer_product_points, the code might look like this:
<?php $finalPointsEarned = ($_item->getCustomerProductPoints() * $_item->getQtyOrdered()); ?>
<div class="product-cart-sku">
<span style="color: #d4af37; font-weight: bold;">
<?php echo $this->__('Points Earned:'); ?>
<?php echo $finalPointsEarned; ?>
</span>
</div>
This code retrieves the points earned for each item and multiplies it by the quantity ordered to get the total points earned.
Conclusion
By following these steps, you should now be able to display your custom product attribute (points earned in this example) on the admin order Items Ordered block.
Remember:
- Always clear the cache after making changes.
- Test thoroughly to ensure the new attributes appear as expected.
- If you face any issues or exceptions, double-check your XML and PHTML files for syntax errors. Ensure that the attribute names match exactly with what’s stored in the database.
For more advanced customizations or if you need help with other admin order pages like Invoices or Refunds, feel free to explore our blog on displaying custom attributes in Magento.
Happy coding!