1

I'm querying for specific memberships that were previously ordered and reordering them via the admin reorder functions. However, when the foreach goes through a first full cycle and successfully places a reorder, the second cycle starts and tries to reorder the second order and fails because some of the original order data exists even though the next order data has been set already.

This is what I am trying to do:

foreach($memberpackageCollection as $mpcKey => $memberpackage) {
    try {
        $orderId = $memberpackage->getOrderIds();
        $personsOrder = Mage::getModel('sales/order')->load($orderId);

        /* Set different payment method */
        $personsOrder->getPayment()->setMethod('checkmo');
        $personsOrder->getPayment()->setMethodInstance(
            Mage::getModel('payment/method_checkmo')->setInfoInstance(
            $personsOrder->getPayment()->getMethodInstance()->getInfoInstance()));

        Mage::unregister('rule_data');
        $orderModel = Mage::getSingleton('adminhtml/sales_order_create');
        $orderModel->initFromOrder($personsOrder->setReordered(true));
        $orderModel->createOrder();
    }
    catch (Exception $e) {
        Mage::logException($e);
    }
}

Error occurs after second time it loops.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '100006914-1' for key 'UNQ_XYYXSALES_FLAT_ORDER_INCREMENT_ID''

I understand that the error is produced because there is a duplicate increment ID. This is because it tries to reorder on the original order and not on the next one. As you can see from the code I show, I am loading a new order and trying to create a reorder on that order. Somehow I need to clear the session data that appears to be set in the model adminhtml/sales_order_create that seems to remaining in there for each order.

2
  • Did you try to use Mage::getModel() instead of Mage::getSingleton('adminhtml/sales_order_create')? Commented Dec 7, 2013 at 23:37
  • Mage::getModel() would make more sense in this case. I've replace it with getModel(), and it seems to be doing the exact same thing. Commented Dec 9, 2013 at 6:02

1 Answer 1

1

Add this at the end of your try block:

Mage::getSingleton('adminhtml/session_quote')->clear();

For some reason the backend reordering system works with sessions. This will clear the session data and let you start fresh on your second iteration.

2
  • hi, maybe this is also relevant, seems that you can only reorder a old order only one time. If you take a look at createOrder()-Method: 'increment_id' => $originalId.'-.$oldOrder->getEditIncrement()+1) Commented Oct 29, 2014 at 9:41
  • 1
    @StefanBothner, you can reorder/edit an order as many times you want. The EditIncrement field is incremented and kept on all parent/child orders at same max increment value. There should be no problem in reordering on the original order, and not on a child of it. Commented Mar 10, 2015 at 21:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.