Skip to content

Instantly share code, notes, and snippets.

@TheFrankman
Created July 20, 2016 18:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save TheFrankman/0f342e07500ba126297ff01d09b3aa07 to your computer and use it in GitHub Desktop.
Save TheFrankman/0f342e07500ba126297ff01d09b3aa07 to your computer and use it in GitHub Desktop.
Programatically create an order in Magento 2.1
<?php
/**
* @author Frank Clark
*/
namespace Vendor\Namespace\Model\Subscription\Order;
class Create
{
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Product $product,
\Magento\Quote\Model\QuoteFactory $quote,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Sales\Model\Service\OrderService $orderService,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface
) {
$this->_storeManager = $storeManager;
$this->_product = $product;
$this->quote = $quote;
$this->quoteManagement = $quoteManagement;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->orderService = $orderService;
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->cartManagementInterface = $cartManagementInterface;
}
/**
* Create Order On Your Store
*
* @param array $orderData
* @return array
*
*/
public function createOrder($orderData) {
//init the store id and website id @todo pass from array
$store = $this->_storeManager->getStore();
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
//init the customer
$customer=$this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($orderData['email']);// load customet by email address
//check the customer
if(!$customer->getEntityId()){
//If not avilable then create this customer
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($orderData['shipping_address']['firstname'])
->setLastname($orderData['shipping_address']['lastname'])
->setEmail($orderData['email'])
->setPassword($orderData['email']);
$customer->save();
}
//init the quote
$quote=$this->quote->create();
$quote->setStore($store);
// if you have already buyer id then you can load customer directly
$customer= $this->customerRepository->getById($customer->getEntityId());
$quote->setCurrency();
$this->cartRepositoryInterface->save($quote);
$quote->assignCustomer($customer); //Assign quote to customer
//add items in quote
foreach($orderData['items'] as $item){
$product=$this->_product->load($item['product_id']);
$product->setPrice(22);
$quote->addProduct(
$product,
intval($item['qty'])
);
}
//Set Address to quote @todo add section in order data for seperate billing and handle it
$quote->getBillingAddress()->addData($orderData['shipping_address']);
$quote->getShippingAddress()->addData($orderData['shipping_address']);
// Collect Rates and Set Shipping & Payment Method
$shippingAddress = $quote->getShippingAddress();
//@todo set in order data
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quote->setPaymentMethod('checkmo'); //payment method
//@todo insert a variable to affect the invetory
$quote->setInventoryProcessed(false);
// Set sales order payment
$quote->getPayment()->importData(['method' => 'checkmo']);
//$quote->save();
$this->cartRepositoryInterface->save($quote);
// Collect total and saeve
$quote->collectTotals();
// Submit the quote and create the order
$quote = $this->cartRepositoryInterface->get($quote->getId());
$order = $this->cartManagementInterface->submit($quote);
//do not send the email
$order->setEmailSent(0);
//give a result back
if($order->getEntityId()){
$result['order_id'] = $order->getEntityId();
}else{
$result=['error'=>1,'msg'=>'Your custom message'];
}
return $result;
}
}
?>
@parththummar
Copy link

Creates order but all prices sets to 0 after adding line $this->cartRepositoryInterface->save($quote); after $quote->setCurrency();

@parththummar
Copy link

@godriccao your solution works, thanks a lot.

@creative-git
Copy link

@godriccao , shipment is not getting saved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment