3

I read the similar question but no luck.

I am working in magento 1.9. I want to display shipping rate in product view page. I have zipcode textbox in product page. After enter zipcode I want to display shipping charges in the same page.

I tried the following code. It is displaying changes but the actuall prices are different that means in checkout page the same zipcode charges are different. and by using following code the charges are different.

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

        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setCountryId('country');
        $shippingAddress->setPostcode('zipcode');
        $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;
            }

        }

Any other way to display fedex charges in product page ? I am using default fedex method.

  • My question apart from your post, I also try to integrate FedEx for my store, in the process of FedEx integration, my I know how you entered weight for products, GRAMS or KG ? – Gem Mar 3 '18 at 4:49
  • Actually, what do you want to achieve exactly? Do you want to see the shipping charge for this (meaning the currently viewed) product or do you want to get the shipping charge for the current cart or do you want to get the charge for the current product plus the current cart? Based on your code I assume the latest is what you want to do? @Rathinam - please consider creating separate questions for your concern. – Christoph Farnleitner Mar 3 '18 at 5:52
  • @ChristophFarnleitner I want to display shipping charges in product view page for current product, For example if user is in category page then if user click on particular product then that particular product detail page will open, so in this page i want to display shipping charge for this particular product. – David Coder Mar 3 '18 at 7:13
  • I suppose you are not using 'country' & 'zipcode' as they are printed in your code (as string) – Raul Sanchez Mar 3 '18 at 8:51
  • No sir I am passing variable for zipcode and country both, yes here i used string for name. – David Coder Mar 3 '18 at 8:53
0

First don't include Model calls within your template files as it seems. Follow best practices with a simple module to avoid issues like this in the future (plus other devs will love you for it!)

Most likely the cause why the code posted isn't working is not using the customer session properly, & not anticipating existing address/cart data. Which would include resetting on subsequent requests.

Also, $shippingAddress = $quote->getShippingAddress(); should be enough without needing to chain the same request in other calls such as: $quote->getShippingAddress()->collectTotals(); $shippingAddress->collectTotals(); would work on the same object as well without the possibility of duplicate objects/properties changing/etc.

Example Model you'll want to follow

Called from the controller:

Hope this helps!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.