neetw
neetw

Reputation: 83

Creating Order Item Grid in Magento

I am working on creating a Sales Order Item Grid in Magento 1.7. I have creted a new submenu under the Sales Menu in the admin for the same. The Order Item grid will display each product ordered in a new row. Hence there can be multiple OrderIds present in the grid. My query in the grid class is:

$collection = Mage::getResourceModel('sales/order_collection')
        ->join(
            'sales/order_item',
            '`sales/order_item`.order_id=`main_table`.entity_id',
            array(
                'skus'  => `sales/order_item`.`sku`,
                'names' => `sales/order_item`.`name`,
                'order_incharge' => `sales/order_item`.`order_incharge` ,
                'proptions' => `sales/order_item`.`product_options` ,

            ));

I do not have any GroupBy clause. When I print this query in the log, it appears as:

SELECT `main_table`.* FROM `sales_flat_order` AS `main_table` INNER JOIN `sales_flat_order_item` AS `sales/order_item` ON `sales/order_item`.order_id=`main_table`.entity_id

I have 2 orders in my database. Order Id =24 has two products Order Id 25 has one product So the above query when run on the database correctly displays 3 records. However this does not display on the grid. I get the following error when trying to display in the grid:

Item (Mage_Sales_Model_Order) with the same id "24" already exist";i:1;s:4274:"#0 C:\wamp\www\bakery\lib\Varien\Data\Collection\Db.php(576): Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order))

How do I resolve this issue, so that the same Order Id can be added to the collection?

Thanks, Neet

Upvotes: 3

Views: 2918

Answers (2)

Stefan
Stefan

Reputation: 9224

Thanks to Marshall for pointing me in the right direction. Here comes my solution, in case someone needs it

MY_MODULE_Block_Adminhtml_Order_Grid

protected function _getCollectionClass()
{
   // This is the model we are using for the grid
   // We need to work with the item collection and join the sales/order table because we wanna show one item per line
   return 'sales/order_item_collection';
}

protected function _prepareCollection()
{
    // Get the collection for the grid
    $collection = Mage::getResourceModel($this->_getCollectionClass())

    // Join the order table
    // The order item collection object already implements Mage_Core_Model_Mysql4_Collection_Abstract. 
    // That means, the join method doesn't need an array for the table and you don't need to get the table manually. 
    ->join('order', 'order_id=entity_id')

    // Get the address
    ->join(array('a' => 'sales/order_address'), 'order.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
        'city'       => 'city',
        'postcode'   => 'postcode',
        'region'     => 'region',
        'country_id' => 'country_id',
        'street'     => 'street',
        'telephone'  => 'telephone'
    ))

    // Get the customer group
    ->join(array('c' => 'customer/customer_group'), 'order.customer_group_id = c.customer_group_id', array(
            'customer_group_code' => 'customer_group_code'
    ))

    // Concat customer name
    ->addExpressionFieldToSelect(
        'customer_name',
        'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
        array('customer_firstname' => 'order.customer_firstname', 'customer_lastname' => 'order.customer_lastname'))
    ;

Upvotes: 0

Emery King
Emery King

Reputation: 3534

You should get the sales/order_item_collection and then join the sales/order table instead.

Your problem is that you are displaying order information per item on the order. Instead you should be getting the item information specifically and blending some order info in with a join.

Depending on what you want, you may not even need to join the sales/order table - just the sales/order_item collection may be all you need.

Upvotes: 2

Related Questions