phpmagentomagento-rest-api

Magento: Add column in sales order grid without join


I need to add a custom column in the sales order adminhtml grid. Since I get the data for that grid from an external REST API by asking for data to a specific increment_id, I can't use the tutorials which are using a magento database to join the needed tables.

Is there another way like:

function rendering_sales_order_row_before($rowdata)  {
    $columnContent = $restapi->callByIncrementId($rowdata['increment_id']);
    $this->addColumn("Custom Column", $columnContent);
}

(This code should just illustrate my goal and I know that the solution will look completely different)

Is it possible to achieve that in magento in an elegant way?

EDIT: I'm using magento 1.9.2.1


Solution

  • Good question. I had a similiar problem. I solved it by using a custom renderer for my column.

    First add your Sales Order Grid block XX_ModuleName_Block_Adminhtml_Order_Grid. Rewrite Mage_Adminhtml_Block_Sales_Order_Grid, extend it and add your column by overriding _prepareColumns() method.

    $this->addColumn('my_column', array(
            'header' => Mage::helper('sales')->__('My Column'),
            'width' => '80px',
            'index' => 'my_column',
            'type' => 'text',
            'order' => 'x',
        ));
    

    Then add a custom renderer (XX_ModuleName_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_MyColumn) and extend Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract.

    Override the render(Varien_Object $row) method. Here you can do your specific operation (do your api request, manipulate in whatever way you want) and return the string. To build your request you want to use the data by param $row.

    That should work :) If you need more info, just comment.