0

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

1 Answer 1

0

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.

2
  • Thanks, that helped a lot. I made a block rewrite in my config.xml in adminhtml, rewrite, sales_order_grid. In my Grid.php, I copied the whole Grid.php vom the core. Is there a better solution?
    – alexandre
    Commented Dec 18, 2015 at 13:00
  • @alexandre, Ideally you'd not want to copy the entire contents to the class. Only those methods you adjust. Preferable, not to use rewrites if you can avoid them. An example to adjust columsn without rewrites, using events: magento.stackexchange.com/questions/72324/… - this is of course for another grid, but the concept is the same
    – ProxiBlue
    Commented Dec 18, 2015 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.