1

I'm trying to create a new order, based on an old order.

$orig_order = Mage::getModel('sales/order')->load((int)$order_id);
        $order_model = Mage::getSingleton('adminhtml/sales_order_create');

        try {
            $orig_order->setReordered(true); 
            $reorder = $order_model->initFromOrder($orig_order); 
            $reorder->getQuote()->getShippingAddress()->collectTotals(); 
            $reorder->getQuote()->getShippingAddress()->save(); 
            $reorder->createOrder();
        } catch (Exception $e) {

        }

This doesn't do anything, but is the only reference I can find to programatically reordering. Any ideas?

  • Do you have to recapture the payment information too? – kab8609 Dec 23 '13 at 15:21
  • Is the reordering option activated in your backend ? – dagfr Dec 23 '13 at 15:57
4

Theres information missing, however I will answer it using assumptions.

Using a clean install of Magento CE, I attempted to use the code above to recreate a existing complete order. Some of the assumptions I made are:

Existing Completed Order

  • One product ordered
  • Billing/Shipping address the same
  • Payment Method: Check/Money Order
  • Shipping Method: Flat rate

For simplicity, I created a file in my root with your code (yours would be in your extension). This file contained:

<?php
include_once 'app/Mage.php';
Mage::app();
$orderId= '1'; //You were missing the Order ID in your code (Issue #1)
$orig_order = Mage::getModel('sales/order')->load((int)$orderId);
$order_model = Mage::getSingleton('adminhtml/sales_order_create');
try {
   $orig_order->setReordered(true);
   $reorder = $order_model->initFromOrder($orig_order);
   $reorder->getQuote()->getShippingAddress()->collectTotals();
   $reorder->getQuote()->getShippingAddress()->save();
   $reorder->createOrder();
} catch (Exception $e) {
}

When I ran this script, I didn't get an error message. However if you get the error message below, most likely you are using the wrong order ID (hint entity_id = order ID)

PHP Fatal error: Call to a member function getData() on a non-object in app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php on line 333

Speeding this up a little, I decided to rewrite the script, and soon after I started to get this error:

PHP Fatal error: Uncaught exception 'Varien_Exception' with message 'Invalid method Mage_Sales_Model_Order_Address::collectShippingRates

So I had to put some additional logic in here for shipping and I ended up with:

<?php
include_once 'app/Mage.php';
Mage::app();
$orderId= '1'; //Think of entity_id, not the preformatted ID of 1000000001
$order = Mage::getModel('sales/order')->load($orderId);
$model = Mage::getSingleton('adminhtml/sales_order_create');
$order->setReordered(true);
$reorder = $model->initFromOrder($order);
//We needed additional logic for shipping rates
$reorder->getQuote()->getShippingAddress()
        ->setShippingMethod($order->getShippingMethod())
        ->setCollectShippingRates(true)
        ->collectShippingRates()
        ->collectTotals()
        ->save();
$reorder->createOrder();

I ran this two times and didn't see any errors. So I decided to go into the admin panel and look at my sales and I saw:

Orders

| improve this answer | |

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.