Question

Since I got on stackoverflow the comment to post my problem here, I'll give it a try. I hope this is ok because of duplicate posting. https://stackoverflow.com/questions/34336110/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?

I'm using magento 1.9.2.1

Was it helpful?

Solution

You can place a custom renderer for a column in the grid.

The column definition:

protected function _prepareColumns()
{
   $this->addColumn('api_call_value',
   array(
          'header'=> Mage::helper('catalog')->__('Api Call Value'),
          'index' => 'entity_id', //<=== your main row id field
          'renderer'  => 'NAMESPACE_MODULE_Block_Adminhtml_Renderer_Api' // <== The code will go in this class
    )); 
}

Create the file: NAMESPACE/MODULE/Adminhtml/Renderer/Api.php

<?php

class NAMESPACE_MODULE_Block_Adminhtml_Renderer_Api
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $productId = $row->getProductId();
        .....
        $result = <YOUR CODE>
        .....
        return $result;

        );
    }
}

OR

you can use a frame_callback defined on the column:

protected function _prepareColumns()
    {
       $this->addColumn('api_call_value',
       array(
              'header'=> Mage::helper('catalog')->__('Api Call Value'),
              'index' => 'entity_id', //<=== your main row id field
              'frame_callback' => array($this, 'api_lookup_callback'),
        )); 
    }

and the place the callback method:

public function api_lookup_callback($value, $row, $column, $isExport)
    {
        $productId = $value
            .....
            $result = <YOUR CODE>
            .....
            return $result;
    }

I prefer the first option, as I like separating the logic to a new class file, as that make sit a bit more extensible, but both will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top