4

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.

6

In line 43

<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" />

the checkbox is being checked (echo checked = "checked") if the condition

$_rate->getCode()===$this->getAddressShippingMethod() is true.

I'd suggest adding something to the condition like -

([[existing condition]] || $rate->getCode()=="flatrate")

With this, you're allowing it to be checked when this condition is true, even though the original one isn't.

Hope this points you in the right direction.

3

Previous answer seem to be correct, additionally, I just took a quick look at this code, if you want to be a good developer, first of all, NEVER do like this:

{echo "Free Shipping";}

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.';

please, make all the strings being translatable (you can do it with: $this->__('Free Shipping');) and try to follow Magento coding standards.

2

Place below script at the end in checkout>cart>shipping.phtml file

<script type="text/javascript">
  jQuery("#Id_Of_Your_Shipping_Method").attr("checked", true);
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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