0

Hi @all actually i am facing Error Exception message: The product's required option(s) weren't entered. Make sure the options are entered and try again. Magento 2 error while Products addToCart programmatically.

And also addProduct() function making problem here.

#1 /var/www/html/url/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Checkout\Model\Cart->addProduct(Object(Magento\Catalog\Model\Product\Interceptor), Array)

Please check code below:

<?php

    namespace Vendor\Module\Controller\AddProduct;
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\Controller\ResultFactory; 
    use Magento\Framework\UrlInterface;
    use Magento\Framework\Serialize\SerializerInterface;

    class AddItems extends \Magento\Framework\App\Action\Action
    {
        protected $formKey;   
        protected $cart;
        protected $product;
        protected $messageManager;
        protected $checkoutSession;
        protected $productRepository; 
        protected $redirect;
        protected $objectManager;
        protected $_productCreate;
        protected $customerSession;
        protected $serializer;

        public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Data\Form\FormKey $formKey,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\ProductFactory $product,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Catalog\Model\Product $productCreate,
        \Magento\Customer\Model\Session $customerSession,
        SerializerInterface $serializer
        ) {
            $this->formKey = $formKey;
            $this->cart = $cart;
            $this->product = $product;      
            $this->messageManager = $messageManager;
            $this->checkoutSession = $checkoutSession;
            $this->productRepository = $productRepository;
            $this->redirect = $redirect;
            $this->objectManager = $objectManager;
            $this->_productCreate = $productCreate;
            $this->customerSession = $customerSession;
            $this->serializer = $serializer;
            parent::__construct($context);
        }

        public function execute() { 

                $product_price = 100;
                $product_id = 109;      
                $params = array(
                        'form_key' => $this->formKey->getFormKey(),
                        'product_id' => $product_id, //product Id
                        'qty'   => 1 //quantity of product                
                        );      

                $customOptions =                                                
                    [
                        "option_id" => 10, // assign option Id
                        "option_value" => "Quaterly" // here set selected option item Id
                    ];

                $_product = $this->product->create()->load($product_id); 

                $_product->addCustomOption('additional_data',$this->serializer->serialize($customOptions)); //assigning custom options with products                    
                $this->cart->addProduct($_product, $params);
                $productItem = $this->getProductQuote($_product);
                $productItem->setCustomPrice($product_price);
                $productItem->setOriginalCustomPrice($product_price);   
                $this->cart->save();
        }

        public function getProductQuote($product){
            $quote = $this->checkoutSession->getQuote();        
            $cartItems = $quote->getItemByProduct($product);        
            return $cartItems;
        }


}

If anyone understand the problem please help me.Thanks in Advance...!!!

1

The correct params format is like below:

$params = [
    'form_key' => <form_key>,
    'product' => <product_id>,
    'qty' => <quantity>,
    'options' => [
        <option_id_1> => <option_value_id_1>,
        <option_id_2> => <option_value_id_2>,
        <option_id_x> => <option_value_id_x>,
    ],
];

Your params array format does not implements the default rule. It should looks like this:

$params = [
    'form_Key' => $this->formKey->getFormKey(),
    'product' => '109',
    'qty' => '1',
    'options' => [
        '10' => 'Quarterly',
    ],
];

The complete code should be like this:

$productId = 109;

$product = $this->product->create()->load($product_id);
$params = [
    'form_Key' => $this->formKey->getFormKey(),
    'product' => $productId,
    'qty' => '1',
    'options' => [
        '10' => 'Quarterly',
    ],
];

$this->cart->addProduct($product, $params);
$this->cart->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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