I have seen many post here about this but none of the solutions have worked for me so I thought I would ask myself posting my own code.
I want a default shipping method to be selected when a customer visits the cart page. From reading other posts I understand it isn't being set because the user hasn't added in their delivery address yet.
Is there a way I can can just set a default delivery method (flat rate) until the user selects another method. This is the .phtml file which shows the delivery methods available:
<?php $sub = Mage::helper('checkout/cart')->getQuote()->getSubtotal() ?>
<div class="shipping">
<div class="shipping-form">
<?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
<form id="co-shipping-method-form" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>">
<?php $quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
if($product->getData('direct_delivery_product')==1){?>
<div class="direct_message">
<?php echo 'You have direct delivery products in your shopping cart. This means it may take longer than the standard 2-5 working days to deliver.';?></div>
<?php
break;
}}?>
<div class="block block-login">
<div class="block-slider">
<ul class="slides">
<li>
<div class="block-title">
<h2><span style="font-size:16px; letter-spacing: normal;"><?php echo $this->__('Shipping Options') ?></span></h2>
</div>
<div class="block-content">
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php /* Extensions placeholder */ ?>
<?php echo $this->getChildHtml('customer.form.login.extra')?>
<?php $count = 0;?>
<form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="login-form">
<ul>
<?php foreach ($_shippingRateGroups as $code => $_rates): ?>
<?php foreach ($_rates as $_rate): ?>
<li<?php if ($_rate->getErrorMessage()) echo ' class="error-msg"';?>>
<?php if ($_rate->getErrorMessage()): ?>
<?php echo $this->escapeHtml($_rate->getErrorMessage()) ?>
<?php else:?>
<input name="estimate_method" type="radio" value="<?php echo $this->escapeHtml($_rate->getCode()) ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio" />
<label for="s_method_<?php echo $_rate->getCode() ?>">
<?php if (($sub > 75 && $count < 1 ) ||(Mage::getSingleton('customer/session')->getCustomerGroupId()==5 && $count < 1 ))
{echo "Free Shipping";}
else{ echo $this->escapeHtml($_rate->getMethodTitle()); }?>
<?php if ($sub == 0 && $count == 0)
{ echo "£4.99";}
else{?>
<?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
<?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
<?php echo $_excl; }?>
</label>
<?php ++$count;?>
<?php endif ?>
<?php endforeach; ?>
<?php endforeach; ?>
</ul> <div class="buttons-set">
<button type="submit" title="<?php echo $this->__('Update Total') ?>" class="button" name="do" value="<?php echo $this->__('Update Total') ?>"><span><span><?php echo $this->__('Update Total') ?></span></span></button>
</div>
<a href="<?php echo $this->getForgotPasswordUrl() ?>" class="forgot-password" id="forgot-password"><?php echo $this->__('Click here if shipping internationally to get a quote') ?></a>
</form>
</div>
</li>
<li>
<div class="block-title">
<strong><span><?php echo $this->__('International Shipping') ?></span></strong>
</div>
<div class="block-content">
<form action="<?php echo $this->getUrl('checkout/cart/estimatePost') ?>" method="post" id="shipping-zip-form">
<p><?php echo $this->__('Enter your destination to get a shipping estimate.') ?></p>
<ul class="form-list">
<li>
<div class="input-box">
<?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?>
</div>
</li>
</ul>
<div class="buttons-set">
<button type="button" title="<?php echo $this->__('Get a Quote') ?>" onclick="coShippingMethodForm.submit()" class="button"><span><span><?php echo $this->__('Get a Quote') ?></span></span></button>
<a href="<?php echo $this->getForgotPasswordUrl() ?>" class="forgot-password" id="back-login"><?php echo $this->__('Back to UK shipping prices'); ?></a>
</div>
</form>
</div>
</li>
</ul>
</div>
</div>
</div>
<?php endif; ?>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('login-form', true);
var dataForgetForm = new VarienForm('form-validate', true);
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var coShippingMethodForm = new VarienForm('shipping-zip-form');
var countriesWithOptionalZip = <?php echo $this->helper('directory')->getCountriesWithOptionalZip(true) ?>;
coShippingMethodForm.submit = function () {
var country = $F('country');
var optionalZip = true;
return VarienForm.prototype.submit.bind(coShippingMethodForm)();
}
//]]>
</script>
</div>
So all I want is the Flat rate to be selected by default in my cart so the user doesn't have to select the price then click update, they should only have to do this if they want a different shipping method.
Thank you if you can help.