Hello,
I was asked to fix a “simple” problem in Magento website (in admin).
There is this table with customized orders, where Status, Department and Owner are listed.
When Status changes, the duo Dept->Owner is automatically updated as well (each Status belong to a different Dept).
When the function below is used on a single order it works perfectly. When it is used on a group of orders (bulk) only THE FIRST one is fully updated ( I mean Status, Dept and Owner ), the rest is udpated ONLY with the new Status.
As I am a complete n00b in Magento, I don’t even get HOW this bloody function works:
public function changeStatusAction()
{
$params = $this->getRequest()->getParams();
$skipUpdateOwnerOriginal = Mage::registry('skip_order_owner_after_save') ?: false;
Mage::unregister('skip_order_owner_after_save');
Mage::register('skip_order_owner_after_save', false);
$data = (empty($params['data'])
? array(array('new_status' => $params['new_status'], 'order_ids' => $params['order_ids']))
: $params['data']
);
try {
/** @var $transaction Mage_Core_Model_Resource_Transaction */
$transaction = Mage::getModel('core/resource_transaction');
$added = true;
foreach ($data as $record) {
$newStatus = $record['new_status'];
foreach ($record['order_ids'] as $orderId) {
/** @var $order Mage_Sales_Model_Order */
$order = Mage::getModel('sales/order')->load((int)$orderId);
$order->setStatus($newStatus);
$transaction->addObject($order);
$added = true;
}
}
if ($added)
$transaction->save();
Mage::getSingleton('core/session')->addSuccess('Updated statuses successfully.');
} catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Failed to update status.');
Mage::logException($e);
}
if ($skipUpdateOwnerOriginal) {
Mage::unregister('skip_order_owner_after_save');
Mage::register('skip_order_owner_after_save', $skipUpdateOwnerOriginal);
}
$this->_redirect('*/*');
}
Could anyone shed some light into my problem?
Thanks in advance!
Regards,
Greg