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

发布于 2024-11-07 10:22:57 字数 1008 浏览 5 评论 0原文

我正在尝试在 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());
        }
        ?>

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

I am trying to display shipping prices on a product page in Magento. I have managed to do this by using php to echo the shipping prices. However, i am now offering free shipping on certain items and on these products i am getting the free shipping price echoed and another shipping price which looks wrong.

How can echo only free shipping when a product has it, and for everything else echo the normal price?

I have managed to just echo out the free shipping rule by using the following code:

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

But this still displays the other shipping price. How can i stop the other price displaying?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音 2024-11-14 10:22:57

你确定你不想这样做吗:

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

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

Are you sure you don't mean to do:

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

Because I don't believe you want to pass a value to a get method in Magento.

流年里的时光 2024-11-14 10:22:57

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

Because you have more than one rate to assess and you loop through them with a foreach you are going to get a message for each rate. Straightforward programming is needed.

$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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文