在 Magento 中,如何在产品页面上添加商品运费?

发布于 2024-10-31 20:54:20 字数 1069 浏览 0 评论 0原文

我想在 Magento 的实际产品页面上显示运费,以便客户可以看到他们需要花多少钱。我可以从购物篮页面添加运费估算器。但我真正想要的是产品价格下的一行文字,上面写着从 £XX.XX 发货

我已经看过本教程:http://aneeshsreedharan.wordpress.com/2010/04/28/estimating-shipping-rate-on -product-details-page-in-magento/#comment-10 但它在 Magento 1.4.2 上对我不起作用 - 我认为本教程使用的是旧版本。

我目前使用的是基于重量的表费率运输方法,只有一种选择。

编辑:我最终解决了问题:相当尴尬的是,我没有意识到博客正在剥离格式。它将 ' 更改为 '

我现在可以确认下面的代码确实显示了运费:

<?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)
{
echo $rate->getPrice();
}
}
?>

I want to put a shipping cost on the actual product page in Magento, so customers can see how much it will cost them. I can add in the shipping estimator from the basket page. But all i actually want is a line of text under the product price saying shipping from £XX.XX

I have seen this tutorial: http://aneeshsreedharan.wordpress.com/2010/04/28/estimating-shipping-rate-on-product-details-page-in-magento/#comment-10 but it is not working for me on Magento 1.4.2 - i think this an older version is used on this tutorial.

I am using a weight based table rate shipping method at the moment, with only one option.

EDIT: I worked out the problem in the end: Rather embarrasingly I didnt realise the blog was stripping the formatting out. It was changing the ' to a ‘

I can now confirm the code below does display the shipping:

<?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)
{
echo $rate->getPrice();
}
}
?>

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

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

发布评论

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

评论(2

仄言 2024-11-07 20:54:20

我最终解决了这个问题:相当尴尬的是,我没有意识到博客正在剥离格式。它将 ' 更改为 '

我现在可以确认下面的代码确实显示发货:

<?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)
{
echo $rate->getPrice();
}
}
?>

我已经编辑了我的原始帖子 - 但要确认上面的代码是否有效。

I worked out the problem in the end: Rather embarrasingly I didnt realise the blog was stripping the formatting out. It was changing the ' to a ‘

I can now confirm the code below does display shipping:

<?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)
{
echo $rate->getPrice();
}
}
?>

I have edited my original post - but to confirm the code above works.

寒尘 2024-11-07 20:54:20

稍微改进一下:

            <!-- SHOW SHIPPING RATES-->
        <?php $quote = Mage::getModel('sales/quote');
        $quote->getShippingAddress()->setCountryId('ES'); // Set your default shipping country here
        $_product->getStockItem()->setUseConfigManageStock(false);
        $_product->getStockItem()->setManageStock(false);
        $quote->addProduct($_product);
        $quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->getShippingAddress()->collectTotals();
        $rates = $quote->getShippingAddress()->getShippingRatesCollection();
        // Find cheapest rate
        $cheapestrate = null;
        foreach ($rates as $rate) {
            if (is_null($cheapestrate) || $rate->getPrice() < $cheapestrate) {
                $cheapestrate = $rate->getPrice();
            }
        }
        $corehelper = Mage::helper('core');
        if ($cheapestrate) {
            echo '<p><strong>Shipping costs:</strong> ' . $corehelper->currency($cheapestrate);?></p>
            <?php
            }else {
            echo "<strong>Free shipping</strong> for this product." ;
        }?>
        <!-- END SHOW SHIPPING RATES-->

顺便说一句:这只适用于简单的产品。还没有时间去调用相应的简单产品。

Improved it a bit:

            <!-- SHOW SHIPPING RATES-->
        <?php $quote = Mage::getModel('sales/quote');
        $quote->getShippingAddress()->setCountryId('ES'); // Set your default shipping country here
        $_product->getStockItem()->setUseConfigManageStock(false);
        $_product->getStockItem()->setManageStock(false);
        $quote->addProduct($_product);
        $quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->getShippingAddress()->collectTotals();
        $rates = $quote->getShippingAddress()->getShippingRatesCollection();
        // Find cheapest rate
        $cheapestrate = null;
        foreach ($rates as $rate) {
            if (is_null($cheapestrate) || $rate->getPrice() < $cheapestrate) {
                $cheapestrate = $rate->getPrice();
            }
        }
        $corehelper = Mage::helper('core');
        if ($cheapestrate) {
            echo '<p><strong>Shipping costs:</strong> ' . $corehelper->currency($cheapestrate);?></p>
            <?php
            }else {
            echo "<strong>Free shipping</strong> for this product." ;
        }?>
        <!-- END SHOW SHIPPING RATES-->

Btw: This only works with simple products. Haven't had the time yet to call the corresponding simple product.

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