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

inf*_*ODE 1 bundle product max min magento

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

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

所有的背景是,我已经做了一个自定义扩展,并希望在那里显示这种配置的"最小可能的价格"和"最大可能的价格".Magento-Setup是:原生"捆绑产品"几个"捆绑产品选项"连接我的"捆绑产品".每个"捆绑选项"都有多个简单的产品,其中包含不同的价格.我认为这就是重点.

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

pbo*_*gut 9

Magento为此目的建立了功能:

Mage::getModel('bundle/product_price')->getTotalPrices($_product,'min',1);
Run Code Online (Sandbox Code Playgroud)


inf*_*ODE 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).'';
Run Code Online (Sandbox Code Playgroud)