Answers.Body: <p>Simple error on my part. You don't pass the old order's "shippingMethod" model to the new cart's 'setShippingMethod' function. You pass the old order's shippingMethodHandle to the setShippingMethod function.</p>
  
  <p>The following works:</p>
  
  <pre><code>public function actionReorder()
  {
      // basic security precautions
          $this-&gt;requirePostRequest();
  
      // get the current cart
          $cart = craft()-&gt;commerce_cart-&gt;getCart();
  
      // get the fields passed to us in the form
          $oldOrderNumber = craft()-&gt;request-&gt;getPost('oldOrderNumber');
  
      // get the 'old' order
          $oldOrder = craft()-&gt;commerce_orders-&gt;getOrderByNumber($oldOrderNumber);
  
      // set the shipping etc on the cart to the same as the old order
          $cart-&gt;shippingAddressId    = $oldOrder-&gt;shippingAddressId;
          $cart-&gt;shippingAddress      = $oldOrder-&gt;shippingAddress;
  
  
          if (!craft()-&gt;commerce_cart-&gt;setShippingMethod($cart, $oldOrder-&gt;shippingMethodHandle, $error)) {
              $cart-&gt;addError('shippingMethod', $error);
              LogicPlugin::log("error adding shipping method to cart: " . $error, LogLevel::Error);
          }
  
      // ok now pass it all over to the multiAdd controller...
          craft()-&gt;runController('multiAdd/multiAdd');
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-03T12:40:01.667Z
Answers.LastActivityDate: 2016-11-03T12:40:01.667Z
Answers.UserDisplayName: Matt Wilcox
Body: <p>Further from yesterday's <a href="https://craftcms.stackexchange.com/questions/17534/duplicate-an-existing-order-as-a-new-cart-in-commerce">Duplicate an existing Order as a new Cart in Commerce</a> I need to step in front of MultiAdd to set up a couple of other things - such as to duplicate the Shipping details from the old order to the new cart.</p>
  
  <p>I have been trying to do it as follows:</p>
  
  <pre><code>public function actionReorder()
  {
      // basic security precautions
          $this-&gt;requirePostRequest();
  
      // get the current cart
          $cart = craft()-&gt;commerce_cart-&gt;getCart();
  
      // get the fields passed to us in the form
          $oldOrderNumber = craft()-&gt;request-&gt;getPost('oldOrderNumber');
  
      // get the 'old' order
          $oldOrder = craft()-&gt;commerce_orders-&gt;getOrderByNumber($oldOrderNumber);
  
      // set the shipping etc on the cart to the same as the old order
          $cart-&gt;shippingAddressId    = $oldOrder-&gt;shippingAddressId;
          $cart-&gt;shippingAddress      = $oldOrder-&gt;shippingAddress;
          $cart-&gt;shippingMethodHandle = $oldOrder-&gt;shippingMethodHandle;
  
      // ok now pass it all over to the multiAdd controller...
          craft()-&gt;runController('multiAdd/multiAdd');
  }
  </code></pre>
  
  <p>This doesn't work because </p>
  
  <blockquote>
    <p>Property "Craft\Commerce_OrderModel.shippingMethodHandle" is read only.</p>
  </blockquote>
  
  <p>I have also tried</p>
  
  <pre><code>if (!craft()-&gt;commerce_cart-&gt;setShippingMethod($cart, $oldOrder-&gt;shippingMethod, $error)) {
      $cart-&gt;addError('shippingMethod', $error);
      LogicPlugin::log("error adding shipping method to cart: " . $error, LogLevel::Error);
  }
  </code></pre>
  
  <p>instead of $cart->shippingMethodHandle = $oldOrder->shippingMethodHandle;</p>
  
  <p>But that doesn't appear to work either. It tells me that the Shipping method not available.</p>
  
  <p>How should I go about doing this?</p>
  
  <p>It's particularly problematic for me because my Commerce set up required the Shipping address to be set in order for the Product Options to work (the options are based on the address the cart will ship to).</p>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: Duplicating shipping details from an Order into a Cart
ThreadText: # Duplicating shipping details from an Order into a Cart
  
  (Asked by: username_0 on 2016-11-03)
  
  Further from yesterday's [Duplicate an existing Order as a new Cart in Commerce](https://craftcms.stackexchange.com/questions/17534/duplicate-an-existing-order-as-a-new-cart-in-commerce) I need to step in front of MultiAdd to set up a couple of other things - such as to duplicate the Shipping details from the old order to the new cart.
  
  I have been trying to do it as follows:
  
  ```
  public function actionReorder()
  {
      // basic security precautions
          $this->requirePostRequest();
  
      // get the current cart
          $cart = craft()->commerce_cart->getCart();
  
      // get the fields passed to us in the form
          $oldOrderNumber = craft()->request->getPost('oldOrderNumber');
  
      // get the 'old' order
          $oldOrder = craft()->commerce_orders->getOrderByNumber($oldOrderNumber);
  
      // set the shipping etc on the cart to the same as the old order
          $cart->shippingAddressId    = $oldOrder->shippingAddressId;
          $cart->shippingAddress      = $oldOrder->shippingAddress;
          $cart->shippingMethodHandle = $oldOrder->shippingMethodHandle;
  
      // ok now pass it all over to the multiAdd controller...
          craft()->runController('multiAdd/multiAdd');
  }
  
  ```
  
  This doesn't work because
  
  > Property "Craft\Commerce\_OrderModel.shippingMethodHandle" is read only.
  
  I have also tried
  
  ```
  if (!craft()->commerce_cart->setShippingMethod($cart, $oldOrder->shippingMethod, $error)) {
      $cart->addError('shippingMethod', $error);
      LogicPlugin::log("error adding shipping method to cart: " . $error, LogLevel::Error);
  }
  
  ```
  
  instead of $cart->shippingMethodHandle = $oldOrder->shippingMethodHandle;
  
  But that doesn't appear to work either. It tells me that the Shipping method not available.
  
  How should I go about doing this?
  
  It's particularly problematic for me because my Commerce set up required the Shipping address to be set in order for the Product Options to work (the options are based on the address the cart will ship to).
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-11-03. Score: 1 | Accepted answer
  
  Simple error on my part. You don't pass the old order's "shippingMethod" model to the new cart's 'setShippingMethod' function. You pass the old order's shippingMethodHandle to the setShippingMethod function.
  
  The following works:
  
  ```
  public function actionReorder()
  {
      // basic security precautions
          $this->requirePostRequest();
  
      // get the current cart
          $cart = craft()->commerce_cart->getCart();
  
      // get the fields passed to us in the form
          $oldOrderNumber = craft()->request->getPost('oldOrderNumber');
  
      // get the 'old' order
          $oldOrder = craft()->commerce_orders->getOrderByNumber($oldOrderNumber);
  
      // set the shipping etc on the cart to the same as the old order
          $cart->shippingAddressId    = $oldOrder->shippingAddressId;
          $cart->shippingAddress      = $oldOrder->shippingAddress;
  
          if (!craft()->commerce_cart->setShippingMethod($cart, $oldOrder->shippingMethodHandle, $error)) {
              $cart->addError('shippingMethod', $error);
              LogicPlugin::log("error adding shipping method to cart: " . $error, LogLevel::Error);
          }
  
      // ok now pass it all over to the multiAdd controller...
          craft()->runController('multiAdd/multiAdd');
  }
  
  ```
  
  
  ---
  
  