Magento 'core/resource_transaction' Model

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

A word of advice.

Nothing is ever simple in Magento.

You need to understand the code before you can fix it. The best way to do that with a monolithic platform like Magento is to use smart debugging. Using smart debugging you will able to step through the code line by line as it executes to better understand what is happening which should lead to a solution to you’re problem.

Furthermore, if you need to make core modifications they should be made as rewrites NOT hacking at the core classes since that decreases the chances of compatibility with future updates.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.