Magento – Bundle, only Required Options

bundle

I need to calculate the total price of my bundle and if the required option of this bundle have promo I need to print the price before the promo and after on catalog/view.

For that I have : (helper)

public function bundleItemsPromo($product) {
    $productId = $product->getId();
    $bundled_product = Mage::getModel('catalog/product')->load($productId);



    $selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
        $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
    );

    $bundled_items_reduct = 0;
    $price = 0;
        foreach($selectionCollection as $option)
        {
            if($price < $option->getPrice()) {
                $price = $option->getPrice();
                if ($option->getSpecialPrice()){
                    $bundled_items_reduct += $price - $option->getSpecialPrice();
                }
            }
        }

    return $bundled_items_reduct;
}

In my foreach I just need calculate the promo only if the product is required
I tried:

$bundled_product->getTypeInstance(true)->getSelectionsCollection(
        $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
    )->hasRequiredOptions()

but…

If you have an idea?

Best Answer

class ????_Bundle_Helper_Promo extends Mage_Core_Helper_Abstract {

    /**
     * return the price of required option on bundle, before the promo
     *
     * @param $product
     * @return int
     */
    public function bundleItemsPromo($product) {
        $OptionsCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
    //init bundle option promo
        $bundled_items_reduct = 0;

        foreach($OptionsCollection as $option){
            $price = 0;

            //if option is required
            if($option->getRequired() == 1){

               foreach($option->getSelections() as $productOption){
                    //if option have promo
                    if($productOption->getSpecialPrice()){

                        //init price with the first special price 
                        if ($price==0){
                            $price = $productOption->getPrice();
                            }
                        //look just the smallest price
                        if($price >= $productOption->getPrice()){
                            $price = $productOption->getPrice();
                                //save the price before the promo
                                $bundled_items_reduct += $price -  $productOption->getSpecialPrice();
                        }
                    }
                }
            }
        }

        return $bundled_items_reduct;
    }
}

It works !

This helper returns the price of bundle product before the promo, 1st foreach to find if option is required and 2nd foreach to get option's price.

Related Topic