1

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;


        parent::__construct($context);
    }


    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);
                $quoteItem->delete();
                /* 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);
                    $this->cart->save();
                }
                /* add current product to cart ends*/    

            }


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


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

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

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

1 Answer 1

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"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="your/update_post/url">
        <section name="cart"/>
    </action>
</config>

Hope it will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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