2

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?

1
  • Don't forget, the ISO alpha-2 code for the United Kingdom is GB.
    – Nick
    May 22, 2015 at 12:30

3 Answers 3

2

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; ?>
6

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

0
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.

1
  • Great Job Man :)
    – Shiv Singh
    Jul 13, 2017 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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