2011-05-14 20 views
1

我試圖在Magento的產品頁面上顯示運費價格。我設法通過使用PHP來回應運費價格。但是,我現在提供免費送貨上某些項目和這些產品我得到的免費送貨價格回聲和另一個運費價格看起來不對。我如何根據php foreach循環中的項目回顯備用郵件

如何回聲只有當產品有它的免費送貨,其他一切迴應正常價格?

我設法只是呼應了通過使用下面的代碼的免費送貨規則:

 <?php 
     if($_product->isSaleable()) 
     { 
     $quote = Mage::getModel('sales/quote'); 
     $quote->getShippingAddress()->setCountryId('*'); 
     $quote->addProduct($_product); 
     $quote->getShippingAddress()->collectTotals(); 
     $quote->getShippingAddress()->setCollectShippingRates(true); 
     $quote->getShippingAddress()->collectShippingRates(); 
     $rates = $quote->getShippingAddress()->getShippingRatesCollection(); 

     foreach ($rates as $rate) 
     if ($rate->getPrice(0.00)) 
     { 
      echo ('This item qualifies for FREE shipping'); 
     } 
     else  
      echo ('Shipping from £' . $rate->getPrice()); 
     } 
     ?> 

但這仍顯示其他貨運價格。我怎樣才能阻止其他價格顯示?

回答

0

因爲您有多個費率需要評估,您可以通過foreach循環查看,您將收到每個費率的消息。簡單的編程是必要的。

$minPrice = PHP_INT_MAX; 
foreach ($rates as $rate) { 
    $minPrice = min($minPrice, $rate->getPrice()); 
} 
if ($minPrice == 0) { 
    echo ('This item qualifies for FREE shipping'); 
} 
elseif ($minPrice < PHP_INT_MAX) { 
    echo ('Shipping from £' . $rate->getPrice()); 
} 
+0

太好了 - 非常感謝。完美的作品。 – 2011-05-15 09:49:50

2

你確定你不是說要做:

if ($rate->getPrice()==0) 

因爲我不相信你想要的值傳遞給在Magento get方法。