Magento2 – How to Set Order Increment ID While Creating Order Dynamically

magento2magento2.2ordersphp-7

I am creating orders in magento 2 programatically but i cannot know how to set increment Id for orders , client's want to migrate old order into magento 2 , so we already have order increment id but

$quote->setIncrementId('1501')

Does not set order increment id like it does in magento 1 , i am creating order using this tutorial

https://www.w3classes.info/create-order-programatically-in-magento-2/

i searched similar question but found none on how to set it while creating the order ,
i don't want to change increment id of the magento order that we have in our website
i just want that when i create an order programatically it should have same increment id number that we have in the excel data .

This question is not related to change increment id in any way , so please don't suggest to open database and make changes to order tables.

here is how i am creating the order ,

    public function createOrder($orderData) {
    $store=$this->_storeManager->getStore();
    $websiteId = $this->_storeManager->getStore()->getWebsiteId();
    $customer=$this->customerFactory->create();
    $customer->setWebsiteId($websiteId);
    $customer->loadByEmail($orderData['email']);
    if(!$customer->getEntityId()){
    $customer->setWebsiteId($websiteId)
    ->setStore($store)
    ->setFirstname($orderData['shipping_address']['firstname'])
    ->setLastname($orderData['shipping_address']['lastname'])
    ->setEmail($orderData['email'])
    ->setPassword($orderData['email']);
    $customer->save();
    }
    $quote=$this->quote->create();
    $quote->setStore($store); 

    $customer= $this->customerRepository->getById($customer->getEntityId());
    $quote->setCurrency();
    $quote->assignCustomer($customer);

    //here i want to use order increment id that are present in the data 
    $quote->setIncrementId('1501');

    foreach($orderData['items'] as $item){
        $product=$this->_product->load($item['product_id']);
        $product->setPrice($item['price']);
        $quote->addProduct(
        $product,
        intval($item['qty'])
        );
    }

    $quote->getBillingAddress()->addData($orderData['shipping_address']);
    $quote->getShippingAddress()->addData($orderData['shipping_address']);

    $shippingAddress=$quote->getShippingAddress();
    $shippingAddress->setCollectShippingRates(true)
    ->collectShippingRates()
    ->setShippingMethod('freeshipping_freeshipping');
    $quote->setPaymentMethod('checkmo');
    $quote->setInventoryProcessed(false);
    $quote->save(); 

    $quote->getPayment()->importData(['method' => 'checkmo']);
    $quote->collectTotals()->save();
    $order = $this->quoteManagement->submit($quote);
    $order->setEmailSent(0); 
    $increment_id = $order->getRealOrderId();

    if($order->getEntityId()){
        $result['order_id']= $order->getRealOrderId();
        echo "order created";
    }else{
        $result=['error'=>1,'msg'=>'Error while creating order'];
        echo "error in order creation";
    }

    return $result;
}

please suggest how to set increment id according to what present in the data , the data has order increment id as #1009

1008,1006,1005,1003,1002,1001

these increment id present inside the data are not in auto increment format , as you can see we have order increment id 1006 and then have 1008 but do not have 1007 , and i want them to be exactly what they are , so these orders could be referenced later for historic purposes .

Best Answer

You should use $quote->setReservedOrderId() instead of $qoute->setIncrementId().

Changing that line, should do the trick:

//here i want to use order increment id that are present in the data 
$quote->setReservedOrderId('1501');

Anyway you must take care of possible conflicts when saveing the order if those increment_ids are already used in the database.

Related Topic