I try to create an order programmatically in magento2 using this code snippet found on webkul.com

Order I want to create for guest customer.while script executing customer creating ... I don't want that Can any one help me to create orders for guest with out creating customers

3 Answers 3


Following links will help you in creating guest order

https://blog.amasty.com/creating-magento-2-orders-programmatically/ http://webkul.com/blog/create-quote-and-order-programmatically-in-magento2/

  • Sorry Both are same they are creating customers if customers not exist. What i need is if customer not exist need to place order with guest mode only
    – Pawankumar
    May 9, 2017 at 13:41
  • Anyone found answer for this guest mode order creation programmatically?
    – Elamurugan
    Aug 17, 2017 at 11:11
  • Yes i found it i was created also order programatically
    – Pawankumar
    Sep 4, 2017 at 4:42
  • @pawankumar Share link Mar 10, 2020 at 13:49

Will give you clues how to create it:

First you need to create guest quote:

use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;

$quoteId = $this->cartManagementInterface->createEmptyCart();
$quote = $this->cartRepositoryInterface->get($quoteId);

Then add product:

$quote->addProduct($product, $qty);
$addressData = [
         'telephone' => '', //fill data
         'email' => '', //fill data
         'firstname' => '', //fill data
         'lastname' => '', //fill data
         'street' => '', //fill data
         'city' => '', //fill data
         'country_id' => '', //add country code you need
         'postcode' => '', //fill data
          'save_in_address_book' => 0
$shippingAddress = $quote->getShippingAddress();
        ->setShippingMethod('freeshipping_freeshipping'); //Add any shipping method, should be enabled
$quote->setPaymentMethod('checkmo'); //Add any you want, payment method should be enabled
$quote->getPayment()->importData(['method' => 'checkmo']); //Add any you want, payment method should be enabled

And after there is quote with products, shipping method, payment method (that doesn't require any action after order is placed), you can place guest order:

use Magento\Quote\Api\GuestCartManagementInterface;    
$orderId = $this->guestCartManagement->placeOrder($quoteId)

After order placed, you should bet order id in response.


this worked for me. I would post the entire function but I have heavily modified it to do more custom things relevant to the module I am was working on that will just clutter up the answer

I followed the way done on here using the 3rd answer down which works on 2.1.

How to create order programmatically in Magento 2?

then changed this line

    $cart = $this->cartRepogitoryInterface->get($cart->getId());


        $cart = $this->cartRepositoryInterface->get($quote->getId())->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);

which can be found in this file magento-2/vendor/magento/module-quote/Model/GuestCart/GuestCartManagement.php

you will also need to add

use Magento\Quote\Api\CartManagementInterface;

And don't forget to remove the customer creation parts.

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.