0

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

2 Answers 2

0

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
         ];
$quote->getShippingAddress()->addData($addressData);
$quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress();
    $shippingAddress->setCollectShippingRates(true)
        ->collectShippingRates()
        ->setShippingMethod('freeshipping_freeshipping'); //Add any shipping method, should be enabled
$quote->setPaymentMethod('checkmo'); //Add any you want, payment method should be enabled
$quote->setInventoryProcessed(false);
$quote->getPayment()->importData(['method' => 'checkmo']); //Add any you want, payment method should be enabled
$quote->collectTotals();
$quote->save();

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.

-1

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());

to

        $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.