0

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

asked Jun 4, 2019 at 12:11
6
  • Can you please share any logs or error you got? because its working from my end. Commented Jun 4, 2019 at 12:18
  • Does the quote id 636 is assigned with frontend have you checked ? if not, display current quote id in cart page to verify. Commented Jun 4, 2019 at 12:19
  • "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): Commented Jun 4, 2019 at 12:24
  • @Ranganathan Quote id assigned by API call through postman Commented Jun 4, 2019 at 12:26
  • @PraveenNegimani then you need to assign this quote to frontend(checkoutsession) to check your cart - magento.stackexchange.com/questions/196548/… Commented Jun 4, 2019 at 12:33

3 Answers 3

0

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.

answered Jun 4, 2019 at 13:49
0

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;
}
answered Jun 7, 2019 at 9:28
1
  • how to add multiple items? Commented Nov 28, 2020 at 13:49
0

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

answered Mar 10, 2020 at 5:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.