2

In my Magento custom module I'm trying to go through array of some orders and re-order each. However, I'm facing strange issue.

The first order from that array is well reordered, but all other orders are not reordered correctly. Precisely, items from my first order are also copied to second order, then items from first and second orders are copied to third order, and so on. Moreover, all Order Total prices (Subtotal, Shipping & Handling, Tax, Grand Total, ...) are zero (for all orders except for the first one which are correct).

This is the code i have so far:

$orderIds = /* ARRAY OF ORDER ID's */
foreach($orderIds as $orderId) {

        $origOrder = Mage::getModel('sales/order')->load($orderId);

        // clear the session data
        Mage::getSingleton('adminhtml/session_quote')->clear();
        Mage::unregister('rule_data');

        $orderModel = Mage::getSingleton('adminhtml/sales_order_create');
        //I also tried $orderModel = Mage::getModel('adminhtml/sales_order_create'); but without success 

        $orderModel->getSession()->clear();
        $origOrder->setReordered(true);
        $orderModel->setRecollect(true)->saveQuote();
        Mage::getSingleton('adminhtml/session_quote')->setUseOldShippingMethod(true);

        $reorder = $orderModel->initFromOrder($origOrder);

        try {
            $reorder->createOrder();
            $orderModel->getSession()->clear();
        } catch (Exception $e) {
            Mage::log("Not Reordered! Error: ".$e->getMessage());
        }
}

Similar question has been raised here, but without answer: Auto reorder loop - Second order errors out

I'm using Magento versions 1.6 and 1.7. I would greatly appreciate any help!

  • Hi @stefan have you got any solution ? I am also in the same situation now – AnNaMaLaI Apr 30 '15 at 9:47
  • Hi @annamalai-somasundaram. I solved this by creating a cron task that runs every 2 minutes or so, then check if there is an order that should be reordered, and in that case the cron job reorder the first such order. It acts like delayed for-loop. – Stefan Stanimirovic May 4 '15 at 10:58
2

I had same problem. But i resolved this by resetting new quote to adminhtml quote session. So it will not take items from previous orders.

Here is your solution.

foreach($orders as $_order){
    $orderId = $_order->getId();

    Mage::unregister('rule_data');
    Mage::getSingleton('adminhtml/session_quote')->clear();

    $order = Mage::getModel('sales/order')->load($orderId);
    $incId = $order->getIncrementId();

    $newQuote = new Mage_Sales_Model_Quote();
    $newQuote->setStoreId($order->getStoreId());
    Mage::getSingleton('adminhtml/sales_order_create')->setQuote($newQuote);

    $order_model = Mage::getSingleton('adminhtml/sales_order_create');
    $order_model->getSession()->clear();

    try {
        $order->setReordered(true);
        Mage::getSingleton('adminhtml/session_quote')->setUseOldShippingMethod(true);

        $reorder = new Varien_Object();
        $reorder = $order_model->initFromOrder($order);
        $newOrder = $reorder->createOrder();

        $reOrderId = $newOrder->getId();
        $reOrderIncId = $newOrder->getIncrementId();
        Mage::log("Order #{$incId} is Reorders To New Order #{$reOrderIncId} Successfully",null,"reorder.log");
    } catch (Exception $e) {
        Mage::log("Order #{$incId} Reorder Error : {$e->getMessage()}",null,"reorder.log";
    }
    $reorder->getSession()->clear();
    Mage::unregister('rule_data');
    Mage::getSingleton('adminhtml/session_quote')->clear();
}
  • 1
    his code doesn't seem work correctly for me. these is a problem with intcrement IDs. Magento gives such an error "Order #100000173 Reorder Error : Mage registry key "_singleton/sale/observer" already exists" if the second item in the loop will be reordered. the code was tested in Magento 1.9. by userXXXX – Fabian Blechschmidt Jul 28 '14 at 10:02
  • 1
    This code doesn't work for me either, but I don't get errors, just problem stays the same (if I remove $order->setReordered(true); from code, I get the same error as @fabian) – Stefan Stanimirovic Jul 29 '14 at 8:08
  • 1
    Hi, You can try with "Model" instead of "Singleton" for Quote session object. Example : Mage::getModel('adminhtml/session_quote')->clear(); Mage::getModel('adminhtml/sales_order_create')->setQuote($newQuote); – Pritesh Modi Aug 12 '14 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.