Skip to content

Instantly share code, notes, and snippets.

@WinstonN
Created December 30, 2014 06:29
Show Gist options
  • Save WinstonN/509f5c7af3e65d3536cc to your computer and use it in GitHub Desktop.
Save WinstonN/509f5c7af3e65d3536cc to your computer and use it in GitHub Desktop.
<?php
require_once 'abstract.php';
/**
* Sync Order Grid shell script looks for orders
* that exist in the sales_flat_order table but not in the sales_flat_order_grid
* and using magento methods syncs them
*
* @author Winston Nolan <winston.nolan@gmail.com>
*/
class Sync_Order_Grid_Shell extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
if($this->getArg('run')) {
// run
if($this->canSync()) {
print "Starting Sync Order Grid Run\n";
$orderIds = $this->getOrders();
print sizeof($orderIds)." orders to sync\n";
foreach($orderIds as $orderId) {
print "RUN!!!::Starting Sync for Order with ID:".$orderId."\n";
$this->syncOrder($orderId);
}
} else {
print "The tables are in sync! Exit\n";
}
} elseif ($this->getArg('dryrun')) {
// dry run
if($this->canSync()) {
print "Starting Sync Order Grid Dry Run\n";
$orderIds = $this->getOrders();
print sizeof($orderIds)." orders to sync\n";
foreach($orderIds as $orderId) {
print "DRYRUN::Starting Sync for Order with ID:".$orderId."\n";
}
} else {
print "The tables are in sync! Exit\n";
}
} elseif ($this->getArg('check')) {
// do a check to see if sales_flat_order and sales_flat_order_grid tables are out of sync
$this->check();
} else {
// show help
echo $this->usageHelp();
}
}
/**
* Check if sales_flat_order and sales_flat_order_grid tables are out of sync
*/
public function check()
{
print "Starting Sync Order Grid Check\n";
$sales_flat_order_orders = Mage::getModel('sales/order')->getCollection()->getAllIds();
// sales_flat_order_grid
$sales_flat_order_grid_orders = Mage::getResourceModel('sales/order_grid_collection')->getAllIds();
print "\n";
print "sales_flat_order count = ".sizeof($sales_flat_order_orders)."\n";
print "sales_flat_order_grid count = ".sizeof($sales_flat_order_grid_orders)."\n";
print "\n";
if(sizeof($sales_flat_order_orders) != sizeof($sales_flat_order_grid_orders)) {
print "Sales Flat Order Grid is out of sync!\n";
print "difference = ".(sizeof($sales_flat_order_orders) - sizeof($sales_flat_order_grid_orders))."\n";
print "\n";
return true;
} else {
print "Sales Flat Order Grid is in sync!\n";
print "\n";
return false;
}
}
/**
* Check if we are allowed to sync
*/
public function canSync()
{
$canSync = $this->check();
if($canSync) {
return true;
} else {
return false;
}
}
/**
* Sync order from sales_flat_order to sales_flat_order_grid
*/
public function syncOrder($orderId)
{
// sync order to sales_flat_order_grid
Mage::getResourceSingleton('sales/order')->updateGridRecords($orderId);
}
/**
* Get orders to sync
*/
public function getOrders()
{
$sales_flat_order_orders = Mage::getModel('sales/order')->getCollection()->getAllIds();
// sales_flat_order_grid
$sales_flat_order_grid_orders = Mage::getResourceModel('sales/order_grid_collection')->getAllIds();
$result = array_diff($sales_flat_order_orders, $sales_flat_order_grid_orders);
return $result;
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f sync_order_grid.php -- [options]
check Check if the sales_flat_order and sales_flat_order_grid tables are out of sync
dryrun Dry run on the sync
run Sync sales_flat_order and sales_flat_order_grid
help This help
USAGE;
}
}
$shell = new Sync_Order_Grid_Shell();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment