Here am have a custom api at that API call am trying to add a product to cart but am not getting it.
namespace Vendor\Cart\Model;
use Magento\Framework\Data\Form\FormKey;
use Vendor\Cart\Api\HelloInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteRepository;
use Magento\Checkout\Model\Session as CheckoutSession;
class Hello implements HelloInterface
{
protected $cart;
protected $formKey;
protected $request;
protected $_wishlistRepository;
protected $_productRepository;
protected $_sellerCollection;
public function __construct(
\Magento\Checkout\Model\Cart $cart,
FormKey $formKey,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
{
$this->cart = $cart;
$this->formKey = $formKey;
$this->quoteRepository = $quoteRepository;
$this->quoteFactory = $quoteFactory;
$this->productRepository = $productRepository;
}
public function new(){
$QuoteId=636;
$quote = $this->quoteFactory->create()->load($QuoteId);
$d = $quote->getData();
$quoteId = $d['entity_id'];
//*********************************** */
$quote = $this->quoteRepository->get($QuoteId);
$items = $quote->getAllItems();
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product' => 37,
// 'sku' => 'SKU590003557',
'quote_id' => $quoteId,
'qty' => 1
);
$product = $this->productRepository->getById(37);
/****************************************** */
$this->cart->addProduct($product,$params);
$this->cart->save();
}
}
Where am doing wrong?. can i get help? Thank you in advance
3 Answers 3
Hope this will help you. I used the following code to add the product to current quote programmatically.
$storeId = $this->_storemanager->getStore()->getId();
$product_coll = $this->productRepository->getById($productId, false, $storeId);
$params = array(
'form_key' => $this->_formkey->getFormKey(),
'product' => $productId, //product Id
'qty' => $qty //quantity of product
);
$this->_cart->addProduct($product_coll, $params);
$this->_cart->save();
Let me know if you face any issue.
You can replace this code in your controller, FYI, No need to use cart Model in __construct().
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var \Magento\Framework\Data\Form\FormKey
*/
protected $formKey;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
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' =>12,//product Id
'qty' =>1,//quantity of product
'price' =>100 //product price
);
$this->_redirect("checkout/cart/add/form_key/", $params);
/** @var \Magento\Framework\View\Result\Page $resultPage */
return $resultPage;
}
-
how to add multiple items?Rizwan Khan– Rizwan Khan2020年11月28日 13:49:02 +00:00Commented Nov 28, 2020 at 13:49
If you want minicart to be updated when you add product to cart by controller you need to add your controller to file section.xml like in Magento_Checkout module:
Magento/Checkout/etc/frontend/sections.xml
https://github.com/magento/magento2/issues/3287#issuecomment-179773861
"message": "Class TW\\Product\\Helper\\Data does not exist",,C:\\xampp\\htdocs\\hylosh\\vendor\\magento\\framework\\ObjectManager\\Definition\\Runtime.php(44): Magento\\Framework\\Code\\Reader\\ClassReader->getConstructor('Vendor\\\\...')\n#1 C:\\xampp\\htdocs\\hylosh\\vendor\\magento\\framework\\ObjectManager\\Factory\\Dynamic\\Developer.php(48):