在Magento,我如何在产品页面上放置物品运费?

时间:2011-04-12 20:01:47

标签: php zend-framework magento shipping

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

2 个答案:

答案 0 :(得分:4)

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

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

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

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

答案 1 :(得分:3)

改进了一点:

            <!-- 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-->

顺便说一下:这只适用于简单的产品。还没有时间打电话给相应的简单产品。

相关问题