2

Here's the scenario. For example $oldOrder has an increment ID of 100000044

if($oldOrder->canCancel()) {
            $order->cancel();   
            $order->save();
}

When I call the above, for some reason, it's messing up the increment ID, which is becoming 000000062. In addition, if I create a new order, it will have the same increment ID of the old ordder, which is 100000044, even though the increment ID is assumed to be unique.

This is causing an issue, as the increment ID might have been used in a call to the 3rd party payment gateway previously, and linked from their side to the old order, as the increment ID is supposed to be unique to each order.

Am I doing something wrong? How do I fix this? I noticed that the increment ID doesn't mess up when I manually cancel it from the admin page, instead of the php script..

I'm creating an order the following way:

    $billingAddress = $quote->getBillingAddress()->addData($billingAddress);
    $shippingAddress = $quote->getShippingAddress()->addData($shippingAddress);

    $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                    ->setShippingMethod('flatrate_flatrate')
                    ->setPaymentMethod('purchaseorder');
    $quote->setBillingAddress($billingAddress);
    $quote->setShippingAddress($shippingAddress);
    $quote->getPayment()->importData(array('method' => 'purchaseorder'));
    $quote->collectTotals()->save();
    $quote->setReservedOrderId(null);  //----This solved the increment issue.
    $quote->reserveOrderId();
    $quote->save();

    $service = Mage::getModel('sales/service_quote', $quote);
    $service->submitAll();
    $order = $service->getOrder();
    // Mage::getSingleton('checkout/session')->setOrderId($order->getId());
    //$quote->setIsActive(0)->save();
    return $order;
2
  • 1
    this should not happen by default. Do you have any extensions that might affect the orders?
    – Marius
    Commented Jul 23, 2015 at 14:17
  • 1
    The answer by Iloiacono solved it. I needed to unset it first before creating a new order.
    – Pat
    Commented Jul 23, 2015 at 14:47

1 Answer 1

4

If you are creating the orders programmatically then is possible that you forgot the get a new reservedIncrementId for the quote. The quote is reserving an increment id, and later when you convert the quote to an order will use that reserved increment id, if you cancel the order but then use the same quote for a new order you need to get a new increment id.

$quote->reserveOrderId();

//code extracted from Mage_Sales_Model_Quote
public function reserveOrderId()
{
    if (!$this->getReservedOrderId()) {
        $this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
    } else {
        //checking if reserved order id was already used for some order
        //if yes reserving new one if not using old one
        if ($this->_getResource()->isOrderIncrementIdUsed($this->getReservedOrderId())) {
            $this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
        }
    }
    return $this;
}

You can try to unset the reservedIncremenId in the quote before getting a new one.

6
  • 1
    I was calling reserveOrderId. But the problem was that I needed to unset it like you said by calling $quote->setReservedOrderId(null); before $quote->reserveOrderId();
    – Pat
    Commented Jul 23, 2015 at 14:43
  • By the way, that is the correct way of creating an order, right? I'm not missing something else that will mess it up? I added the order creation in the main question if you can have a quick review.
    – Pat
    Commented Jul 23, 2015 at 14:46
  • 1
    Take a look to this other answer, this is how I did it once and it works fine: magento.stackexchange.com/a/74478/5913. I just copied what magento is doing.
    – lloiacono
    Commented Jul 23, 2015 at 20:40
  • 1
    Is part of magento, you can check in your magento installation: app/code/core/Mage/Checkout/Model/Type/Onepage.php
    – lloiacono
    Commented Jul 24, 2015 at 11:45
  • 1
    I already have. :)
    – Pat
    Commented Jul 24, 2015 at 11:48

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.