-1

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 .

3

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.

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

you can get next increment id using below code.

<?php

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;
        parent::__construct($context);
    }

    /**
     * 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();
    }
}
  • the above code does not help as it is irrelevant to the question asked . – Kumar Feb 13 at 8:02

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.