1

I have a module that removes cart items from Cron job every day. After removing the item, the quote total quantity and quote subtotal amount does not update. By using the below function I want to update the quote data but it does not work.

    public function updateCart($quoteId,$customerId){

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager


    /**
     * @var $itemModel \Magento\Quote\Api\CartRepositoryInterface
     */
    $itemModel = $objectManager->create('\Magento\Quote\Api\CartRepositoryInterface');//Quote item model to load quote item

    /**
     * @var $q \Magento\Quote\Model\Quote
     */
    $q=$objectManager->create('\Magento\Quote\Model\Quote');
    $q->loadByCustomer($customerId);

    $q->collectTotals();
    $q->setTotalsCollectedFlag(false);

    $itemModel->save($q);
    $q->save();

    $itemModel->get($quoteId);

    $itemModel->save($q);

}

Where am I making a mistake?

  • 1
    I am also getting same issue – Navin Bhudiya Feb 19 '18 at 12:59
  • @NavinBhudiya in cron table on db i see this error No such entity with cartId = 341 do you know what's happened ? – gh darvishani Feb 19 '18 at 13:11
  • 1
    That error coming because that cart id is not found in quate table, Cart id is removed from some where that's y error are coming – Navin Bhudiya Feb 19 '18 at 13:13
  • @NavinBhudiya and what is difference between quote id and cart id ...i can not see any cart id on db – gh darvishani Feb 19 '18 at 13:16
  • 1
0

after many search and test, i found answer here

i use Magento 2.1.8 and for fixed this bug on vendor/magento/module-quote/Model/QuoteRepository.php change load to loadByIdWithoutStore . final method should be like below

   public function get($cartId, array $sharedStoreIds = [])
{
    if (!isset($this->quotesById[$cartId])) {
      //  $quote = $this->loadQuote('load', 'cartId', $cartId, $sharedStoreIds);
        $quote = $this->loadQuote('loadByIdWithoutStore', 'cartId', $cartId, $sharedStoreIds);
        $this->getLoadHandler()->load($quote);
        $this->quotesById[$cartId] = $quote;
    }
    return $this->quotesById[$cartId];
}
  • so now quote total quantity and quote subtotal update properly? – Navin Bhudiya Feb 20 '18 at 4:48
  • @NavinBhudiya yeah – gh darvishani Feb 21 '18 at 5:18

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.