Here I am using magento 2.1.6 and trying to create order programmatically.
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ProductFactory $product,
\Magento\Framework\Data\Form\FormKey $formkey,
\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\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Sales\Model\Service\InvoiceService $invoiceService,
\Magento\Framework\DB\Transaction $transaction,
\Magento\Sales\Api\Data\OrderInterface $order,
\Magento\Framework\ObjectManagerInterface $objectmanager,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
\Magento\Quote\Model\Quote\Address\Rate $shippingRate
) {
$this->_storeManager = $storeManager;
$this->_productFactory = $product;
$this->_formkey = $formkey;
$this->quote = $quote;
$this->quoteManagement = $quoteManagement;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->orderService = $orderService;
$this->_orderRepository = $orderRepository;
$this->_invoiceService = $invoiceService;
$this->_transaction = $transaction;
$this->order = $order;
$this->_objectManager = $objectmanager;
$this->productFactory = $productFactory;
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->cartManagementInterface = $cartManagementInterface;
$this->shippingRate = $shippingRate;
parent::__construct($context);
}
Create a new order function
public function createOrderNew($orderData) {
$store = $this->_storeManager->getStore();
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
$customer=$this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($orderData['email']);
if(!$customer->getEntityId()){
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($orderData['shipping_address']['firstname'])
->setLastname($orderData['shipping_address']['lastname'])
->setEmail($orderData['email'])
->setPassword($orderData['email']);
$customer->save();
}
$cart_id = $this->cartManagementInterface->createEmptyCart();
$cart = $this->cartRepositoryInterface->get($cart_id);
$cart->setStore($store);
$customer= $this->customerRepository->getById($customer->getEntityId());
$cart->setCurrency();
$cart->assignCustomer($customer);
$cart->save();
ob_start();
foreach($orderData['items'] as $item){
foreach($item as $item) {
$product = $this->_productFactory->create()->load($item['product_id']);
$customOptions = $this->_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
try {
$params = array('product' => $item['product_id'], 'qty' => $item['qty']);
if (array_key_exists('options', $item) && $item['options']) {
$params['options'] = json_decode(json_encode($item['options']), True);
}
if ($product->getTypeId() == 'configurable') {
$params['super_attribute'] = $item['super_attribute'];
} elseif ($product->getTypeId() == 'bundle') {
$params['bundle_option'] = $item['bundle_option'];
$params['bundle_option_qty'] = $item['bundle_option_qty'];
} elseif ($product->getTypeId() == 'grouped') {
$params['super_group'] = $item['super_group'];
}
$objParam = new \Magento\Framework\DataObject();
$objParam->setData($params);
$cart->addProduct($product, $objParam);
} catch (Exception $e) {
$response[$item['product_id']]= $e->getMessage();
}
unset($product);
}
}
$cart->save();
ob_flush();
$cart->getBillingAddress()->addData($orderData['shipping_address']);
$cart->getShippingAddress()->addData($orderData['shipping_address']);
$this->shippingRate
->setCode('freeshipping_freeshipping')
->getPrice(1);
$shippingAddress = $cart->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate');
$cart->setPaymentMethod('checkmo');
$cart->setInventoryProcessed(false);
$cart->getPayment()->importData(['method' => 'checkmo']);
$cart->collectTotals();
$cart->save();
$cart = $this->cartRepositoryInterface->get($cart->getId());
$order_id = $this->cartManagementInterface->placeOrder($cart->getId());
if($orderData['status'] == 4) {
return $this->createInvoice($order_id);
}
return $order_id;
}
Array of order data
array(5) {
["currency_id"]=>
string(3) "USD"
["email"]=>
string(16) "xxx@gmail.com"
["shipping_address"]=>
array(10) {
["firstname"]=>
string(7) "xxx"
["lastname"]=>
string(10) "xxx"
["street"]=>
string(14) "xxx"
["city"]=>
string(10) "Manchester"
["country_id"]=>
string(2) "GB"
["region"]=>
string(10) "Lancashire"
["postcode"]=>
string(7) "M23 2GF"
["telephone"]=>
string(11) "xxx"
["fax"]=>
string(0) ""
["save_in_address_book"]=>
int(1)
}
["items"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(3) {
["product_id"]=>
string(4) "4694"
["qty"]=>
string(1) "1"
["options"]=>
array(1) {
[3658]=>
string(4) "test"
}
}
}
}
["status"]=>
string(1) "4"
}
I added a Custom parameter for the product
- name: test
- type: drop-down list
- value: test
- price: $ 10
- ID: 3658
This code has been successfully created. Products that are also properly assigned, but the user settings do not match the purpose of the products.
Looking for help.
source
share