0

I have tried the following controller file to add product to cart in my custom extension.

<?php namespace MS\Recommendeditems\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { protected $productRepository; protected $cart; public function __construct( \Magento\Catalog\Model\ProductRepository $productRepository, \Magento\Checkout\Model\Cart $cart ) { $this->productRepository = $productRepository; $this->cart = $cart; } public function execute() { $this->_view->loadLayout(); $this->_view->getLayout()->initMessages(); $params = $this->getRequest()->getParams(); $options = $params['options']; $superAttribute = $params['super_attribute']; $product = $this->productRepository->getById($params['product']); $paramater = array('product' => $params['product'], 'qty' => $params['qty'], 'options' => $options, 'super_attribute' => $superAttribute ); $this->cart->addProduct($product, $request); $this->cart->save(); $this->_view->renderLayout(); } } 

but this code is not working and it shows the blank page.

Please help me to fix this issue.

Thanks

2

2 Answers 2

0

Can try full of code and add this code in your controller

<?php namespace MS\Recommendeditems\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; class Index extends Action {     protected $formKey;        protected $cart;     protected $product;     public function __construct(         Context $context,         FormKey $formKey,         Cart $cart,         Product $product) {             $this->formKey = $formKey;             $this->cart = $cart;             $this->product = $product;                   parent::__construct($context);     }     public function execute()      {          $productId =$yourproductid; //yourproduct id like 2         $params = array(                     'form_key' => $this->formKey->getFormKey(),                     'product' => $productId,                      'qty'   =>1                 );                       $product = $this->product->load($productId);                $this->cart->addProduct($product, $params);         $this->cart->save();      } } 

And this then check more information :-

https://meetanshi.com/blog/programmatically-add-product-to-cart-in-magento-2/

THANKS.

1
  • your code is working but I need to work it with my code. Do you have any suggestion? Commented Sep 2, 2020 at 16:25
1

I would suggest First check check var/log file for errors. So you will get idea what is wrong with your current code If still unable to solve then try below code :-

protected $formKey; protected $cart; protected $product; 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, array $data = []) { $this->formKey = $formKey; $this->cart = $cart; $this->product = $product; parent::__construct($context); } public function execute() { $productId =10; $params = array( 'form_key' => $this->formKey->getFormKey(), 'product' => $productId, //product Id 'qty' =>1 //quantity of product ); //Load the product based on productID $_product = $this->product->load($productId); $this->cart->addProduct($_product, $params); $this->cart->save(); } 
1
  • I have already checked the var/log files but there is no errors for it. Commented Sep 2, 2020 at 16:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.