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...!!!