0

I have a controller, from where I can send cart quantity. And I used ajax call to fetch data

require([
    'jquery'
], function($){
    jQuery.ajax({
        url: "/mycart/cartcount",
        type: "POST",
        data: "",
        success: function (res) {
            jQuery(".cart_qty").html(result);
        }
    });
});

My question is, how can I get cart quantity in my controller?

  • As per as,your code, you do not send any parameters controller then how you get Cart Qty and Cart Item – Amit Bera Mar 1 '19 at 7:39
1

You can't simply update the qty field in the quote_item table because there is lots of constrains that has.

  1. Validate requested qty exist or not?
  2. Is the product is In-Stock or not?
  3. Is the product enabled or not and so?

and yes you can update the quantity field in quote table.

Try this, add this in your controller and you need to get item_id and qty to update the specific field.

<?php                                                                 
 namespace Vendor\Module\Controller\Index;                                
 class Save extends \Magento\Framework\App\Action\Action                      
 {
  public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Quote\Api\CartRepositoryInterface $quote,
    array $data = []
  ) {
    $this->quoteRepository = $quote;
    parent::__construct($context, $data);
 }
 public function execute()
 {
    $item_id = '6';
    $qty = '10';
    $quote = $this->quoteRepository->getById($item_id);
    foreach ($quote->getItems() as $quoteItem) {
        $quoteItem->setQty($qty);
    }
    $quote->save();
    return true;
 }}

Hope this will help you :)

| improve this answer | |

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.