I am trying to create order programmatically product with customizable options, and it's working but it's created order with one product even I passed two products for me for these I tried below code.
Controller.php Code is below
<?php
namespace Learning\COP\Controller\Index;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\View\Result\PageFactory;
use \Magento\Framework\App\Action\Context;
use \Magento\Framework\Controller\ResultInterface;
use \Magento\Framework\App\ResponseInterface;
use \Learning\COP\Helper\Data;
class Index extends Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var Data
*/
protected $helper;
/**
* Index constructor.
*
* @param Context $context
* @param PageFactory $pageFactory
* @param Data $helper
*/
public function __construct(
Context $context,
PageFactory $pageFactory,
Data $helper
)
{
$this->helper = $helper;
$this->resultPageFactory = $pageFactory;
parent::__construct($context);
}
/**
* @return ResponseInterface|ResultInterface|void
*/
public function execute()
{
$order = [
'currency_id' => 'USD',
'email' => '[email protected]',
'shipping_address' => ['firstname' => 'John',
'lastname' => 'Doe',
'street' => '123 Street',
'city' => 'New York',
'country_id' => 'US',
'region' => 'New York',
'postcode' => '10004',
'telephone' => '52556542',
'fax' => '3242322556',
'region_id' => 43,
'save_in_address_book' => 1],
'items' => [
['product_id' => '2', 'qty' => 1, 'name' => 'Test Product 1', 'sku' => 'ts1', 'price' => 250 ],
['product_id' => '2', 'qty' => 2, 'name' => 'Test Product 2', 'sku' => 'ts2','price' => 15 ]
]
];
$result = $this->helper->createOrder($order);
var_dump($result);
}
}
Helper class
<?php
namespace Learning\COP\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Helper\Context;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Catalog\Model\Product;
use \Magento\Framework\Data\Form\FormKey;
use \Magento\Quote\Model\QuoteFactory;
use \Magento\Quote\Model\QuoteManagement;
use \Magento\Customer\Model\CustomerFactory;
use \Magento\Customer\Api\CustomerRepositoryInterface;
use \Magento\Sales\Model\Service\OrderService;
use \Magento\Catalog\Api\Data\CustomOptionInterface;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use \Magento\Catalog\Model\Product\Option;
use Magento\Quote\Api\Data\ProductOptionInterface;
use \Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;
class Data extends AbstractHelper{
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var Product
*/
protected $product;
/**
* @var FormKey
*/
protected $formkey;
/**
* @var QuoteFactory
*/
protected $quote;
/**
* @var QuoteManagement
*/
protected $quoteManagement;
/**
* @var CustomerFactory
*/
protected $customerFactory;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @var OrderService
*/
protected $orderService;
/**
* @var CustomOptionInterface
*/
protected $_pOptions;
/**
* @var ProductCustomOptionInterface
*/
protected $_productWithCustomOptions;
/**
* @var Option
*/
protected $_options;
/**
* @var ProductOptionInterface
*/
protected $_productOptionInterface;
/**
* @var CartItemRepositoryInterface
*/
protected $_cartItemRep;
/**
* @var CartItemInterface
*/
protected $_cartItem;
/**
* Data constructor.
*
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param Product $product
* @param FormKey $formKey
* @param QuoteFactory $quote
* @param QuoteManagement $quoteManagement
* @param CustomerFactory $customerFactory
* @param CustomerRepositoryInterface $customerRepository
* @param OrderService $orderService
* @param CustomOptionInterface $customOption
* @param ProductCustomOptionInterface $productCustomOption
* @param Option $productOptions
* @param ProductOptionInterface $_productOptionInterface
* @param CartItemRepositoryInterface $cartItemRepository
* @param CartItemInterface $cartItem
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
Product $product,
FormKey $formKey,
QuoteFactory $quote,
QuoteManagement $quoteManagement,
CustomerFactory $customerFactory,
CustomerRepositoryInterface $customerRepository,
OrderService $orderService,
CustomOptionInterface $customOption,
ProductCustomOptionInterface $productCustomOption,
Option $productOptions,
ProductOptionInterface $_productOptionInterface,
CartItemRepositoryInterface $cartItemRepository,
CartItemInterface $cartItem
)
{
$this->_pOptions = $customOption;
$this->storeManager = $storeManager;
$this->product = $product;
$this->formkey = $formKey;
$this->quote = $quote;
$this->quoteManagement = $quoteManagement;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->orderService = $orderService;
$this->_productWithCustomOptions = $productCustomOption;
$this->_options = $productOptions;
$this->_productOptionInterface = $_productOptionInterface;
$this->_cartItemRep = $cartItemRepository;
$this->_cartItem = $cartItem;
parent::__construct($context);
}
public function createOrder($order)
{
$store = $this->storeManager->getStore();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($order['email']);
if (!$customer->getEntityId()) {
$customer->setWebsiteId($websiteId)->setStore($store)->setFirstname($order['shipping_address']['firstname'])->setLastname($order['shipping_address']['lastname'])->setEmail($order['email'])->setPassword($order['email']);
$customer->save();
}
$quote = $this->quote->create();
$quote->setStore($store);
$customer = $this->customerRepository->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer);
$i = 1;
$optionData = [];
$optionsArray = [];
foreach ($order['items'] as $item) {
$product = $this->product->load($item['product_id']);
$product->setPrice($item['price']);
$product->setQty(intval($item['qty']));
/*foreach ($product->getProductOptionsCollection()->getItems() as $options){
$this->_productWithCustomOptions->setSku($item['sku']);
$this->_productWithCustomOptions->setTitle($item['name']);
$this->_productWithCustomOptions->setPrice($item['price']);
$this->_productWithCustomOptions->setOptionId($options->getData('option_id'));
$this->_productWithCustomOptions->setIsRequire(true);
$this->_productWithCustomOptions->setProductSku($product->getSku());
}
$optionsArray[] = array(
'sku' => $item['sku'],
'name' => $item['name'],
'price' => $item['price'],
'option_id' => $i,
'product_id' => $product->getId()
);*/
$params = array(
'product' => $product->getId(),
'qty' => intval($item['qty']),
'options' => array(
'1' => $item['name'],
'2' => $item['sku'],
'3' => $item['price']
)
);
$requestParams = new \Magento\Framework\DataObject($params);
//$options = $this->_productWithCustomOptions->getData();
//$product->setCustomOptions($options);
$quote->addProduct($product, $requestParams);
$i++;
}
/* $customOptionInterface = \Magento\Framework\App\ObjectManager::getInstance()->create(\Magento\Catalog\Api\Data\CustomOptionInterface::class);
foreach ($optionsArray as $option) {
$optionData[] = $customOptionInterface->setdata($option);
}
$productOption = $this->_productOptionInterface; // instance of Magento\Quote\Api\Data\ProductOptionInterface
$extAttribute = $productOption->getExtensionAttributes();
$extAttribute->setCustomOptions($optionData[0]);
$productOption->setExtensionAttributes($extAttribute);
$cartItem = $this->_cartItem;
$cartItem->setProductOption($productOption);
$this->_cartItemRep->save($cartItem);*/
$quote->getBillingAddress()->addData($order['shipping_address']);
$quote->getShippingAddress()->addData($order['shipping_address']);
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping');
$quote->setPaymentMethod('checkmo');
$quote->setInventoryProcessed(false);
$quote->save();
$quote->getPayment()->importData(['method' => 'checkmo']);
$quote->collectTotals()->save();
$orderdata = $this->quoteManagement->submit($quote);
if(isset($orderdata)){
if ($orderdata->getEntityId()) {
$result['order_id'] = $orderdata->getRealOrderId();
} else {
$result = ['error' => 1, 'msg' => 'Order not created..'];
}
}else {
$result = ['error' => 1, 'msg' => 'Order not created..'];
}
return $result;
}
}
Customizable options image here it is.
And created order with only one product see below screenshot.
Could you please suggest me where I went wrong on this process.