Magento – Adding Multiple Configurable Product Variations Issue

addtocartcartconfigurable-productmagento-1superattribute

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.

This is the code I've got:

public function indexAction ()   {
   $post = $this->getRequest()->getPost();
   $attr = array_keys($post['super_attribute']);
   $cart = Mage::getSingleton('checkout/cart');
    $product = Mage::getModel('catalog/product')->load($post['product']);
    foreach ($post['super_attribute'][$attr[0]] as $optId){

        if (abs($post['qty'][$optId]) > 0){

            $options = array(
                //"product"=>$post['product'], 
                "super_attribute"=>array(
                    $attr[0] => $optId
                ),                    
                "qty"=>$post['qty'][$optId]
            ); 

            echo "Add To Cart:";
            print_r($options);
            echo "<br /><br />";                
            $cart->addProduct($product, $options);
        }

    }

    $cart->save(); // save the cart
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 

    die("??");
    $this->_redirect('checkout/cart/');    
}

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

 Add To Cart:Array ( [super_attribute] => Array ( [141] => 5 ) [qty] => 2 ) 

 Add To Cart:Array ( [super_attribute] => Array ( [141] => 4 ) [qty] => 4 ) 

But in the cart I'm seeing 6 of the first super_attribute.

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

Thanks!

Best Answer

I managed to figure it out in the end - looks like it is using parts of the Magento request internally when adding to the cart - I didn't get this straight away as I coincidently used the same names for my fields as Mage was expecting.

Once I re-named my own super_attribute field and then re-set the request in the loop, it worked. The resulting code was:

public function indexAction ()
{

   $post = $this->getRequest()->getPost();

   $attr = array_keys($post['sa']);

   $cart = Mage::getSingleton('checkout/cart');



    foreach ($post['sa'][$attr[0]] as $optId){

        if (abs($post['qty'][$optId]) > 0){
            $product = Mage::getModel('catalog/product')->load($post['product']);
            $this->getRequest()->setParam('product',$post['product']);
            $this->getRequest()->setParam('super_attribute',array(
                    $attr[0] => $optId
                ));
            $options = array(
                //"product"=>$post['product'], 
                "super_attribute"=>array(
                    $attr[0] => $optId
                ),                    
                "qty"=>$post['qty'][$optId]
            ); 


            $cart->addProduct($product, $options);
            $cart->save();
            $cart = Mage::getSingleton('checkout/cart');
            $cart->init();

            $pdts = $cart->getItems();
        }


    }

    $cart->save(); // save the cart
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 



$this->_redirect('checkout/cart/'); 

}
Related Topic