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


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


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) {
    $websiteId = $this->_storeManager->getStore()->getWebsiteId();

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

    //here i want to use order increment id that are present in the data 

    foreach($orderData['items'] as $item){



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

        $result['order_id']= $order->getRealOrderId();
        echo "order created";
        $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


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 .

2 Answers 2


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 

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

  • Thanks HelgeB , that is exactly what i needed . will take care of conflicts Commented Feb 14, 2019 at 6:51

you can get next increment id using below code.


namespace Namespace\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\SalesSequence\Model\Manager;

class Data extends AbstractHelper

     * @var Manager
    protected $_sequenceManager;

     * @param Context $context
     * @param Manager $sequenceManager
    public function __construct(
        Context $context,
        Manager $sequenceManager
        $this->_sequenceManager = $sequenceManager;

     * get Next IncrementId
     * @param object of order/invoice/shipment
     * @return object
    protected function getNextIncrementId($object)
        return $this->_sequenceManager->getSequence($object->getEntityType(), $object->getStore()->getGroup()->getDefaultStoreId())->getNextValue();
  • 1
    the above code does not help as it is irrelevant to the question asked . Commented Feb 13, 2019 at 8:02

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.