LinkedIn Twitter RSS Reset

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.

/*** 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();
			}
		}
	}

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.

21 Responses to “Magento: Setting Up a Default Shipping Method on Cart Page”

  1. icvu
    September 30, 2010 at 3:00 am #

    Thanks for the posting. It helped me.

    However, I added this code and it only show shipping after checkout/cart is loaded the second time.

    Where do you place this code snippet to allow checkout/cart to show shipping on the first load?

    Thanks for you help.

    icvu

    • Shaun E
      October 16, 2010 at 11:18 am #

      This is a great snippet, but I agree with icvu, I am not entirely sure where to place this code. Is there an event that we should write a listener for, a contoller or model we should override? If you have a moment it would be fantastic if you could share with us where you posted this.
      Thanks again, really good stuff.

  2. greg
    October 29, 2010 at 11:48 am #

    +1 for, where does this code go?

    thanks

    • admin
      October 29, 2010 at 11:52 am #

      You can add this on cart page.

      • greg
        October 29, 2010 at 10:36 pm #

        which file is this?

  3. Mem
    October 29, 2010 at 12:35 pm #

    you mean
    template\checkout\cart.phtml

    or

    \Mage\Checkout\controllers\CartController.php
    indexAction

    ??

    • admin
      October 31, 2010 at 11:21 am #

      The template file cart.phtml file.

  4. Cristian
    November 10, 2010 at 3:59 am #

    I see the logic behind the code but where exactly in cart.phtml should I put it?

    • admin
      November 11, 2010 at 9:27 am #

      Probably at the top of cart.phtml.

  5. gegerino
    November 23, 2010 at 2:26 pm #

    I’ve set the code to the top of cart.phtml.
    The Code only works if the cart will be loaded the second time.

  6. August 24, 2011 at 11:20 am #

    I couldn’t get this to work either, however I did find this working solution -> http://icodesnip.com/snippet/php/setting-default-shipping-method-in-magento

  7. valentin
    October 16, 2011 at 12:51 am #

    Has anyone figured out why it only shows the shipping method on the second time you load the cart page?

  8. daria
    November 2, 2011 at 3:54 pm #

    works like a charm
    thanks :)

  9. November 2, 2011 at 4:19 pm #

    just an update
    the script works very well with logged users
    However i had to change the code ( parenthesis ) at this point

    if($customerAddress->getId()){
    …….
    }
    }else{
    …..

    }
    }

    in order to work fine with guess users

    Anyway, thank you again :)

  10. November 3, 2011 at 8:57 am #

    in order to resolve the refresh issue, you need to add these lines of code before the “main if” closure

    Mage::getSingleton(‘checkout/session’)->setCartWasUpdated(true);
    Mage::getModel(‘checkout/cart’)->init();
    Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart/’));
    return;

    Maybe should be better-written. This is the first “dirty” solution :)

    • November 25, 2011 at 2:03 pm #

      it works. I think the script in the post should be updated.

  11. November 15, 2011 at 10:02 am #

    This blog was so well written and was so incredibly informative. Even Comments are also useful in understanding the topic

  12. valentin
    November 20, 2011 at 7:43 pm #

    Hi Daria,

    Your solution helped me a lot! Thank you! I searched for it for weeks with no luck :)

    A quick side question, maybe someone knows. If i want to show that shipping cost in the cart sidebar how do you pull the information?

    • November 30, 2011 at 8:30 pm #

      Hi

      I have done something similar for an ajax mini-cart box (we don’t use the sidebar)

      Here il the code of a function in my helper class

      public function setShippingCosts(){
      /*** Setting Default Shipping Method USING SESSION OBJECTS!!!!*/
      $session = Mage::getSingleton(‘checkout/session’);
      $quote = $session->getQuote();
      $address = $quote->getShippingAddress();

      $sessionCost = $session -> getQuoteShippingCost();
      $sessionCountry = $session -> getQuoteShippingCountry();

      if ((!$sessionCost || $sessionCost == 0) ) {
      $country = null;
      $customerSession = Mage::getSingleton(“customer/session”);
      if ($customerSession -> isLoggedIn()) {
      $customerAddress = $customerSession -> getCustomer() -> getDefaultShippingAddress();
      if ($customerAddress != null) {
      if ($customerAddress -> getId()) $country = $customerAddress -> getCountryId();
      }
      }
      if ($country == null) $country = ‘FR’;

      $address->setCountryId($country)->setCollectShippingRates(true)->collectShippingRates();
      $quote->setShippingMethod(‘flatrate_flatrate’)->save();

      $method = $quote->getShippingMethod();

      if ($method) {
      foreach ($address->getAllShippingRates() as $rate) {
      if ($rate->getCode()==$method) {
      $session->setQuoteShippingCost($address->getQuote()->getStore()->convertPrice($rate->getPrice(), false));
      Mage::getSingleton(‘checkout/type_onepage’)->getQuote()->getShippingAddress()->setCountryId($country)->setShippingMethod($method)->save();
      break;
      }
      }
      }
      //$session->getQuote()->getShippingAddress()->setCollectShippingRates(false)->removeAllShippingRates()->save();
      $session->getQuote()->save();
      $session->resetCheckout();

      }else{
      //log
      }
      }

      I call this function everytime the user accesses to the mini-cart, working this way:

      $this->getHelper()->setShippingCosts();
      $quote = Mage::getSingleton(‘checkout/session’)->getQuote();
      $gTotal = (int)$quote->getGrandTotal() ;
      $totals = $quote->getTotals();

      if(!isset($totals['shipping']))
      $gTotal = ( $gTotal + Mage::getSingleton(‘checkout/session’)->getQuoteShippingCost());

      As you can see, I save my shipping price value in a session variable

      This is not a perfect solution but it works fine :)

      Hope this helps :)

  13. December 5, 2011 at 6:46 pm #

    This is what worked for me. This eliminates the need to refresh the page in the Magento Cart.
    $shippingAddress = $this->getQuote()->getShippingAddress();

    if (!$shippingAddress->getCountryId()) {
    $shippingAddress->setCountryId() ==’US’;}

    if ($shippingAddress->getCountryId() ==’US’) {
    if($shippingAddress->getShippingMethod() ==”){
    $shippingAddress->setShippingMethod(‘msmultiflat_Standard US Shipping’)->save();
    $this->getQuote()->save();
    Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart/’));

    }
    }else{
    if ($shippingAddress->getCountryId() !=’US’) {
    if($shippingAddress->getShippingMethod() ==”){
    $shippingAddress->setShippingMethod(‘fsmultiflat_Standard International Shipping’)->save();
    $shippingAddress->save();
    Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart/’));

    }
    }
    }

  14. Lehoangel
    February 24, 2012 at 3:35 pm #

    Great! This solution works perfectly and seem to be the only one ;)

Leave a Comment