我如何根据 php foreach 循环中的项目回显备用消息
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你确定你不想这样做吗:
因为我不相信你想将值传递给 Magento 中的 get 方法。
Are you sure you don't mean to do:
Because I don't believe you want to pass a value to a get method in Magento.
因为您有多个费率需要评估,并且您使用
foreach
循环遍历它们,所以您将收到每个费率的一条消息。需要简单的编程。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.