2

How to add product to cart programmatically in Magento 2.2.0 ?

I have tried below code but when I do not get Quote item id in Validate method of below file.

\Magento\AdvancedSalesRule\Model\Rule\Condition\Product.php

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 = 32;
  $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' => $productId, 
                'qty'   => 2                
            );              

    $_product = $this->product->load($productId);  

    $this->cart->addProduct($_product, $params);

    $this->cart->save();
 }

Sales Rule Validate Method Code:

File: \Magento\AdvancedSalesRule\Model\Rule\Condition\Product.php

Method:

public function validate(\Magento\Framework\Model\AbstractModel $model)
    {
        //@todo reimplement this method when is fixed MAGETWO-5713
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $model->getProduct();
        if (!$product instanceof \Magento\Catalog\Model\Product) {
            $product = $this->productRepository->getById($model->getProductId());
        }

        if ($model instanceof \Magento\Quote\Model\Quote\Item) {
            die($model->getItemId()); //NULL

Thanks

3
  • 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 = 32; $params = array( 'form_key' => $this->formKey->getFormKey(), 'product' => $productId, 'qty' => 2, 'price' => 20 ); $_product = $this->product->load($productId); $this->c Commented Feb 6, 2018 at 13:52
  • @kapil, did you get any solution for this? Commented Feb 20, 2020 at 4:13
  • Yes, I figured it out, I will post the answer shortly. Commented Feb 20, 2020 at 6:03

2 Answers 2

0

you can used this 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 = 32;
  $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' => $productId, 
                'qty'   => 2                
            );              

    $_product = $this->product->load($productId);  

    $this->cart->addProduct($_product, $params);

    $this->cart->save();
 }
2
  • Thanks, But I do not get Quote item id in \Magento\AdvancedSalesRule\Model\Rule\Condition\Product.php Validate method , when I use above code. Commented Feb 6, 2018 at 13:05
  • 2
    If you have new requirement with this, please ask question according to that. Commented Feb 6, 2018 at 13:07
0
namespace Vendor\Module\Controller\Index;

class AddProducttocart extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;
    /**
     * @var \Magento\Framework\Data\Form\FormKey
     */
    protected $formKey;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Data\Form\FormKey $formKey,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->formKey = $formKey;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
            $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' =>1,//product Id
                'qty'   =>1,//quantity of product
                'price' =>20 //product price
            );
            $this->_redirect("checkout/cart/add/form_key/", $params);
        return $resultPage;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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