I tried to add the products to cart by using code. I tried with below code
class Producttocart extends \Magento\Framework\App\Action\Action {
protected $formKey;
protected $cart;
protected $product;
protected $pageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context, \Magento\Framework\Data\Form\FormKey $formKey, \Magento\Checkout\Model\Cart $cart, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Catalog\Model\Product $product, array $data = []
) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
$this->pageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
$minQty = 3;
$cart_product = $this->getRequest()->getParams();
//echo "<pre>";print_r($cart_product);die("here");
foreach ($cart_product as $selecttid => $optionid) {
if (strpos($selecttid, 'options') !== false) {
$productoptions[$selecttid] = $optionid;
}
}
//echo "<pre>";print_r($productoptions);die("here");
//$cart_product = $this->_productFactory->getCollection()->addAttributeToFilter('entity_id',array('product_id1','product_id2'));
if ($cart_product) {
foreach ($cart_product as $key => $value) {
if($value > 0){
$product = $this->_productFactory->create()->load($key);
$customOptions = $this->_objectManager->create('Magento\Catalog\Model\Product\OptionFactory')->create()->getProductOptionCollection($product);
$qty = $this->stockItem->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
// echo $qty;die("here"); if ($value < $minQty) $value = $minQty; if ($value > $qty) { $this->messageManager->addError(__('The requested quantity for "%s" is not available.' . $product->getName())); }else{ if($customOptions){ $matches = array(); foreach ($productoptions as $selecttid => $optionid) { if (strpos($selecttid, $product->getId() . 'options') !== false && is_numeric($optionid) && stripos($selecttid, $product->getId() . 'options')==0) { $selecttid1 = str_replace($product->getId() . 'options', "", $selecttid); $matches[$selecttid1] = $optionid;
}
}
//echo "<pre>";print_r($matches);die("here");
$this->addCartProductwithOptions($product->getId(), $value, $matches);
}else{
$this->addCartProduct($key, $value);
}
}
}
}
$this->_cart->save();
}
$this->_redirect("checkout/cart/add/form_key/");
}
public function addCartProduct($productID, $productQty)
{
$product = $this->_productFactory->create()->load($productID);
$info = new \Magento\Framework\DataObject(
[
'form_key' => $this->formKey->getFormKey(),
'product_id' => $productID,
'qty' => $productQty
]
);
return $this->_cart->addProduct($product, $info);
}
public function addCartProductwithOptions($productID, $productQty, $options)
{
//echo "<pre>";print_r($options);die("here");
$product = $this->_productFactory->create()->load($productID);
$info = new \Magento\Framework\DataObject(
[
'form_key' => $this->formKey->getFormKey(),
'product_id' => $productID,
'qty' => $productQty,
'options' => $options
]
);
return $this->_cart->addProduct($product, $info);
}
In this $params value is array like
Array
(
[form_key] => D6a4V5hbr9XO26zk
[1] => 1 //here [productid] => qty
[2] => 1 //here [productid] => qty
[Add_to_Cart] => Add to Cart
)
Here $productoptions value is like this Array ( [1_options_3] => 3 [2_options_4] => None ) $matches value is like this Array ( [3] => 3 ) product is added to cart but its qty is double is after added to cart is double like below screnshot http://prntscr.com/lty4h9
when qty>0 and is_numeric($productId) product need to add to cart but above code is only one product is added to cart. how can i solve this issue?