Estimating shipping rate on product details page in Magento

28Apr10

Here is a very useful functionality that doesn’t come with Magento by default. It is to display the shipping rate of the product in the product details/view page. This clearly gives the user an idea of what would be the shipping cost before going to checkout with other products. It can be used very well with shipping table rate calculation.

You can paste the following code into the ‘/template/catalog/product/view.phtml’ file in your theme.

<?php
if($_product->isSaleable())
{
$quote = Mage::getModel(‘sales/quote’);
$quote->getShippingAddress()->setCountryId(‘DK’);
$quote->addProduct($_product); 

$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$rates = $quote->getShippingAddress()->getShippingRatesCollection();

foreach ($rates as $rate)
{
echo $rate->getPrice();
}
}
?>

The last foreach will give you the shipping rates of different shipping methods enabled. Store it in any variable and display it where ever you want in the view.phtml file. You are done!

(WordPress is converting the single quote ['] to a different charecter, so when you copy paste the code please check if they are understandable by PHP)

Advertisement


No Responses Yet to “Estimating shipping rate on product details page in Magento”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.