4

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!

  • Sorry I'm not at a computer right now, but when adding a configurable product. You technically add the configurable itself and its chosen option. I'd start by doing a count of products in the cart collection prior to the super attribute – Rob May 14 '15 at 12:25
  • did you get the answer this question? i have try same but not work. each time takes first product super_attribute configuration. – Rakesh Jesadiya Apr 11 '16 at 7:24
1

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/'); 

}
| improve this answer | |
  • I am also facing same issue could you exlpain what do you mean by "re-named my own super_attribute field and then re-set the request in the loop," – Mukesh Aug 12 '16 at 11:01
0

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);
            }
        }
    }
}
| improve this answer | |

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