#StackBounty: #magento2 #product #custom-options #create-order Create order programmatically product with customizable options in Magen…

Bounty: 50

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 LearningCOPControllerIndex;

use MagentoFrameworkAppActionAction;
use MagentoFrameworkViewResultPageFactory;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultInterface;
use MagentoFrameworkAppResponseInterface;
use LearningCOPHelperData;

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' => 'bojjaiah@gmail.com',
            '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 LearningCOPHelper;

use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppHelperContext;
use MagentoStoreModelStoreManagerInterface;
use MagentoCatalogModelProduct;
use MagentoFrameworkDataFormFormKey;
use MagentoQuoteModelQuoteFactory;
use MagentoQuoteModelQuoteManagement;
use MagentoCustomerModelCustomerFactory;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoSalesModelServiceOrderService;
use MagentoCatalogApiDataCustomOptionInterface;
use MagentoCatalogApiDataProductCustomOptionInterface;
use MagentoCatalogModelProductOption;
use MagentoQuoteApiDataProductOptionInterface;
use MagentoQuoteApiCartItemRepositoryInterface;
use MagentoQuoteApiDataCartItemInterface;

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 MagentoFrameworkDataObject($params);
            //$options = $this->_productWithCustomOptions->getData();
            //$product->setCustomOptions($options);
            $quote->addProduct($product, $requestParams);
            $i++;
        }

       /* $customOptionInterface = MagentoFrameworkAppObjectManager::getInstance()->create(MagentoCatalogApiDataCustomOptionInterface::class);
        foreach ($optionsArray as $option) {
            $optionData[] = $customOptionInterface->setdata($option);
        }

        $productOption = $this->_productOptionInterface; // instance of MagentoQuoteApiDataProductOptionInterface
        $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.

enter image description here

And created order with only one product see below screenshot.

enter image description here

Could you please suggest me where I went wrong on this process.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.