2
votes

I tried several ideas I could find at google, but no one did work for me for few days. Finally I found a way to display in a custom extension the "minimal possible price" and the "maximal possible price" for a bundle product in magento:

    $_product_id            = YOUR_BUNDLE_PRODUCT_ID;

    // highest possible price for this bundle product
    $return_type            = 'max'; // because I used this in a helper method

    // lowest possible price for this bundle product
    // $return_type         = 'min';

    $model_catalog_product  = Mage::getModel('catalog/product'); // getting product model
    $_product               = $model_catalog_product->load( $_product_id );

    $TypeInstance           = $_product->getTypeInstance(true);
    $Selections             = $TypeInstance->getSelectionsCollection($OptionIds, $_product );
    $Options                = $TypeInstance->getOptionsByIds($OptionIds, $_product);
    $bundleOptions          = $Options->appendSelections($Selections, true);

    $minmax_pricevalue      = 0; // to sum them up from 0

    foreach ($bundleOptions as $bundleOption) {
        if ($bundleOption->getSelections()) {

            $bundleSelections       = $bundleOption->getSelections();

            $pricevalues_array  = array();
            foreach ($bundleSelections as $bundleSelection) {

                $pricevalues_array[] = $bundleSelection->getPrice();

            }
                if ( $return_type == 'max' ) {
                rsort($pricevalues_array); // high to low
                } else {
                sort($pricevalues_array);   // low to high
                }

            // sum up the highest possible or lowest possible price
            $minmax_pricevalue += $pricevalues_array[0];


        }
    }

    // echo $minmax_pricevalue;
    echo ''.Mage::helper('core')->currency($minmax_pricevalue, true, false).'';

If you have better and shorter ways feel free to post here. Thanks to all involved!

Background of all was that, I have made a custom extension and wanted also to show there the "minimal possible price" and the "maximal possible price" for such a configuration. Magento-Setup was: Native "bundle product" several "Bundle Items Options" connected my "bundle product". Each "bundle option" has multiple simple products with different prices in it. I think that was the point here.

Hope that all helps someone - love to share such stuff :-)

3

3 Answers

9
votes

Magento has build in function for that purpose:

Mage::getModel('bundle/product_price')->getTotalPrices($_product,'min',1);
5
votes

Here once again the final working code:

$_product_id            = YOUR_BUNDLE_PRODUCT_ID;

// highest possible price for this bundle product
$return_type            = 'max'; // because I used this in a helper method

// lowest possible price for this bundle product
// $return_type         = 'min';

$model_catalog_product  = Mage::getModel('catalog/product'); // getting product model
$_product               = $model_catalog_product->load( $_product_id );

$TypeInstance           = $_product->getTypeInstance(true);
$Selections             = $TypeInstance->getSelectionsCollection($OptionIds, $_product );
$Options                = $TypeInstance->getOptionsByIds($OptionIds, $_product);
$bundleOptions          = $Options->appendSelections($Selections, true);

$minmax_pricevalue  = 0; // to sum them up from 0

foreach ($bundleOptions as $bundleOption) {
    if ($bundleOption->getSelections()) {


        $bundleSelections       = $bundleOption->getSelections();

        $pricevalues_array  = array();
        foreach ($bundleSelections as $bundleSelection) {

            $pricevalues_array[] = $bundleSelection->getPrice();

        }
            if ( $return_type == 'max' ) {
            rsort($pricevalues_array); // high to low
            } else {
            sort($pricevalues_array);   // low to high
            }

        // sum up the highest possible or lowest possible price
        $minmax_pricevalue += $pricevalues_array[0];


    }
}

// echo $minmax_pricevalue;
echo ''.Mage::helper('core')->currency($minmax_pricevalue, true, false).'';
0
votes

I had a similar problem a while back and came up with this (with some help from Inchoo's blog for getting the bundled product collection). It loads all of the bundled products associated with the main product_id into an array. By sorting the array you get the min on the bottom and max at the end which I retrieved with $bundled_prices[0] and array_slice($bundled_prices, -1, 1, false).

    $product_id = YOUR_PRODUCT_ID;

    $bundled_product = new Mage_Catalog_Model_Product();
    $bundled_product->load($product_id);
    $selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
            $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
    );
    $bundled_items = array();
    foreach($selectionCollection as $option) { 
        if ($option->getPrice()!="0.0000"){                 
            $bundled_prices[]=$option->getPrice();
        }
    }

    sort($bundled_prices);

    $min_price=$bundled_prices[0];
    $max_price_tmp=array_slice($bundled_prices, -1, 1, false);
    $max_price=$max_price_tmp[0];

    echo "Min: " . $min_price . '<br>'; 
    echo "Max: " . $max_price;