Search code examples
magento-1.6

Magento – Programmatically reorder multiple orders by cronjob


I would like to programmatically reorder specific orders by cronjob and send an order confirmation by mail. I have a simple php file in my root directory which is triggered by a cronjob every once in a while:

<?php
include_once 'app/Mage.php';
Mage::app();

//some existing order ids
$orderIds= array('911', '1106', '926');

foreach($orderIds as $orderId){
    Mage::unregister('rule_data');
    Mage::getModel('adminhtml/session_quote')
        ->clear();

    /* @var Mage_Sales_Model_Order $order */
    $order = Mage::getModel('sales/order')->load($orderId)
        ->setReordered(true);

    /* @var Mage_Sales_Model_Quote $quote */
    $quote = Mage::getModel('sales/quote')
        ->setStoreId($order->getStoreId())
        ->assignCustomer(Mage::getModel('customer/customer')->load($order->getCustomerId()))
        ->setUseOldShippingMethod(true);

    /* @var Mage_Adminhtml_Model_Sales_Order_Create $model */
    $model = Mage::getModel('adminhtml/sales_order_create')
        ->initFromOrder($order)
        ->setQuote($quote);

    /* @var Mage_Sales_Model_Order $newOrder */
    $newOrder = $model->createOrder();
    $newOrder->setQuoteId($quote->getId())
        ->sendNewOrderEmail();

    $model->getSession()
        ->clear();
}

It seems to work so far, new orders are placed and the emails are sent. But the problem I experienced is that the customer in the new orders is always the one from the first old order (in this case the order with the id 911). Of course, this also affects the order confirmation emails, so they're all sent to the same email address... Also, the order items seem to add up in the cart, so the last order which is reordered contains all the order items of the previous orders... What am I doing wrong?

I appreciate every help I can get! Thank you!


Solution

  • There are two problems:

    1. You are calling Mage::getModel('adminhtml/session_quote')->clear(), which gets a new instance of the session and tries to clear that. But the Mage_Adminhtml_Model_Sales_Order_Create class gets the session by using Mage::getSingleton('adminhtml/session_quote'), which always gets the same instance from the Magento registry. So you are trying to clear another session instance.
    2. Even if you would try to clear the right session instance from the Magento registry by using: Mage::getSingleton('adminhtml/session_quote')->clear(), there would still be a problem. As the Mage_Adminhtml_Model_Session_Quote class doesn't define this method, it ultimately calls Varien_Object->unsetData(), which only does this: $this->_data = array();

      But the information in the Mage_Adminhtml_Model_Session_Quote class is stored in the $_quote, $_customer, $_store and $_order properties, so they don't actually get cleared, but persist.

    I solved the problem by deleting the Mage_Adminhtml_Model_Session_Quote instance in Magento registry before each of the orders, so that it needs to create a new one for each order.

    Just add this at the beginning of the loop:

    Mage::unregister('_singleton/adminhtml/session_quote');
    

    Cheers! ;)