如何根据php foreach循环中的项目回显备用消息

如何根据php foreach循环中的项目回显备用消息,php,magento,foreach,echo,Php,Magento,Foreach,Echo,我试图在Magento的产品页面上显示发货价格。我已经设法做到这一点,使用php来回应航运价格。然而,我现在提供免费送货的某些项目和这些产品,我得到的免费送货价格呼应和另一个航运价格,这看起来是错误的 当一个产品有货时,echo怎么能只提供免费送货,而对于其他所有东西,echo怎么能提供正常价格 我通过使用以下代码成功地呼应了免费送货规则: <?php if($_product->isSaleable()) { $quo

我试图在Magento的产品页面上显示发货价格。我已经设法做到这一点,使用php来回应航运价格。然而,我现在提供免费送货的某些项目和这些产品,我得到的免费送货价格呼应和另一个航运价格,这看起来是错误的

当一个产品有货时,echo怎么能只提供免费送货,而对于其他所有东西,echo怎么能提供正常价格

我通过使用以下代码成功地呼应了免费送货规则:

        <?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());
        }
        ?>


但这仍然显示了其他装运价格。如何停止其他价格显示?

您确定不想这样做:

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

因为我认为您不想将值传递给Magento中的get方法。

因为您要评估多个速率,并且您使用
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());
}
$minPrice=PHP\u INT\u MAX;
foreach($费率为$费率){
$minPrice=min($minPrice,$rate->getPrice());
}
如果($minPrice==0){
echo(“该商品符合免费送货条件”);
}
elseif($minPricegetPrice());
}