Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Magento, how do I populate a new column in sales_order_grid with data from sales_flat_order?

Tags:

magento

I am following Ivan's tutorial (Adding order attribute to the orders grid in Magento 1.4.1) to add an extra column to the sales_order_grid table (the Shipping Description text) and it's working except it's not bring old data across from sales_flat_order to the new column in sales_order_grid.

My SQL install script adds the column correctly and because I'm using the same field name as in sales_flat_order I don't think I need an observer, however the code to import all of the existing shipping description data into the shipping_description field isn't running.

What I am doing wrong?

My SQL install script:

<?php
/**
 * Setup scripts, add new column and fulfills
 * its values to existing rows
 *
 */
/* @var $this Mage_Sales_Model_Mysql4_Setup */
$this->startSetup();

// Add column to grid table
$this->getConnection()->addColumn(
    $this->getTable('sales/order_grid'),
    'shipping_description',
    "varchar(255) not null default ''"
);

// Add key to table for this field,
// it will improve the speed of searching & sorting by the field
$this->getConnection()->addKey(
    $this->getTable('sales/order_grid'),
    'shipping_description',
    'shipping_description'
);


// fill existing rows with data
$select = $this->getConnection()->select();
$select->join(
    array('order'=>$this->getTable('sales/order')),
    $this->getConnection()->quoteInto('order.entity_id = order_grid.entity_id',''),
    array('shipping_description' => 'shipping_description')
);

$this->getConnection()->query(
    $select->crossUpdateFromSelect(
        array('order_grid' => $this->getTable('sales/order_grid'))
    )
);

$this->endSetup();

I am using Magento 1.5.1 Community Edition!

Thanks for any assistance!

like image 920
simonyoung Avatar asked Jun 26 '11 07:06

simonyoung


1 Answers

this should work:

$select = $this->getConnection()->select();
$select->join(
    array('order_shipping'=>$this->getTable('sales/order')),//alias=>table_name
    $this->getConnection()->quoteInto(
        'order_shipping.entity_id = order_grid.entity_id',
        Mage_Sales_Model_Quote_Address::TYPE_SHIPPING
    ),//join clause
    array('shipping_description' => 'shipping_description')//fields to get
);
$this->getConnection()->query(
    $select->crossUpdateFromSelect(
        array('order_grid' => $this->getTable('sales/order_grid'))
    )
);

If you want, take a look at my extension :)
HTH

like image 74
OSdave Avatar answered Oct 05 '22 11:10

OSdave