2013-03-01 202 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

相關問題