0

I create an order programmatically like this:

$store = $this->storeManager->getStore();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$customer = $this->_customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($email); 
$quote = $this->quote->create();  
$quote->setStore($store);  
$customer = $this->_customerRepoInterface->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer);  

foreach ($productsOrder as $productOrder) {
  $productId = $productOrder['product_id'];
  $qty = $productOrder['qty'];
  $product = $this->_product->load($productId);
  $price = $product->getPrice();
  $product->setPrice($price);
  $quote->addProduct(
      $product,
      intval($qty)
    );
}

$quote->getBillingAddress()->addData($address);
$quote->getShippingAddress()->addData($address);

$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
  ->collectShippingRates()
  ->setShippingMethod($shippingMethod);  
$quote->setPaymentMethod($paymentMethod);  
$quote->setInventoryProcessed(false);  
$quote->save(); 

$quote->getPayment()->importData(['method' => $paymentMethod]);

$quote->collectTotals()->save();

$order = $this->quoteManagement->submit($quote);

how can i apply coupon code in here and validate the coupon code?

1 Answer 1

0

Add this code to quote

$quote->setCouponCode($couponCode)->save()

after $quote->save();

2
  • how to validate the coupon code ? Jul 3, 2018 at 9:15
  • I have to check
    – Amit Bera
    Jul 4, 2018 at 10:36

Your Answer

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

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