【问题标题】:Magento: get max and min price for bundle productMagento:获取捆绑产品的最高和最低价格
【发布时间】:2013-03-01 14:47:50
【问题描述】:

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

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

【问题讨论】:

    标签: magento bundle max product min


    【解决方案1】:

    Magento 为此目的内置了函数:

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

    【讨论】:

      【解决方案2】:

      这里又是最终的工作代码:

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

      【讨论】:

      • 对于导致错误的缺少 $OptionIds 可以添加 $OptionIds = $TypeInstance->getOptionsIds($_product);
      【解决方案3】:

      不久前我遇到了类似的问题并想出了这个(在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.0000 和最大值:2604.6200。与 magento 代码的默认捆绑输出相比,我得到最小值:2.052,45 欧元和最大值:4.346,62 欧元。还有什么想法吗?
      • 嘿@seanbreeden,我按照你的建议更改了我的代码 - 但仍然是错误的值 - 我认为整个价格计算有问题。奇怪的是没有人有类似的问题。
      • 这适用于捆绑产品。修改后的代码的值是多少?
      • 所有连接的捆绑项目的值确实是最小值和最大值,但就像我所说的那样。我现在有一个解决方案,将其添加到我上面的帖子中。
      • 此代码不考虑捆绑中禁用的产品。需要在foreach中添加对$option-&gt;getStatus( ) == Mage_Catalog_Model_Product_Status::STATUS_DISABLED的检查
      猜你喜欢
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多