Magento:获得捆绑产品的最高和最低价格

时间:2013-03-01 14:47:50

标签: magento bundle max product min

我尝试了一些我可以在谷歌找到的想法,但几天没有人为我工作。最后,我发现了一种在自定义扩展中显示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).'';

如果你有更好更短的方式随时发布在这里。感谢所有参与者!

背景的是,我已经制作了自定义扩展程序,并希望在此处显示此类配置的“最低可能价格”和“最大可能价格”。 Magento-Setup是:原生“捆绑产品”几个“捆绑产品选项”连接我的“捆绑产品”。每个“捆绑选项”都有多个简单的产品,其中包含不同的价格。我认为这就是重点。

希望所有人都能帮助 - 喜欢分享这些东西: - )

3 个答案:

答案 0 :(得分:9)

Magento为此目的建立了功能:

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

答案 1 :(得分:5)

这里再次提到最终的工作代码:

$_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).'';

答案 2 :(得分:0)

我有一段时间遇到过类似的问题并提出了这个问题(在Inchoo's blog的帮助下获得了捆绑的产品系列)。它将与主product_id关联的所有捆绑产品加载到数组中。通过对数组进行排序,您可以使用$bundled_prices[0]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;
相关问题