Subesh Pokhrel

Magento Developers Blog

Magento: Setting Up a Default Shipping Method on Cart Page

I recently came to a situation where I need to show a shipping price amount on cart before selecting the shipping address from the checkout page. Basically shipping price is shown after the user has added the shipping address and selected the shipping method. So to show shipping price on cart page load, I needed to find out the following. Shipping Address (Actually only country_id will work) Shipping Method (Set to Flatrate in the code below) So what I did was first check if the user has already specified the shipping address (when user have added the shipping address in the checkout page and navigated back to cart page). If the shipping address is not present then I checked if the user is logged in and checked for default shipping address of the customer and used its country_id (if present), but if the user is not logged in or default shipping address is not present then I set the shipping address (country_id) to some country (NL).After setting the country I then set the shipping method and then saved the quote so that the prices are calculated.Here’s the snippet. [source language=”php”] /*** Setting Default Shipping Method * Checking If Quote Already Has Address Or Not * - If Yes then leave as it is * - Else check if Customer has default Shipping Address or Not * – Yes then get Default Shipping Address and set Shipping Method * – No Set Default Shipping address to NL*/ if(!Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getShippingAddress()->getCountryId()){ $customerSession=Mage::getSingleton("customer/session"); if($customerSession->isLoggedIn()){ $customerAddress=$customerSession->getCustomer()->getDefaultShippingAddress(); if($customerAddress->getId()){ $customerCountry=$customerAddress->getCountryId(); $shipping = Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getShippingAddress()->setCountryId($customerCountry)->setShippingMethod(‘flatrate_flatrate’)->save(); }else{ $shipping = Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getShippingAddress()->setCountryId(‘NL’)->setShippingMethod(‘flatrate_flatrate’)->save(); } } } [/source] Cheers! [UPDATE] The original solution seems to show some issue. Thanks to the great reader of this blog the “perfect” solution for this case can be found in comments. As always new and better solutions are always welcome.