【问题标题】:How can i echo alternate message depending on items within a php foreach loop我如何根据 php foreach 循环中的项目回显备用消息
【发布时间】:2011-05-14 19:21:27
【问题描述】:

我正在尝试在 Magento 的产品页面上显示运费。我已经设法通过使用 php 来回显运费来做到这一点。但是,我现在为某些商品提供免费送货服务,并且在这些产品上,我得到了与免费送货价格相呼应的免费送货价格和另一个看起来错误的送货价格。

当一个产品有免费送货时,如何才能只呼应,而其他一切都呼应正常价格?

我已经设法通过使用以下代码来呼应免费送货规则:

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

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

【问题讨论】:

    标签: php magento foreach echo


    【解决方案1】:

    你确定你不是故意的:

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

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

    【讨论】:

      【解决方案2】:

      因为您有多个要评估的费率,并且您使用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());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多