I m trying to remove the group of item from the cart and adding another group of product to the cart. My error is, price is displaying only for first item not other items in cart page after doing the replace in cart..

Friends pls help me out..In new to magento

Below is my code

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\ProductFactory;
use Magento\Customer\Model\Url;

class UpdatePost extends Action

    protected $cart;
    protected $product;

    protected $_customerSession;

    protected $_catalogProduct;
    protected $checkout_session;

    public function __construct(
        Context $context,
        ProductFactory $catalogProduct,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Catalog\Model\Product $product,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Model\Session $checkout_session
    ) {
        $this->_customerSession = $customerSession;
        $this->_catalogProduct = $catalogProduct;
        $this->cart = $cart;
        $this->product = $product;
        $this->checkout_session = $checkout_session;


    public function execute()
        $params = $attr = array();  
        $productInfo = array();      
        $params = $this->getRequest()->getParams();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        if(isset($params['item_id']) && count($params['item_id']) > 0){

            /* remove item cart starts */
            for ($i=0; $i cart->removeItem($item_id)->save();
                $quoteItem = $this->getItemModel()->load($item_id);
                /* remove the exiting product from cart ends*/

            /* remove item cart ends */

            for ($i=0; $i  $opt_val) {
                            $attr['options'][$opt_key]  = $opt_val;
                /* add current product to cart starts*/
                $_product = $this->_catalogProduct->create()->load($product_id);
                if ($_product)
                    $this->cart->addProduct($_product, $attr);
                /* add current product to cart ends*/    


            $this->messageManager->addSuccess(__('Cart Updated successfully.'));

             $this->messageManager->addError(__('Invalid Item. Please select item in cart!'));

         return $this->goBack($this->_redirect->getRefererUrl()); 

  • After added new single item to the cart. Price is displaying for all item Commented Mar 8, 2017 at 15:42
  • Friend check about code is correct or not? Commented Mar 8, 2017 at 15:52

1 Answer 1


Try to add the sections.xml file in etc/frontend and add the code below:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <action name="your/update_post/url">
        <section name="cart"/>

Hope it will help you.

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.