cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2. Create an order programatically with free products and mark the order as paid

SOLVED

Magento2. Create an order programatically with free products and mark the order as paid

Hi. I'm trying to create an order programatically in Magento 2

 

The main idea I want to get is the following:

  • I have downloadable products with price set to zero due to Catalog Price Rules in a website
  • I want when they click the "Add to cart button" the cart convert that "one-item" cart to an complete order so they skip all the intermediate steps

To perform the last step I have created an observer to the checkout_cart_product_add_after method

 

In this event I do the following

 

 

// Get quote from the cart (_cart is a Magento\Checkout\Model\Cart)
$quote = $this->_cart->getQuote ();

// Ensure our quote has our customer ID (_current customer is Magento\Customer\Helper\Session\CurrentCustomer)
$quote->setCustomerId ($this->_currentCustomer->getCustomerId ());

// Get the billing information and configure it
$billing = $quote->getBillingAddress ();
$billing->setFirstname ('lorem-ipsum');
// ... 

// Set the payment methods
$quote->setPaymentMethod ('free');

// Set Sales Order Payment
$quote->getPayment ()->importData (array ('method' => 'free'));

// Configure quote
$quote->setInventoryProcessed (false);
$quote->collectTotals ();

// Update changes
$quote->save ();

// Place order (Magento\Quote\Model\QuoteManagement)
$order_id = $this->_quotemanagement->placeOrder ($quote->getId ());

At this point I have the following status

  • I can see on the customer's orders tab in the backend his order marked as PENDING
  • The customer hasn't receive any emails yet

What If we be desierable is to have this order marked as paid on the backed and the customer receiving his email with his pursache's links but I can't figure out what I'm missing

 

Any help is welcome

 

Kind regards

 

 

 

 

1 REPLY

Re: Magento2. Create an order programatically with free products and mark the order as paid

Hi. I could fix the email problem using the Magento\Sales\Model\Order\Email\Sender\OrderSender to send the order email to the customer this way

 

$this->_orderSender->send ($order);