【问题标题】:In Magento, how can i put an items shipping cost on the product page?在 Magento 中,我如何在产品页面上放置商品的运费?
【发布时间】:2011-08-04 04:44:45
【问题描述】:

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

【问题讨论】:

    标签: php zend-framework magento shipping


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我需要在哪里添加此代码?我在 view.phtml 文件中尝试过,但它对我没有用。我收到了这个错误:Fatal error: Call to a member function setStore() on a non-object in app/code/core/Mage/Shipping/Model/Shipping.php on line 424
    【解决方案2】:

    稍微改进一下:

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

    顺便说一句:这只适用于简单的产品。还没来得及调用相应的简单产品。

    【讨论】:

      猜你喜欢
      • 2016-09-16
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      相关资源
      最近更新 更多