Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Set custom shipping method in Magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 365
    Comment on it

    In magento you can add custom shipping in your website by writing some custom code , lets first go through how shipping work in magento ,

    When magento looks for available shipping methods, Magento first collects all available shipping methods. Each shipping method is represented by a class that extends Mage_Shipping_Model_Carrier_Abstract.

    After list of shipping method, shipping information(implemented as Mage_Shipping_Model_Rate_Request) is sent to carrier in order to retrieve all available rates provided by given shipping method, represented as Mage_Shipping_Model_Rate_Result.

    This process happens in Mage_Shipping_Model_Shipping::collectRates() as listed out in code below:

    $carriers = Mage::getStoreConfig('carriers', $storeId);
    foreach ($carriers as $carrierCode => $carrierConfig) {
        $this->collectCarrierRates($carrierCode, $request);

    Here collectCarrierRates() will check all condition like is carrier enabled in admin, is it available for requested country, etc and eventually triggers collectRates() function of your class, which we will implement later.

    That is general outline of what is going on behind the scenes. We are now ready to write some code that will fit nicely into everything explained above. First thing you will need to do, is create new module which depends on Mage_Shipping. Besides standard module configuration, you will have following inside your config.xml:

    And this is how the general flow how shipping methods comes into play ,now we will create a custom shipping module

    1 custom_shipping/carrier Custom Shipping Carrier 10 0 1

    entries like active, sallowspecific and express_max_items are config entries. We will start with model entry. You wil see that carrier will be represented with Custom_Shipping_Model_Carrier, so lets implement that class.

    class Custom_Shipping_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract
        implements Mage_Shipping_Model_Carrier_Interface
        protected $_code = 'custom_shipping';

    interface requires us to implement getAllowedMethods() that will returns array of key-value pairs of all available methods

    public function getAllowedMethods()
        return array(
            'standard'    =>  'Standard delivery',
            'express'     =>  'Express delivery',

    rates are collected by calling collectRates(). This function takes shipping information as parameter, and returns all available rates

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
        $result = Mage::getModel('shipping/rate_result');

    /** @var Custom_Shipping_Helper_Data $expressMaxProducts */ $expressMaxWeight = Mage::helper('custom_shipping')->getExpressMaxWeight(); $expressAvailable = true; foreach ($request->getAllItems() as $item) { if ($item->getWeight() > $expressMaxWeight) { $expressAvailable = false; } } if ($expressAvailable) { $result->append($this->)getExpressRate()); } $result->append($this->_getStandardRate()); return $result; }

    Weight of all products in cart are compared to value stored in config, which determines availability of our express rate. Our standard rate is always available. Each rate is added by passing Mage_Shipping_Model_Rate_Result_Method object to append() of our result object. And we get those rate objects by calling _getExpressRate() and _getStandardRate(),

    protected function _getStandardRate()
        $rate = Mage::getModel('shipping/rate_result_method');
        $rate->setMethodTitle('Standard delivery');
        return $rate;

    now we will be adding admin configuration through system.xml

    <custom_shipping translate="label">
    <active translate="label">
    <title translate="label">
    <sallowspecific translate="label">
    <frontend_class>shipping-applicable-country</frontend_class>                           <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
    <specificcountry translate="label"> <br>
    <express_max_weight translate="label">


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: