1

I want to add some information in additional_options of quote_item_option table while adding item to quote while creating order from backend. For this purpose I need an event that called after adding item in quote from backend.

FYI it is saving in info_buyRequest right now.

1 Answer 1

0

In your controller file, put this data, make changes according to your needs and code.

path : vendor/addtocart/controller/index/index.php

<?php
namespace Vendor\Addtocart\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Data\Form\FormKey;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $formKey;   
    protected $cart;
    protected $product;
    protected $_pageFactory;
    protected $_session;
    protected $serializer;
    protected $ProductRepository;

    protected $_responseFactory;

    public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Data\Form\FormKey $formKey,
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Catalog\Model\Product $product,
    \Magento\Catalog\Model\ProductRepository $ProductRepository,
    \Magento\Customer\Model\Session $session,
    \Magento\Framework\Serialize\SerializerInterface $serializer,
    \Magento\Framework\View\Result\PageFactory $PageFactory,
    \Magento\Framework\App\ResponseFactory $responseFactory,
    array $data = []) {
        $this->_responseFactory = $responseFactory;

        $this->formKey = $formKey;
        $this->_pageFactory = $PageFactory;
        $this->cart = $cart;
        $this->serializer = $serializer;
        $this->product = $product; 
        $this->ProductRepository = $ProductRepository;
        $this->_session = $session;    
        parent::__construct($context);
    }

    public function execute()
    {
      $page = $this->_pageFactory->create();
      $productId =1;
      $id = $this->_session->getId();
      $additionalOptions['firstnamesku'] = [
                'label' => 'First Name',
                'value' => 'chirag',
            ];
      $additionalOptions['lnamesku'] = [
                'label' => 'Last Name',
                'value' => 'parmar',
            ];
      $additionalOptions['emailidsku'] = [
                'label' => 'Email ID',
                'value' => '[email protected]',
            ];
      $additionalOptions['phonenosku'] = [
                'label' => 'Phone No',
                'value' => '1234567890',
            ];

      if(!$this->_session->isLoggedIn()){
      $params = array(
                    'form_key' => $this->formKey->getFormKey(),
                    'product' => $productId, //product Id
                    'qty'   =>1 //quantity of product
            );   
       }else{
            $params = array(
                    'form_key' => $this->formKey->getFormKey(),
                    'product' => $productId, //product Id
                    'qty'   =>1, //quantity of product
                    'customer_id'   =>$id
            );
    }           
    //Load the product based on productID   
    $_product = $this->ProductRepository->getById($productId);
    $_product->addCustomOption('additional_options', $this->serializer->serialize($additionalOptions)); 
    $this->cart->addProduct($_product, $params);
    $this->cart->save();
    //return $page;
    $message = "Product have been successfully added to your Shopping Cart.";
    $this->messageManager->addSuccess($message);
    $accUrl = $this->_url->getUrl('checkout/cart/');
    $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse();
    $this->setRefererUrl($accUrl);
  }
}
?>

This will save your data to tables like sales_order_item, quote_item_option, and related tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.