2

I added simple product with options programmatically into cart using this code.

namespace Vendor\Extension\Controller\Customer;

class Myclass extends \Magento\Framework\App\Action\Action
{
    protected $messageManager;
    protected $custsession;
    protected $_responseFactory;
    protected $_url;
    protected $_appRequestInterface;

    public function __construct(\Magento\Framework\App\Action\Context $context, 
    \Magento\Customer\Model\Session $custsession,
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url,
    \Magento\Framework\App\RequestInterface $appRequestInterface,
    \Magento\Framework\Message\ManagerInterface $messageManager)
    {
        $this->custsession = $custsession;
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_appRequestInterface = $appRequestInterface;
        $this->messageManager = $messageManager;
        parent::__construct($context);
    }

    public function execute()
    {
        if($this->custsession->isLoggedIn())
        {
            try
            {
                $params = [
                          'product' => 12,
                          'selected_configurable_option' => NULL,
                          'related_product' => NULL,        
                          'options' => [
                              3 => 8,
                          ],
                          'qty' => 2
                ];

                $product = $om->create('Magento\Catalog\Model\Product')->load(12);
                $mycartp = $om->get('Magento\Checkout\Model\Cart');
                $mycartp->addProduct($product,$params);
                $mycartp->save();

                $quote = $om->get('Magento\Quote\Model\Quote');
                $quote->setTotalsCollectedFlag(false)->collectTotals()->save();

                $checkout = $om->get('Magento\Checkout\Model\Session');
                $checkout->getQuote()->collectTotals()->save();

                $message = "Your quote has been successfully added to cart";
                $this->messageManager->addSuccess($message);

                $accUrl = $this->_url->getUrl('checkout/cart/');
                $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse();   
                exit;

            }
            catch(\Exception $e)
            {
                $this->messageManager->addError($e->getMessage());
                $accUrl = $this->_url->getUrl('extension/controller/mypage');
                $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse();   
                exit;
            }
        }
        else
        {
            $accUrl = $this->_url->getUrl('customer/account/login');
            $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse();   
            exit;
        }
    }
}

The product sucessfully added to cart with options, but it not show correct subtotal in cart page and mini-cart. See below screen-sort.

enter image description here

One's I click on "Update Shopping Cart" it show correct total. See below screen-sort.

enter image description here

Any one know about this?

1
  • did you ever find a solution to this?
    – pixiemedia
    Aug 27, 2017 at 12:09

2 Answers 2

5

Please replace below code with my code :

Replace This :

 $quote = $om->get('Magento\Quote\Model\Quote');
 $quote->setTotalsCollectedFlag(false)->collectTotals()->save();

 $checkout = $om->get('Magento\Checkout\Model\Session');
 $checkout->getQuote()->collectTotals()->save();

Replace With :

$quoteRepository = $om->get('\Magento\Quote\Api\CartRepositoryInterface');
$quoteObject = $quoteRepository->get($mycartp->getQuote()->getId());
$quoteObject->setTriggerRecollect(1);
$quoteObject->setIsActive(true);
$quoteObject->collectTotals()->save();

Above code works for me. So please try this one.

0

You can recalculate the cart total using the below code

$quote->setTriggerRecollect(1);
$quote->collectTotals()->save();

Your Answer

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

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