add new column with custom attribute in sales > order grid in Magento ver. 1.9.3.3
-
12-12-2019 - |
Question
Magento ver. 1.9.3.3
I am trying to add columns with **custom product attribute **(like publisher which was created by me) in Admin > sales >order
.
Copied
app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
to
app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
& create Renderer at
app/code/local/Mage/Adminhtml/Block/Sales/Order/Renderer/Productspublisher.php
code in Productspublisher.php
<?php
class Mage_Adminhtml_Block_Sales_Order_Renderer_Productspublisher extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$_item = $this->getItem());
$product=Mage::getModel('catalog/product')->load($_item->getProductId());
$str="";
foreach($product as $item)
{
$str .= $item->getAttributeText('publisher');
}
unset($product);
return $str;
}
}
?>
code in protected function _prepareColumns() of Grid.php
$this->addColumn('publisher', array(
'header' => Mage::helper('sales')->__('Publisher'),
'index' => 'publisher',
'renderer' => 'Mage_Adminhtml_Block_Sales_Order_Renderer_Productspublisher',
));
but getting error
Parse error: syntax error, unexpected ')' in sitedomain.com\app\code\local\Mage\Adminhtml\Block\Sales\Order\Renderer\Productspublisher.php on line 6
Solution
use this code in Productspublisher.php
<?php
class Mage_Adminhtml_Block_Sales_Order_Renderer_Productspublisher extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$order=Mage::getModel('sales/order')->load($row->getData('entity_id'));
$str="";
foreach($order->getAllVisibleItems() as $_item){
$product=Mage::getModel('catalog/product')->load($_item->getProductId());
$str .= $product->getAttributeText('publisher')."<br>";
}
unset($order);
return $str;
}
}
?>
OTHER TIPS
Following a blog post from atwix about ADDING A COLUMN TO MAGENTO ORDERS GRID - ALTERNATIVE WAY USING LAYOUT HANDLES
I would suggest you need a module that does the following.
Listens to the admin event sales_order_grid_collection_load_before
and then adds the correct attribute/join to the sales_order collection.
public function salesOrderGridCollectionLoadBefore($observer)
{
$collection = $observer->getOrderGridCollection();
// Now you can add attributes to select or join onto this collection
}
Add the correct column using a layout file for the module.
<layout>
<sales_order_grid_update_handle>
<reference name="sales_order.grid">
<action method="addColumnAfter">
<columnId>column_id</columnId>
<arguments>
<header>Column Title</header>
<index>column_attribute</index>
<filter_index>column_attribute</filter_index>
<type>text</type>
</arguments>
<after>shipping_name</after>
</action>
</reference>
</sales_order_grid_update_handle>
<adminhtml_sales_order_grid>
<!-- apply layout handle defined above -->
<update handle="sales_order_grid_update_handle" />
</adminhtml_sales_order_grid>
<adminhtml_sales_order_index>
<!-- apply layout handle defined above -->
<update handle="sales_order_grid_update_handle" />
</adminhtml_sales_order_index>
</layout>
I have not tried this myself but from reading through the complete blog post it seems not only very due-able but also a very nice approach without any rewrites.
There is serious error in your codes. What do you mean by this:
$order=Mage::getModel('catalog/product')->load($row->getData('getProductId'));
You are loading a product model here and assuming it is order object ?
foreach($order->getAllItems() as $_item){
You cannot call getAllItems() on a product object. Its a function of order model. If you share what you are trying to achieve, I can help you.