Skip to content

Instantly share code, notes, and snippets.

@LeeSaferite
Created October 2, 2015 13:24
Show Gist options
  • Save LeeSaferite/4eab06bed2f5a8dbf140 to your computer and use it in GitHub Desktop.
Save LeeSaferite/4eab06bed2f5a8dbf140 to your computer and use it in GitHub Desktop.
Code to queue sales grid updates
<?php
class Aoe_QueuedOperations_Model_Observer
{
/**
* This is used to queue the sales grid updates
*
* NB: This should be attached to the sales_order_resource_update_grid_records event
*
*
* @see Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords
*/
public function queueSalesGridUpdate(Varien_Event_Observer $observer)
{
if (!Mage::getStoreConfigFlag('aoe/queued_operations/sales_grid_updates')) {
return;
}
/** @var Varien_Object $proxy */
$proxy = $observer->getProxy();
if (!$proxy instanceof Varien_Object) {
return;
}
/** @var Mage_Sales_Model_Resource_Order_Abstract $resource */
$resource = $proxy->getResource();
if ($resource instanceof Mage_Sales_Model_Resource_Order) {
$resourceName = 'sales/order';
} else {
return;
}
$ids = $proxy->getIds();
if (!is_array($ids)) {
return;
}
$ids = array_filter(array_map('intval', $ids));
if (empty($ids)) {
return;
}
try {
/** @var Aoe_Queue_Model_Queue $queue */
$queue = Mage::getModel('aoe_queue/queue', 'sales_grid_updates');
$queue->addTask('Aoe_QueuedOperations/Observer::processSalesGridUpdate', array($resourceName, $ids));
$proxy->setIds(array());
} catch (Exception $e) {
Mage::logException($e);
}
}
/**
* This is used to processed queued sales grid updates
*
* @param string $resource
* @param int[] $ids
*
* @see Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords
* @see Aoe_QueuedOperations_Model_Observer::queueSalesGridUpdate
*/
public function processSalesGridUpdate($resource, array $ids)
{
if (empty($resource) || empty($ids)) {
Mage::throwException('Invalid callback parameters');
}
/** @var Mage_Sales_Model_Resource_Order_Abstract $resource */
$resource = Mage::getResourceModel($resource);
if (!$resource instanceof Mage_Sales_Model_Resource_Order_Abstract) {
Mage::throwException('Invalid resource parameter');
}
$ids = array_filter(array_map('intval', $ids));
if (empty($ids)) {
return;
}
$columnsToSelect = array();
$table = $resource->getGridTable();
$select = $resource->getUpdateGridRecordsSelect($ids, $columnsToSelect);
$select->getAdapter()->query($select->insertFromSelect($table, $columnsToSelect, true));
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment