3

When adding a product to the cart programatically, the product is added with a price of zero. Having read other questions on here with similar requests, I have added in $this->quote->collectTotals(); which doesn't appear to fix anything.

Without doing this as an observer, or using $this->cart->save(); (which is now deprecated), how can this be resolved? My code is below.

$this->quote->addProduct($this->product);
$this->quote->collectTotals();
$this->quoteResourceModel->save($this->quote);

Thanks!

5
  • Did you try this answer : magento.stackexchange.com/a/243628/51810 Commented Oct 30, 2018 at 9:22
  • @RohanHapani, thanks for the suggestion. Yes - however, \Magento\Checkout\Model\Cart is deprecated, says to use \Magento\Quote\Model\Quote instead, but doesn't appear to add the price to the item correctly. Commented Oct 30, 2018 at 9:29
  • But, that code is working properly. I tested in latest version 2.2.6 also. Commented Oct 30, 2018 at 9:31
  • @RohanHapani - You are correct, however I've noticed that the price comes out as zero when doing $this->response->setRedirect($this->getUrl('checkout'))->send(); after (which refreshes the page). If I manually refresh the page after it loads, the price appears. Is there a better way instead of setRedirect which will ensure the totals are updated upon refresh? Commented Oct 31, 2018 at 14:04
  • Hi , I am also facing same issue, while adding products to quote from REST API. Loading quote using \Magento\Quote\Api\CartRepositoryInterface. After adding successfully to cart, the last product showing zero totals. Thanks in Advance Commented Jun 23, 2020 at 17:28

3 Answers 3

2

You need to reload the quote from a DB to have all collectedTotals flags empty there.

/** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
$quoteRepository->get($quoteId);
$quote->addProduct($product, $request);
$quote->collectTotals()->save();

See Magento\Quote\Model\Quote::collectTotals and Magento\Quote\Model\Quote\Address\Total\Subtotal::collect

1

app/code/Ktpl/Addtocart/Controller/Index/Customaddtocart.php

<?php

namespace Ktpl\Addtocart\Controller\Index;
use Magento\Framework\App\Action\Context;

class Customaddtocart extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $checkoutSession;
    protected $_productloader;
    protected $cartRepository;
    protected $productRepository; 
    protected $_messageManager;

    public function __construct(Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Quote\Api\CartRepositoryInterface $cartRepository,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        array $data = [])
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->checkoutSession = $checkoutSession;
        $this->cartRepository = $cartRepository;
        $this->productRepository = $productRepository;
        $this->_productloader = $_productloader;
        $this->_messageManager = $messageManager;
        parent::__construct($context);
    }

    public function execute()
    {
        $post = $this->getRequest()->getPost();
        $addtocartid = $post['addtocartid'];
        $qty = $post['qty'];

        $addtocartid=explode(",", $addtocartid);
        $qty=explode(",", $qty);
        $quote = $this->checkoutSession->getQuote();
        for($j = 0; $j < count($addtocartid); $j++)
        {
            $_product = $this->_productloader->create()->load($addtocartid[$j]);
            $quote->addProduct($_product, (int)$qty[$j]);
        }
        $this->cartRepository->save($quote);
        $this->checkoutSession->replaceQuote($quote)->unsLastRealOrderId();
    }
}

app/code/Ktpl/Addtocart/etc/frontend/sections.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="addtocart/index/customaddtocart">
        <section name="cart"/>
    </action>
</config>
0

I had this very same issue and I couldn't find a solution. This may sound strange, but I fixed it with adjusting $quote->collectTotals(); to $quote->collectTotals()->setTotalsCollectedFlag(false)->collectTotals();

This is the only code what worked for me (Magento 2.4) without using deprecated code. Here is an example snippet:

<?php

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Api\CartRepositoryInterface;

class ProductCartHelper
{
    protected $productRepository;
    protected $cartRepository;
    protected $checkoutSession;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        CartRepositoryInterface    $cartRepository,
        CheckoutSession            $checkoutSession
    )
    {
        $this->productRepository = $productRepository;
        $this->cartRepository = $cartRepository;
        $this->checkoutSession = $checkoutSession;
    }

    public function setCartItem()
    {
        $productId = 22;
        $qty = 1;

        $quote = $this->checkoutSession->getQuote();

        $product = $this->productRepository->getById($productId);

        $quote->addProduct($product, $qty);

        //THIS IS REQUIRED FOR CORRECT PRODUCT PRICING AND TOTALS CALCULATION
        $quote->collectTotals()->setTotalsCollectedFlag(false)->collectTotals();

        $this->cartRepository->save($quote);
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.