Refresh order grid table in Magento 1

Recently, we’ve faced a data inconsistency in the Magento sales_flat_order_grid db table. Some of the values in custom columns look like they were shifted. This issue may occur if the order grid has multiple custom fields and when a user archives orders in the Enterprise Edition.

Thanks to this thread, we’ve developed a shell-script, which will refresh the sales_flat_order_grid table to correspond exactly to the sales_flat_order table:

<?php
/**
 * @author Atwix Team
 * @copyright Copyright (c) 2016 Atwix (https://www.atwix.com/)
 * @package Atwix_Shell
 */
require_once 'abstract.php';

class Atwix_Shell_Free_Order_Grid_Update extends Mage_Shell_Abstract
{
    public function run()
    {
        try {
            Mage::getModel('sales/order')->getResource()->updateGridRecords(
                Mage::getResourceModel('sales/order_collection')->getAllIds()
            );
            echo 'Done!' . PHP_EOL;
        } catch (Exception $e) {
            echo $e->getMessage() . PHP_EOL;
        }
    }
}

$shell = new Atwix_Shell_Free_Order_Grid_Update();
$shell->run();

You can run this script from your Magento installation root directory in the following way:

php shell/orders_grid_update.php

Pretty simple and really efficient. I hope, this little blog post will save you some time.

Thanks for reading!

You may also want to read:

  • Adding custom product attribute to quote and order items in Magento