3

I'm creating quote / order programmaticaly. But when I do this, I have an exception : (in english, "select shipping method")

Magento\Framework\Exception\LocalizedException: Sélectionnez un mode de livraison.

However, on my localhost, I can create quote / order. See my code :

$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->setCurrency();
        $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']);
            $product->setPrice(0);
            $quote->addProduct(
                $product,
                intval($item['qty'])
            );
        }

        //Set Address to quote
        $quote->getBillingAddress()->addData($orderData['shipping_address']);
        $quote->getShippingAddress()->addData($orderData['shipping_address']);

        // Collect Rates and Set Shipping & Payment Method

        $shippingAddress=$quote->getShippingAddress();
        //set shipping method is not working
        $shippingAddress->setCollectShippingRates(true)
                        ->collectShippingRates()
                        ->setShippingMethod('freeshipping_freeshipping'); //shipping method
        $quote->setPaymentMethod('checkmo'); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory

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

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

            // Collect Totals & Save Quote
            $quote->collectTotals();

            $this->quoteRepository->save($quote);

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

2 Answers 2

10
    Need to add following dependency in constructor:

\Magento\Quote\Model\Quote\Address\Rate $shippingRate

and then use the following code:

$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->setCurrency();
        $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']);
            $product->setPrice(0);
            $quote->addProduct(
                $product,
                intval($item['qty'])
            );
        }

        //Set Address to quote
        $quote->getBillingAddress()->addData($orderData['shipping_address']);
        $quote->getShippingAddress()->addData($orderData['shipping_address']);

        // Collect Rates and Set Shipping & Payment Method
$this->shippingRate
    ->setCode('freeshipping_freeshipping')
    ->getPrice(1);
$shippingAddress = $quote->getShippingAddress();
//@todo set in order data
$shippingAddress->setCollectShippingRates(true)
    ->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate'); //shipping method
$quote->getShippingAddress()->addShippingRate($this->shippingRate);
$quote->setPaymentMethod('checkmo'); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory

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

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

            // Collect Totals & Save Quote
            $quote->collectTotals();

            $this->quoteRepository->save($quote);

            // Create Order From Quote (exception here)
            $order = $this->quoteManagement->submit($quote);
7
  • Perfect. (accord to my code, it's not "$cart" but "$quote" to avoid confusion for other users). Oct 1, 2018 at 9:49
  • yes updated my anser Oct 4, 2018 at 6:01
  • @HassanAliShahzad Can you please tell why we need to set the free shipping first? Feb 21, 2019 at 3:59
  • @saiid because shipping address is required for collect shipping rates and then can continue these are predefined steps for order creation we have to follow them same as magento instruct us. So that's why we need to set shipping or free shipping at same position where we require it. Its not our choice . . . . . Feb 22, 2019 at 6:02
  • @HassanAliShahzad okay will share my findings on it Feb 22, 2019 at 7:00
0

To make above code working(By Hassan) ,

Make sure Flat Rate shipping method is enabled in backend

Check that Country selected in Shipping address is selected in Ship to specific countries dropdown for Flat rate (In case you have selected Specific countries in Ship to Applicable Countries Dropdown)

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.