0

I would like to programmatically create new orders from existing orders by their id and send new order confirmations by mail. The new orders need to contain all the information the old ones had (Items, Customer, Shipping Information etc.):

<?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();
}

Unfortunately Magento keeps the Customer information while looping through the order IDs, so the emails are all sent to the customer of the first order (in this case the one with the id 911). Also, the order items seem to add up in the cart, so the last order which is placed contains all the order items of the previous orders... What am I doing wrong?

Sourav
1,2988 silver badges16 bronze badges
asked Jan 9, 2015 at 13:58
5
  • Bledert, I am not really sure what the question is that you are asking. Can you please clarify? You want to reorder orders by id but do not want the order details to remain the same? Thanks Commented Jan 9, 2015 at 14:07
  • No, the order details can stay the same! It works perfectly if I only have one order ID in $orderIds, but as soon as there are mutiple order ids, the customer in the following orders remain the same :( Commented Jan 9, 2015 at 15:13
  • From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer(). Commented Jan 10, 2015 at 9:46
  • Yep, that's what I thought... But $quote->getCustomerEmail() actually gives me the correct email, but $newOrder->getCustomerEmail() doesn't. So I think $model->setQuote() doesn't really work after $model is initiated by $order. Any other suggestions to clear the object cache? Commented Jan 12, 2015 at 9:32
  • 1
    So really nobody has an answer on that? :( Commented Jan 15, 2015 at 9:06

2 Answers 2

0

Magento uses lots of singletons and assumes that certain actions are only executed once.

If you take a look at Mage_Adminhtml_Model_Sales_Order_Create, you will find this one:

public function __construct()
{
 $this->_session = Mage::getSingleton('adminhtml/session_quote');
}

You can reset singletons like this:

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

If it still does not work, walk through the code, there might be other relevant singletons.

answered Sep 3, 2015 at 19:31
0

Try bellow script

<?php
require_once('app/Mage.php');
Mage::app('default');
class ORDERCREATE
{
 public function index() 
 {
 //some existing order ids
 $orderIds= array('911', '1106', '926');
 foreach($orderIds as $orderId) {
 $order = Mage::getModel('sales/order')->load($orderId);
 if (!$order->getId()) {
 echo 'Invalid order id'.$orderId;
 exit;
 }
 $quoteId = $order->getQuoteId();
 $storeId = $order->getStoreId();
 $quote = Mage::getModel("sales/quote")
 ->setStoreId($storeId)
 ->load($quoteId);
 try {
 $quote->collectTotals();
 /** @var $service Mage_Sales_Model_Service_Quote */
 $service = Mage::getModel('sales/service_quote', $quote);
 $service->submitAll();
 $order = $service->getOrder();
 if ($order) {
 try {
 $order->sendNewOrderEmail();
 } catch (Exception $e) {
 Mage::logException($e);
 }
 }
 } catch (Mage_Core_Exception $e) {
 $e->getMessage();
 }
 echo $order->getIncrementId();
 }
 }
}
$obj = new ORDERCREATE();
$obj->index();
?> 
answered Dec 11, 2015 at 5:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.