1

I am using FedEx shipping carrier, I would like to display shipping rate on product detail page.

I tried following codes in the product view page (catalog/product/view.phtml)

...
if($_product->isSaleable())
{
$quote = Mage::getModel('sales/quote');
$quote->getShippingAddress()->setCountryId('US')->setPostcode('27007');
$quote->addProduct($_product); 
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$rates = $quote->getShippingAddress()->getShippingRatesCollection();

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

Also, I tried another code

...
if($_product->isSaleable())
    {
$shippingblock = $this->getLayout()->createBlock('checkout/cart_shipping');
        $quote = Mage::getModel('sales/quote');

        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setCountryId('US');
        $shippingAddress->setPostcode('27007');
        $shippingAddress->setCollectShippingRates(true);


        $quote->addProduct($_product);
        $quote->getShippingAddress()->collectTotals();
        $quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->getShippingAddress()->collectShippingRates();

        $rates = $quote->getShippingAddress()->getGroupedAllShippingRates();


        if(empty($rates)){
             echo  Mage::helper('shipping')->__('There are no rates available');
        }


        foreach ($rates as $code=>$rate) {
            $carierName = '<div class="cariername">'
                . $shippingblock->getCarrierName($code)
                .'</div>';
            echo $carierName;

            foreach($rate as $r){
                $price = Mage::helper('core')->currency($r->getPrice(), true, false);
                $rates  = '<div>'
                    . $r->getMethodTitle()
                    . ' <strong>'
                    . $price
                    .' </strong>';
                echo $rates;
            }

        }
}
.....

Both the code won't display shipping rate as expected.

Any answer appreciate.

  • Your both approach seems worked for me, can you please ensure your shipping method configuration. I got Flat Rate & United Parcel Service rates according to my shipping method configuration. This might be shipping configuration issue. – Jayesh Patel Feb 4 '16 at 10:03
  • My shipping rate working fine on shopping cart page as well as checkout page. Any other advice? – Kishor Hase Feb 4 '16 at 10:20
  • Can you please share your view.phtml whole file content in your question? – Jayesh Patel Feb 4 '16 at 10:22
  • Are you using FedEx shipping rate service client? A file named with "RateWebServiceClient.php5" if yes could you please tell me the location of this file? or you are using default magento's Fedex method? I can help you a lot. – Anup Chaudhary Feb 4 '16 at 11:01
  • I tried this is works but getting different rate compare to checkout page. I am using default fedex method of magento 1.9. I also want to display the rate in product view page I use this but rate is coming different then checkout page so any idea ? – David Coder Feb 28 '18 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.