Magento Automatically select shipping method based on customer group

Posted by Damodar Bashyal on August 06, 2013

 

Today's task was to auto select shipping method based on customer group. For this website only approved and logged in customers were allowed to proceed to checkout.

Checkout

Because of multi-store magento website first I created configuration setting as:

file: /trunk/wholesale/app/code/local/Technooze/Checkout/etc/system.xml

<?xml version="1.0"?>
<config>
    <sections>
        <checkout  translate="label" module="checkout">                    
            <label>Checkout</label>
            <tab>sales</tab>
            <frontend_type>text</frontend_type>
            <sort_order>0</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>            
            <groups>
                <shippingmethod translate="label">
                    <label>Shipping</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>200</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <autoselect_shipping translate="label">
                            <label>Auto select shipping based on customer group</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>0</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </autoselect_shipping>
                    </fields>
                </shippingmethod>
            </groups>
        </checkout>
    </sections>
</config>

Now we can enable this feature only on selected websites. Another step was to override method's available block:

file: /trunk/wholesale/app/code/local/Technooze/Checkout/etc/config.xml

    <global>
        ...
        <blocks>
            <technooze_checkout>
                <class>Technooze_Checkout_Block</class>
            </technooze_checkout>
            <checkout>
                <rewrite>
                    <onepage_shipping_method_available>Technooze_Checkout_Block_Onepage_Shipping_Method_Available</onepage_shipping_method_available>
                </rewrite>
            </checkout>
        </blocks>
        ...
    </global>

Then override getShippingRates() method:

File: /trunk/wholesale/app/code/local/Technooze/Checkout/Block/Onepage/Shipping/Method/Available.php

<?php
class Technooze_Checkout_Block_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
    public function getShippingRates()
    {
        $rates = parent::getShippingRates();
        $selected = $this->getAddressShippingMethod();
        $select = Mage::getStoreConfig('checkout/shippingmethod/autoselect_shipping');
        $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        /*
         * Groups:
         * 0: Guest
         * 1: T member
         * 2: S account customer
         * 3: S proforma customer
         * 4: SF sale customer
         * 5: newsletter testers
         */
        if($select){
            $selected = false;
        }
        foreach($rates as $code => $_rates){
            foreach ($_rates as $_rate){
                if(empty($selected)){
                    switch($groupId){
                        case 0: break;
                        case 1: break;
                        case 2:
                            if($_rate->getCode() === 'flatrate_flatrate'){
                                $quote = $this->getQuote()->getShippingAddress();
                                $quote->setShippingMethod($_rate->getCode());
                                $quote->save();
                                $selected = $this->getAddressShippingMethod();

                                // below code will only display selected option, comment out this if you want to display all options
                                return array($code => $rates[$code]);
                            }
                            break;
                        case 3:
                            if($_rate->getCode() === 'tablerate_bestway'){
                                $quote = $this->getQuote()->getShippingAddress();
                                $quote->setShippingMethod($_rate->getCode());
                                $quote->save();
                                $selected = $this->getAddressShippingMethod();

                                // below code will only display selected option, comment out this if you want to display all options
                                return array($code => $rates[$code]);
                            }
                            break;
                        case 4: break;
                        case 5: break;
                        default:
                    }
                }
            }
        }
        return $rates;
    }
}

Checkout

Thanks to magepsycho for his blog post: http://www.blog.magepsycho.com/hide-other-shipping-methods-when-free-shipping-is-enabled/

 
not published on website


QR Code: Magento Automatically select shipping method based on customer group