Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Adding bundle via code shows 0 price and does not refresh basket total
 
luckyknight
Jr. Member
 
Total Posts:  22
Joined:  2010-09-10
 

We are using a third party plugin that automatically adds a product to the basket. I have customized it so that it can add bundles.

The bundle gets added but the top cart (on enterprise) shows a price of 0 for the item. The basket total stays the same.

The bundle is configured as fixed price of 0 and the items in the bundle have specific prices (e.g. 3 x 3.99). The bundle is not editable by the customer. We can’t use dynamic price due to the way our integration has been setup.

private function getBundleOptions($product$qty{

        
// if the product is not a bundle just return the quantity
        
if($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE{

            
return $qty;

        
}

        
// get a list of the options
        
$typeInstance $product->getTypeInstance(true);
        
$typeInstance->setStoreFilter($product->getStoreId(), $product);

        
//$optionCollection = $typeInstance->getOptionsCollection($product);

        
$selectionCollection $typeInstance->getSelectionsCollection(
            
$typeInstance->getOptionsIds($product),
            
$product
        
);


        
$bundle_options = array();
        
$bundle_options_qty = array();
        
// loop over the options and build an array
        
foreach($selectionCollection as $option)
        
{
            $bundle_options[$option
->option_id] $option->selection_id;
            
//$bundle_options_qty[$option->option_id] = $option->getSelectionQty();
        
}

        
//$product->setPrice($price);

        
$params = array(
                           
'product' => $product->getId(),
                        
'related_product' => null,
                        
'bundle_option' => $bundle_options,
                        
//'bundle_option_qty' => $bundle_options_qty,
                        
'qty' => $qty,
                    );

        
// return the params
        
return $params;

    
}

$params 
$this->getBundleOptions($product$qty);
Mage::getSingleton('checkout/cart')->addProduct($product$params);

The bundle price is correctly worked out when you goto the actual real basket page. The top cart also gets updated.

The top cart is updated via AJAX.

Could it be that the top cart AJAX is the problem? Or something wrong with my code that adds the params?

 
Magento Community Magento Community
Magento Community
Magento Community
 
luckyknight
Jr. Member
 
Total Posts:  22
Joined:  2010-09-10
 

I’ve noticed that the top cart uses:

Mage_Checkout_Block_Cart_Item_Renderer

after viewing the cart page it uses:

Mage_Bundle_Block_Checkout_Cart_Item_Renderer

Updated my config.. it now uses bundle but price is still 0

 
Magento Community Magento Community
Magento Community
Magento Community
 
luckyknight
Jr. Member
 
Total Posts:  22
Joined:  2010-09-10
 

OK I have this working now. But, when I add multiple quantities to the basket the price is 0 again!!

This is my latest code

public function addBundleToCart($productA$qty)
    
{
        Mage
::log('Bundle - addBundleToCart qty = ' $qty);
        
        
$product $this->_initProduct($productA->getId());
        
        
// if the product is not a bundle just return the quantity
        
if($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE{

            
return $qty;    

        
}

        
//Mage::register('product', $product);
        //Mage::register('current_product', $product);
        
$options $this->getProductBundleOptions($product);
        
$bundle_options = array();
        
$bundle_options_qty = array();
        
        
// loop over the options and build an array
        
foreach($options as $option)
        
{
            $bundle_options[$option
->option_id] $option->selection_id;
            
$bundle_options_qty[$option->option_id] = (int)$option->getSelectionQty(); // * (int)$qty
        
}
        
        $params 
= array(
                        
'product' => (string)$product->getId(),
                        
'related_product' => null,
                        
'qty' => (string)$qty,
                        
'bundle_option' => $bundle_options,
                        
'bundle_option_qty' => $bundle_options_qty,
                  );
        
        
// Magento Default Add to Cart output
        //Array ( [uenc] => aHR0cDovL21hZ2VudG9lZS5sb2NhbC90ZXN0LTIvdGVzdGJ1bmRsZS5odG1sP29wdGlvbnM9Y2FydA,
        //, [product] => 2 [related_product] => [qty] => 1 [bundle_option] => Array ( [1] => 1 ) [bundle_option_qty] => Array ( [1] => 3 ) ) 
        
        // THIS output
        //Array ( [product] => 2 [related_product] => [qty] => 1 [bundle_option] => Array ( [1] => 1 ) [bundle_option_qty] => Array ( [1] => 3 ) ) 
        
        // set flag to recalculate totals - this is required for bundles as price is 0 on fixed product without it
        
Mage::log('reset totals');
        
$this->resetTotalsCache();
        
        
Mage::log('Bundle - addProduct');
        
$cart $this->_getCart();
        
$cart->addProduct($product$params);
        
$cart->save();
        
        
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
luckyknight
Jr. Member
 
Total Posts:  22
Joined:  2010-09-10
 

OK Now I have qts working… but now If I add a simple product, then a bundle afterwards.. price is back to 0

 
Magento Community Magento Community
Magento Community
Magento Community
 
luckyknight
Jr. Member
 
Total Posts:  22
Joined:  2010-09-10
 

Is there a way to refresh the addressCollection in quote object? I am sure that is not updating

 
Magento Community Magento Community
Magento Community
Magento Community
 
otnielgomez
Jr. Member
 
Total Posts:  2
Joined:  2013-06-28
 

Hello, how do you solve this?
I have the exact same issue Adding bundle via code shows 0 price and does not refresh basket total.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top