I found this solution by Igor Furseev at Atwix, The full code is:
<?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();
which you save to a file in the shell directory of your Magento installation and then execute with php from the command line with php shell/orders_grid_update.php
.
I've taken a bit of time to analyse this code, to try and reassure myself that I'm not just implementing a random solution from the internet, which may or may not be right.
The above code gets a Magento collection object for all orders in the system, uses it to generate a list of all the entity IDs (not the same as order ids), which it then passes to Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords
.
The Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords
method is the method Magento uses itself to update the grid records any time a sales/order
object is saved. The save method is defined higher up the models hierarchy in Mage_Core_Model_Abstract::save
, where the method generates a call to Mage_Sales_Model_Abstract::afterComitCallback
, which in turn calls the Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords
method.
Looking at Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords
, we can see that it builds an insert select query, which will insert (or update) data in sales_flat_order_grid
based on data from from a select query which joins together sales_flat_order
and sales_flat_order_address
(actually joins twice onto sales_flat_order_address
to get both billing and shipping addresses associated with the order).
I tried the above on a copy of a store and then diffed the databases before and afterwards. The only tables which had changed was sales_flat_order_grid
as I'd expected. It had modified this table to sort out the orders which were in an inconsistent state.
The only other change the database diff reveated were some double spaces between the firstname and surname on both the billing and shipping addresses. I tracked this down to a code change in Mage_Sales_Model_Resource_Order::_initVirtualGridColumns
sometime between version 1.6 and 1.9. The earlier version just concatenated a customers first and surname, but the later version concatenated a customers first, middle and surname but mistakenly appeneded a space to both first and middle names during the concatenation, even if the middle name was not present, hence the double space.
I'm not to concerned if the above code messes up the sales_flat_order_grid
table as this answer says that the grid table is just for presentation to the admin, and is not the source of truth about an order.
sales_flat_order_grid
immediately, but later they appear. Have you tried reindexing all?php shell/indexer.php info
and run them all withphp shell/indexer.php reindexall
.