1

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!

user10170135
  • 11
  • 1
  • 3
  • Did you try this answer : https://magento.stackexchange.com/a/243628/51810 – Rohan Hapani Oct 30 '18 at 09: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. – user10170135 Oct 30 '18 at 09:29
  • But, that code is working properly. I tested in latest version 2.2.6 also. – Rohan Hapani Oct 30 '18 at 09: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? – user10170135 Oct 31 '18 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 – purna gattu Jun 23 '20 at 17:28

2 Answers2

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>
Kushal Dani
  • 2,114
  • 2
  • 17
  • 49
1

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