1
votes

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!

2
did you get the answer this question? - Nidheesh

2 Answers

1
votes

I stumbled upon this question and answers weren't helpful. So I am posting my version here.

$parentProduct = Mage::getModel('catalog/product')->load($parentId)
    ->setStoreId(Mage::app()->getStore()->getId());

foreach($postData['super_attribute'] as $simpleProdId => $simpleProdConfig){

    //This cloning is important
    $product = clone $parentProduct;

    $cartParams = array();

    $cartParams = array(
        'super_attribute' => $simpleProdConfig['super_attribute'],
        'qty' => $simpleProdConfig['qty'],
    );

    $this->_getCart()->addProduct($product, $cartParams);

}

$this->_getCart()->save();

$this->_getSession()->setCartWasUpdated(true);

Or instead of passing the parent product object pass its id, as it seems to work though the request gets slow.

$parentProduct = Mage::getModel('catalog/product')->load($parentId)
    ->setStoreId(Mage::app()->getStore()->getId());

foreach($postData['super_attribute'] as $simpleProdId => $simpleProdConfig){

    $cartParams = array();

    $cartParams = array(
        'super_attribute' => $simpleProdConfig['super_attribute'],
        'qty' => $simpleProdConfig['qty'],
    );

    //Passing id instead of instance of the parent product here
    $this->_getCart()->addProduct($parentProduct->getId(), $cartParams);

}

$this->_getCart()->save();

$this->_getSession()->setCartWasUpdated(true);
0
votes

create a custom module in etc/config.xml add

<?xml version="1.0"?>
<config>
    <modules>
        <Package_Mymodule>
            <version>0.1.0</version>
        </Package_Mymodule>
    </modules>
    <global>
        <models>
            <checkout>
                <rewrite>
                    <cart>Package_Mymodule_Model_Checkout_Cart</cart>
                </rewrite>
            </checkout>
        </models>
    </global>
</config>

and create file in the next path Package/Mymodule/model/Checkout/Cart.php

class Package_Mymodule_Model_Checkout_Cart extends Mage_Checkout_Model_Cart{
    public function addProduct($productInfo, $requestInfo=null){   
        $producstChildId = $requestInfo['product_child_id'];

        foreach ($producstChildId as $key => $value){
            $requestInfo['qty'] = current($value);

            if($requestInfo['qty']){    
                //It is the size of the product     
                $requestInfo['super_attribute'][133] = key($value);

                $product = Mage::getModel('catalog/product')
                    ->setStoreId(Mage::app()->getStore()->getId())
                    ->load($requestInfo['product'])
                    ->setConfiguredAttributes(array('super_attribute'=>$requestInfo['super_attribute']));           
                parent::addProduct($product,$requestInfo);
            }
        }
    }
}