0

I have a plugin and i want to add a product to the actual cart. This is my code:

          $productId = 2;
          $params = array(
                        'form_key' => $this->formKey->getFormKey(),
                        'product' => $productId, //product Id
                        'qty'   =>1 //quantity of product
                        'price' => 0
                    );
$_product = $this->product->load($productId);
$cart->addProduct($_product, $params);
$cart->save();

and i get the error: invalid request for adding product to quote. I try this:

          $productId = 2;
          $params = array(
                        'form_key' => $this->formKey->getFormKey(),
                        'product' => $productId, //product Id
                        'qty'   =>1, //quantity of product
                        'price' => 0
                    );
          //Load the product based on productID
          $request = new Varien_Object();
          $request->setData($params);
          $_product = $this->product->load($productId);
          $cart->addProduct($_product, $params);
          $cart->save();

But it crash without any error.

Can someone help me to add a product to cart?

Thank you in advance!

0

Please try using below code, it is controller file so update it accordingly :

<?php
namespace Test\Module\Controller\Cart;

class Test extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $formKey;
    protected $checkoutSession;
    protected $cart;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Data\Form\FormKey $formKey,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\View\Result\PageFactory $pageFactory
    ){
        $this->_pageFactory = $pageFactory;
        $this->formKey = $formKey;
        $this->checkoutSession = $checkoutSession;
        $this->cart = $cart;
        return parent::__construct($context);
    }

    public function execute()
    {
            $resultPage = $this->_pageFactory->create();
            $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' =>146,
                'qty'   =>1,                
            );
            $this->_redirect("checkout/cart/add/form_key/", $params);
    }
}
  • I add pageFactory and now I become this error: Type Error occurred when creating object: [Name of my Plugin] – vDrews Jun 6 at 12:57
  • Did you remove generated folder ?. if not then please remove it and also flush cache then check it. – Hardik Patel Jun 6 at 12:59
  • This works, now i dont have this error. But it's not working. I don't become an error but no product added – vDrews Jun 6 at 13:46
  • This is working code i used in past, Please check your product id and also product is created properly from backend. – Hardik Patel Jun 6 at 13:51
  • The product is added from me in the admin mode and have the right id. 2. But still the same problem. – vDrews Jun 7 at 10:54

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.