2013-03-01 196 views
1

我尝试了几个想法,我可以在谷歌找到,但没有人为我工作了几天。最后,我找到了一种方法自定义扩展,以显示“最小可能的价格”和“最大可能的价格”捆绑产品在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).''; 

如果你有更好的和更短的方式随意张贴在这里。感谢所有参与!

背景所有的是,我已经做了一个自定义的扩展,并希望在那里显示这种配置的“最低可能的价格”和“最大可能的价格”。 Magento-Setup是:本地“捆绑产品”几个“捆绑项目选项”连接我的“捆绑产品”。每个“捆绑选项”都有多个不同价格的简单产品。我认为这是重点。

希望所有帮助别人 - 爱分享这种东西:-)

回答

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).''; 
+0

获取错误$ OptionIds未定义。任何线索? – aravind 2016-02-05 16:06:31

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; 
+0

如果我试试这个代码,我得到两个错误的价值观。最小值:0.0000和最大值:2604.6200。与默认代码束输出相比,我得到Min:2.052,45€和Max:4.346,62€。还有什么想法? – infinitelyCODE 2013-03-04 09:32:16

+0

查看我上面的编辑。 – seanbreeden 2013-03-04 13:33:48

+0

嗨@seanbreeden,我改变了我的代码,像你所建议的 - 但仍然是错误的价值观 - 我认为在整个价格计算中出现错误。没有人有类似的问题。 – infinitelyCODE 2013-03-05 09:12:23

8

的Magento已经建立功能用于这一目的:

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

工作完美:)谢谢 – jruzafa 2015-04-23 06:50:03

相关问题