Magento – Adding bundle product to basket – prices are zero

addtocartbundled-productcartmini-cartprice

I know this problem is common, but the answers on the internet has not helped me, so I turn to you.

I'm adding simple products to my basket and that works fine, but I also want to be able to add bundled product, I've come this far (see code below). It adds the products nicely, but the price is not updated properly. If I press update item in the "mini cart" (an ajax cart) or go to the checkout page, it recalculates the prices fine and shows them in the cart and on the checkout page.

I'm thinking, is't a flag that I'm missing, but I cannot seem to figure this bit out.

// Initialize cart
$cart = Mage::getSingleton('checkout/cart');
$cart->init();

// If the items already is in the basket, delete them to avoid duplicates
$cart->truncate();
$cart->save();

foreach($entries as $entry){

    $quantity = $entry->getQuantity();
    $productId = intval($entry->getProductId());
    $product = Mage::getModel('catalog/product')->load($productId);

    // Resetting $params
    $params = [];

    // Setting default params (quantity)
    $params['qty'] = (float)$quantity;

    // If current item is of type bundled product
    if($product->getTypeId() == "bundle"){
         $isBundle = true;

        // Getting all the default options for the product
        $bundled_items = [];
        $optionCollection = $product->getTypeInstance()->getOptionsCollection();
        $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
        $options = $optionCollection->appendSelections($selectionCollection);

        /**
         * Looping through and creating the bundled options array for passing
         * the addProduct()
         */
        foreach($options as $option) {
            $_selections = $option->getSelections();

            foreach($_selections as $selection) {
                $bundled_items[$option->getOptionId()][] = $selection->getSelectionId();
                $bundled_items_option[$option->getOptionId()] = $selection->getSelectionQty();
            }
        }

        // Setting all the options for the bundle
        $params['bundle_option'] = $bundled_items;
        $params['bundle_option_qty'] = $bundled_items_option;
        $params['related_product'] = null;
        $params['product'] = $productId;
    }


    /**
     * Don't know why this has to be here, but every tutorial online has
     * it. I think it's because the quantity can be a decimal and there
     * is a difference between countries on how to do that.
     */
    if(isset($params['qty'])){
        $filter = new Zend_Filter_LocalizedToNormalized([
            'locale' => Mage::app()->getLocale()->getLocaleCode()
        ]);
        $params['qty'] = $filter->filter($params['qty']);
    }

    /**
     * Get the product by id
     */
    $product = Mage::getModel('catalog/product')->load($productId);

    // Finally added the product to the cart
    $cart->addProduct($product, new Varien_Object($params));

}

// Set a flag to inform the cart to recalculate cart prices
$cart->getQuote()->setTotalsCollectedFlag(false);
$cart->save();

// Set a flag to indicate, that the cart has been updated.
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

I have tried to set some flags for allowing the bundle options to be saved, but that did not do the trick. I also tried to add a custom price to the product before adding it to cart, but no luck.

I would be really pleased to get some inputs on how to solve this problem.

I should say, that all my bundled products have a fixed price, that the parent has. All prices on children are zero.

// Ulrik

Best Answer

It looks like your parameters don't have the right keys. From my $buyRequest reference:

Bundle Products

  • options: Array with the selected options in the form

    option_id => selection_id[]
    
  • bundle_option_qty: Array with quantities of the selected options in the form:

    option_id => qty
    

So please try to replace

$params['bundle_option'] = $bundled_items;

with

$params['options'] = $bundled_items;
Related Topic