Magento 2 Collect Total Quote After Removing Item

magento2quote

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?

Best Answer

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];
}