I'm trying to add multiple configurable products at once to the cart in Magento. The code I used for this is:
$postData = Mage::app()->getRequest()->getPost();
$superAttributes = $postData['super_attribute'];
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
if(isset($postData['bundleconfigurable']) && !empty($postData['bundleconfigurable'])){
foreach($postData['bundleconfigurable'] as $optionId => $qtyArray){
foreach($qtyArray as $valueId => $qty){
if($qty < 1){
continue;
}
// Add items to cart
$itemSuperAttributes = $superAttributes;
$itemSuperAttributes[$optionId] = $valueId;
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($postData['product']);
$params = array(
'product' => $postData['product'],
'super_attribute' => $itemSuperAttributes,
'qty' => $qty
);
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($postData['product']);
$cart->addProduct($product, $params);
}
}
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
I believe the code should be correct. A sample output of the $params:
Array ( [product] => 2287 [super_attribute] => Array ( [179] => 1203 [154] => 626 ) [qty] => 1 )
When redirecting to the cart page, everything is added nicely (the super attributes are just fine), but the price of the product is wrong. I've checked the numbers in the $params (179=>1203, etc) and they match with the options chosen.
Please find a screenshot below of the configuration and of the shopping cart.
Configuration:
Chosen options:
Result in cart:
So as you can see, the options are just fine, but the price is from another combination (IDS 2255 - 2259).
Any ideas how this problem can be solved?
Thanks!