2

我尝试了几个可以在谷歌上找到的想法,但几天没有人为我工作。最后,我找到了一种在自定义扩展中显示 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 是:本机“捆绑产品”几个“捆绑项目选项”连接了我的“捆绑产品”。每个“捆绑选项”都有多个价格不同的简单产品。我认为这就是重点。

希望一切都对某人有所帮助-喜欢分享这些东西:-)

4

3 回答 3

9

Magento 为此目的内置了功能:

Mage::getModel('bundle/product_price')->getTotalPrices($_product,'min',1);
于 2015-02-05T15:22:32.000 回答
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).'';
于 2013-03-05T13:43:09.837 回答
0

不久前我遇到了类似的问题并想出了这个问题(在Incho 的博客上获取捆绑产品系列的一些帮助)。它将与 main 关联的所有捆绑产品加载product_id到一个数组中。通过对数组进行排序,您可以得到底部的最小值和末尾的最大值,我用$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;
于 2013-03-01T19:08:00.027 回答