Recently, I needed to create an order programmaticaly.

Here's my code :

    public function createMageOrder($orderData) {
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        $customer->loadByEmail($orderData['email']);// load customet by email address
//        if(!$customer->getEntityId()){
//            //If not avilable then create this customer
//            $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(); //Create object of quote
        $quote->setStore($store); //set store for which you create quote
        $customer= $this->customerRepository->getById($customer->getEntityId());
        $quote->assignCustomer($customer); //Assign quote to customer

        //add items in quote
        foreach($orderData['items'] as $item){
            $product = $this->getProduct($item['product_id']);
//            $product->setPrice($item['price']);

        //Set Address to quote

        // Collect Rates and Set Shipping & Payment Method

                        ->setShippingMethod('freeshipping_freeshipping'); //shipping method
        $quote->setPaymentMethod('checkmo'); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory

//        $quote->save(); //Now Save quote and your quote is ready

            // Set Sales Order Payment
            $quote->getPayment()->importData(['method' => 'checkmo']);

            // Collect Totals & Save Quote

            // Create Order From Quote
            $order = $this->quoteManagement->submit($quote);

            $increment_id = $order->getRealOrderId();
                $result['order_id']= $order->getRealOrderId();
                $result=['error'=>1,'msg'=>'Your custom message'];
            return $result;

This code is perfecty working, the quote is well created.

Now, I need to load items from the last created quote and then fill the cart with this items.

To do this, I tried this :

$customerId  = $this->_customerSession->getCustomer()->getId();
                $collection = $this->collection->create();
                $collection->addFieldToFilter('customer_id', $customerId);
                $quoteId = $collection->getLastItem()->getData('entity_id'); //get id of last quote
                $quote = $this->_quoteRepository->get($quoteId);
                if ($quoteId) {
                    try {
                    } catch (\Exception $e) {

This is almost working. If a choose to create a "regular" quote (thanks to my first function), it's working. But if I reload the last quote and then create a quote, the quote is creating twice, the first is an error (it has no item, quantity...) and the second is good.

enter image description here

Does anyone ever face this issue ?

  • I had the same issue with similliar scenario Apr 1, 2019 at 6:03


Your Answer

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