Estimating shipping rate on product details page in Magento
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.
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)
Filed under: Magentocommerce, PHP | Leave a Comment
Tags: magento, php, shipping rates
No Responses Yet to “Estimating shipping rate on product details page in Magento”