1

Further from yesterday's 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).

1

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');
}
| 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.