0

I'm creating an order programmaticaly thanks to this (see below).

The problem is that every mail of order I receive, I have a 100% discount, so the total is equal to 0.

Any help would be appreciated

    $store=$this->_storeManager->getStore();
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
$customer=$this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($orderData['email']);// load customet by email address
$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']);

    $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('flatrate_flaterate');

$shippingAddress=$quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
                ->collectShippingRates();

$shippingAddress->setShippingMethod('flatrate_flaterate'); //shipping method
$quote->getShippingAddress()->addShippingRate($this->shippingRate);
$quote->setPaymentMethod('checkmo'); //payment method
$quote->setInventoryProcessed(false); //not effetc inventory

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

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

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

    // Create Order From Quote (exception ici) => manque shipping method (set shipping method)
    $order = $this->quoteManagement->submit($quote);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.