Is there a way to show product costs in product detail page in Magento 1.9.1? I'd like for each product to display related shipping cost, really thanks
1 Answer
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!
-
-
1Just one glitch, as not everyone lives in Denmark:
$quote->getShippingAddress()->setCountryId(Mage::getStoreConfig('general/country/default'));
– AnseCommented Sep 22, 2017 at 8:46 -
I've successfully used this option for several years in Magento1 but I've changed to Magento2. How to achieve to show shipping-costs per product in product-page on Magento2– PeterCommented Apr 5, 2020 at 10:44
-
It's not working for me at magento 1.9.4.4. I can debug until $quote->getShippingAddress()->collectTotals(); after it not works. someone can help me? Commented May 24, 2021 at 18:42