Programmatically create new orders from multiple existing orders












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?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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
    – philcollin_us
    Jan 9 '15 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 :(
    – biedert
    Jan 9 '15 at 15:13










  • From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer().
    – Melvyn
    Jan 10 '15 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?
    – biedert
    Jan 12 '15 at 9:32






  • 1




    So really nobody has an answer on that? :(
    – biedert
    Jan 15 '15 at 9:06
















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?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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
    – philcollin_us
    Jan 9 '15 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 :(
    – biedert
    Jan 9 '15 at 15:13










  • From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer().
    – Melvyn
    Jan 10 '15 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?
    – biedert
    Jan 12 '15 at 9:32






  • 1




    So really nobody has an answer on that? :(
    – biedert
    Jan 15 '15 at 9:06














0












0








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?










share|improve this question















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?







magento-1.9 orders magento-1.6 programmatically






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 20 '18 at 4:58









Sourav

1,109413




1,109413










asked Jan 9 '15 at 13:58









biedert

12




12





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 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
    – philcollin_us
    Jan 9 '15 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 :(
    – biedert
    Jan 9 '15 at 15:13










  • From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer().
    – Melvyn
    Jan 10 '15 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?
    – biedert
    Jan 12 '15 at 9:32






  • 1




    So really nobody has an answer on that? :(
    – biedert
    Jan 15 '15 at 9:06


















  • 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
    – philcollin_us
    Jan 9 '15 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 :(
    – biedert
    Jan 9 '15 at 15:13










  • From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer().
    – Melvyn
    Jan 10 '15 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?
    – biedert
    Jan 12 '15 at 9:32






  • 1




    So really nobody has an answer on that? :(
    – biedert
    Jan 15 '15 at 9:06
















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
– philcollin_us
Jan 9 '15 at 14:07




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
– philcollin_us
Jan 9 '15 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 :(
– biedert
Jan 9 '15 at 15:13




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 :(
– biedert
Jan 9 '15 at 15:13












From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer().
– Melvyn
Jan 10 '15 at 9:46




From the top of my head, the current customer has an object cache. Should become apparent when you trace assignCustomer().
– Melvyn
Jan 10 '15 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?
– biedert
Jan 12 '15 at 9:32




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?
– biedert
Jan 12 '15 at 9:32




1




1




So really nobody has an answer on that? :(
– biedert
Jan 15 '15 at 9:06




So really nobody has an answer on that? :(
– biedert
Jan 15 '15 at 9:06










2 Answers
2






active

oldest

votes


















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.






share|improve this answer





























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





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f51706%2fprogrammatically-create-new-orders-from-multiple-existing-orders%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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.






      share|improve this answer


























        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.






        share|improve this answer
























          0












          0








          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 3 '15 at 19:31









          Fabian Schmengler

          54k20127337




          54k20127337

























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





              share|improve this answer


























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





                share|improve this answer
























                  0












                  0








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





                  share|improve this answer












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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 11 '15 at 5:19









                  Abdul

                  7,97511135




                  7,97511135






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f51706%2fprogrammatically-create-new-orders-from-multiple-existing-orders%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      An IMO inspired problem

                      Management

                      Has there ever been an instance of an active nuclear power plant within or near a war zone?