Search code examples
phpmagento

How to get bundle product price in Magento?


I have an bundle product as array like this: (take from params when add product to cart)

Array
(
    [product] => 165
    [bundle_option] => Array
    (
        [17] => 47
        [22] => 60
        [16] => 46
        [15] => 42
        [14] => Array
            (
                [0] => 39
            )
    )
)

How could I get price for this bundle product?


Solution

  • Something like this should also work:

    public function getDisplayPrice($product) {
        if($product->getFinalPrice()) {
            return $product->getFormatedPrice();
        } else if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
            $optionCol= $product->getTypeInstance(true)
                                ->getOptionsCollection($product);
            $selectionCol= $product->getTypeInstance(true)
                                   ->getSelectionsCollection(
                $product->getTypeInstance(true)->getOptionsIds($product),
                $product
            );
            $optionCol->appendSelections($selectionCol);
            $price = $product->getPrice();
    
            foreach ($optionCol as $option) {
                if($option->required) {
                    $selections = $option->getSelections();
                    $minPrice = min(array_map(function ($s) {
                                    return $s->price;
                                }, $selections));
                    if($product->getSpecialPrice() > 0) {
                        $minPrice *= $product->getSpecialPrice()/100;
                    }
    
                    $price += round($minPrice,2);
                }  
            }
            return Mage::app()->getStore()->formatPrice($price);
        } else {
            return "";
        }
    }