I'm trying to add multiple variations of a configurable product to the cart at once, and I've put the code together, but currently it's adding the right qty of products, but only using the first variation.

In other words, if I try to add 2 Green T-Shirts and 4 White T-Shirts, It's adding 6 Green T-Shirts into cart.

This is the code I've got inside add controller:

public function execute()
        $paramsData = $this->getRequest()->getParams();
        try {
            $msg = array();
            $errorMsg = array();
            foreach($paramsData['qty'] as $pId=>$param){ 

                if (isset($param)) {
                    $filter = new \Zend_Filter_LocalizedToNormalized(
                        ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
                    $params['qty'] = $filter->filter($param);

                $params['product'] = $paramsData['product'][$pId];
                $product = $this->initProduct($params['product']);   
                $params['super_attribute'] = $paramsData['super'][$pId]; 
                 * Check product availability
                if (!$product) {
                    return $this->goBack();

                $this->cart->addProduct($product, $params);
                $msg[] = $product->getName(); 


        return $resultRedirect;

And from that print_r, it's confirming that the options are correct:

 Array ( [super_attribute] => Array ( [90] => 5 ) [qty] => 2 ) 

 Array ( [super_attribute] => Array ( [90] => 7 ) [qty] => 4 ) 

But in the cart I'm seeing 6 of the first super_attribute(6 green T-Shirts instead of 2 green and 4 white t-shirt).

Is there something I need to do to 'reset' the cart after adding each item or something?


2 Answers 2


I have got answer, issue is product object override next loop, in controller,

we have to create each time new product object and now its working after doing below changes.

Instead of $product = $this->initProduct($params['product']);we have set below line,

$storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);

Its works.

  • Good Answer, This one saved my time! Thank You! @Rakesh Jul 26, 2016 at 13:18
  • Hi Rakesh i want to add configurable product in cart from listing page. can you help me Jul 3, 2017 at 12:14
  • @Rakesh can you answer for this magento.stackexchange.com/questions/201931/…
    – amith lal
    Nov 17, 2017 at 6:34
  • but if use this code will made process add cart slow when add much items.
    – xanka
    Apr 9, 2018 at 3:55
  • Hi Rakesh, I need to add multiple products with multiple sizes on the Category page at once? Can you help me please?
    – kiran
    May 21, 2019 at 2:02

Old question, just to report a better way to do the same. Instead of using ObjectManager...

$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);

Using \Magento\Catalog\Model\ProductFactory (sou you'll need to add that new dependency to your class constructor) will produce the same result...

$product = $this->_productFactory->create()->setStoreId($storeId)->load($params['product']);

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.