0

I'm trying to add a grid to order listing table following this awesome blog which works if all code copied but I'm unable to customize it. And I want to show the data from my custom table. I've created model, resource model and collection model for my custom table. I have to store the order id from magento file system and order id from payment gateway API in it. My database table looks like this:

enter image description here Where storeoid is our system's primary key and orderid is the uniques id sent by the payment gateway API.

I have successfully added a grid in order listing table by using event

  <core_layout_block_create_after>
      <observers>
          <magestyapps_ordergrid_add_columns>
              <class>Myname_Mygateway_Model_Observer</class>
              <method>addColumnsToGrid</method>
          </magestyapps_ordergrid_add_columns>
      </observers>
  </core_layout_block_create_after> 

And corresponsing observer for it:

public function addColumnsToGrid(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();

    // Check whether the loaded block is the orders grid block
    if (!($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid)
        || $block->getNameInLayout() != 'sales_order.grid'
    ) {
        return $this;
    }

    // Add a new column rigth after the "Ship to Name" column
    $block->addColumnAfter('bankoid', [
        'header' => $block->__('Bank Order Id'),
        'index' => 'bankoid',
    ], 'shipping_name');

    return $this;
}

I can see the grid added in backend. Next I'm trying to load the order grid collection with data from my custom table. I listen to this event:

  <sales_order_grid_collection_load_before>
      <observers>
          <magestyapps_ordergrid_prepare_collection>
              <class>Myname_Mygateway_Model_Observer</class>
              <method>prepareOrderGridCollection</method>
          </magestyapps_ordergrid_prepare_collection>
      </observers>
  </sales_order_grid_collection_load_before>

And it's observer is:

public function prepareOrderGridCollection(Varien_Event_Observer $observer)
{
    $collection = $observer->getEvent()->getOrderGridCollection(); // grid collection

         $orderCollection = Mage::getResourceModel('myname_mygateway/createorder');
        $orderCollection->getSelect()
            ->reset(Zend_Db_Select::COLUMNS)
            ->columns(['soid' => 'storeoid', 'orderid']);

         $collection->getSelect()    
             ->joinLeft(['order' => $orderCollection->getSelect()],
                 'order.order_id = main_table.entity_id',
                 ['bankoid']); // just join the grid collection and the another in this case sales/order_coll, I'll have to replace later one.

     return $this;
}

I get error in backend :

Call to a member function getSelect() on null

If I can do joins and show result by directly using table's name then suggest me that as well. So please help me solve my problem.

1 Answer 1

1

I solved my problem by making following changes:

public function prepareOrderGridCollection(Varien_Event_Observer $observer)
{
    $collection = $observer->getEvent()->getOrderGridCollection(); // grid collection

$collection->getSelect()->joinLeft(
   'myname_mygateway_createorder', //joining table name
// note this join clause!
   'main_table.increment_id = myname_mygateway_createorder.storeoid',
   array('orderid')
);
var_dump((string)$collection->getSelect());
// die();

return $this;
}

And in the

public function addColumnsToGrid(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();

    // Check whether the loaded block is the orders grid block
    if (!($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid)
        || $block->getNameInLayout() != 'sales_order.grid'
    ) {
        return $this;
    }

    // Add a new column rigth after the "Ship to Name" column
    $block->addColumnAfter('orderid', [
        'header' => $block->__('Bank Order Id'),
        'index' => 'orderid',
    ], 'shipping_name');

    return $this;
}

Your Answer

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

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