How to set default shipping method and country?

All we need is an easy explanation of the problem, so here it is.

How can I set my default shipping method in the shopping cart to the country where my store is?

For instance, I want to set the default shipping method to the UK when the customer are on the shopping cart page.

By Magento default, you have to select your country where you want to ship your order to. Then you have to hit the button ‘Get a quote’ for the ‘Estimate Shipping and Tax’. But I want to skip this step by setting the default shipping method to the UK automatically when you are on this page.

Is it possible?

I tried with this code below,

$shippingAddress = $this->getQuote()->getShippingAddress();
$shippingAddress->setCountryId('UK')->setShippingMethod('tablerate_bestway')->save();

But you have to refresh the shopping cart page for a couple of times before it sets the shipping method to the UK.

I need it set the shipping method to the UK as soon as you land on the shopping cart page though, is it possible?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I used this method just so I could change the css on the <select> element but it makes it easier to “filter” countries and what not as well, which could help you.

Go to /app/design/frontend/THEME_NAME/default/template/checkout/cart/shipping.phtml

Try replacing:

<?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?>

With this:

<?php $_countries = Mage::getResourceModel('directory/country_collection')->loadByStore()->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
    <select name="country_id" id="country" class="validate-select">
        <option value="GB">United Kingdom</option>
        <?php foreach($_countries as $_country): ?>
            if($_country['value']=="GB")
            {
                continue
            }
            else
            {
                <option value="<?php echo $_country['value'] ?>">
                    <?php echo $_country['label'] ?>
                </option>
            }
        <?php endforeach; ?>
    </select>
<?php endif; ?>

Method 2

Override the CartController and add this code in the indexAction

 class Ajaxy_Core_Checkout_CartController extends Mage_Checkout_CartController {  

     public function indexAction() {

            parent::indexAction();
            $shippingAddress = $this->_getQuote()->getShippingAddress();
            if (!$shippingAddress->getShippingMethod()) {
              $shippingAddress->setCountryId('US')->setShippingMethod('freeshipping_freeshipping')->save();
              $shippingAddress->save();
              $this->_getQuote()->setTotalsCollectedFlag(false)->collectTotals();
              $this->_getQuote()->save();
              Mage::getSingleton('checkout/cart')->save();
              $this->_redirect('checkout/cart');
            }

        }
}

this works and refreshes the cart, set the country to ‘US’, shipping to free if the customer have no shipping method

Method 3

An easier way is to change the CartController.php

Inside the indexAction() function. Add

 $this->_getQuote()->getShippingAddress()->setShippingMethod('tablerate_bestway')->save();

More options?

    $this->_getQuote()->getShippingAddress()
                ->setCountryId($country)
                ->setCity($city)
                ->setPostcode($postcode)
                ->setRegionId($regionId)
                ->setRegion($region)
                ->setCollectShippingRates(true);
     $this->_getQuote()->save();

I hope this will help someone.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply