Adding New Shipping Module In Magento

magento-shipping-module

You must have wondered how to add a new Shipping Module in Magento especially if you don’t find your preferred payment module in existing Magento installation with your preferred Shipping Module. Adding a new Magento Shipping Module in not difficult but requires little bit of programming at your end. In this article i am going to elaborate on how to add a new Shipping module to your existing Magento Installation.

Few Notes:

  1. Just Replace all instances of ‘YourCompany’ with your company name.
  2. Replace ‘NewModule’ with the Payment Module name.
  3. Ensure that the path of the PHP include_path is /app/code/local/
  4. Clean your cache after modifying the config xml files.

Now, you are all set to create a new shipping module of your own. Following is a step by step guide to build your own shipping module, just follow them in exact order and make sure that the above requirements are taken care of.

Set The Configuration

You need to create app/code/local/YourCompany/NewModule/etc/config.xml file and write the following lines of code in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0"?>
<config>
  <modules>
<!-- declare module's version information -->
    <YourCompany_NewModule>
<!-- this version number will be used for database upgrades -->
      <version>0.1.0</version>
    </YourCompany_NewModule>
  </modules>
 
  <global>
<!-- declare model group for new module -->
    <models>
<!-- model group alias to be used in Mage::getModel() -->
      <newmodule>
<!-- base class name for the model group -->
        <class>YourCompany_NewModule_Model</class>
      </newmodule>
    </models>
 
<!-- declare resource setup for new module -->
    <resources>
<!-- resource identifier -->
      <newmodule_setup>
<!-- specify that this resource is a setup resource and used for upgrades -->
        <setup>
<!-- which module to look for install/upgrade files in -->
          <module>YourCompany_NewModule</module>
        </setup>
<!-- specify database connection for this resource -->
        <connection>
<!-- do not create new connection, use predefined core setup connection -->
          <use>core_setup</use>
        </connection>
      </newmodule_setup>
    </resources>
  </global>
</config>

Now, edit app/etc/modules/YourCompany_NewModule.xml file

1
2
3
4
5
6
7
8
9
10
11
12
13
<config>
<!-- ... -->
  <modules>
<!-- ... -->
<!-- declare YourCompany_NewModule module -->
    <YourCompany_NewModule>
      <active>true</active>
      <codePool>local</codePool>
    </YourCompany_NewModule>
<!-- ... -->
  </modules>
<!-- ... -->
</config>

Now, the Magento installation is aware that there is a new Module but nothing will happen as there is no Database selected for this.

manage-mysql-databases-magento

Adding Information Into Adapter

Now, create app/code/local/YourCompany/NewModule/Model/Carrier/ShippingMethod.php file and put the following code in it. You can always set the ShippingMethod name of your choice, depending upon for which Payment Gateway you are making a module.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 
/**
 * Our test shipping method module adapter
 */
class YourCompany_NewModule_Model_Carrier_ShippingMethod extends Mage_Shipping_Model_Carrier_Abstract
{
  /**
   * unique internal shipping method identifier
   * 
   * @var string [a-z0-9_]
   */
  protected $_code = 'newmodule';
 
    /**
     * Collect rates for this shipping method based on information in $request
     *
     * @param Mage_Shipping_Model_Rate_Request $data
     * @return Mage_Shipping_Model_Rate_Result
     */
  public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  {
    // skip if not enabled
    if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
        return false;
    }
 
    /**
     * here we are retrieving shipping rates from external service
     * or using internal logic to calculate the rate from $request
     * you can see an example in Mage_Usa_Model_Shipping_Carrier_Ups::setRequest()
     */
 
    // get necessary configuration values
    $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
 
    // this object will be returned as result of this method
    // containing all the shipping rates of this method
    $result = Mage::getModel('shipping/rate_result');
 
    // $response is an array that we have 
    foreach ($response as $rMethod) {
      // create new instance of method rate
      $method = Mage::getModel('shipping/rate_result_method');
 
      // record carrier information
      $method->setCarrier($this->_code);
      $method->setCarrierTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/title'));
 
      // record method information
      $method->setMethod($rMethod['code']);
      $method->setMethodTitle($rMethod['title']);
 
      // rate cost is optional property to record how much it costs to vendor to ship
      $method->setCost($rMethod['amount']);
 
      // in our example handling is fixed amount that is added to cost 
      // to receive price the customer will pay for shipping method.
      // it could be as well percentage: 
      /// $method->setPrice($rMethod['amount']*$handling/100);
      $method->setPrice($rMethod['amount']+$handling);
 
      // add this rate to the result
      $result->append($method);
    }
 
    return $result;
  }
}

Soon after you have created a Model, just give admin an option to configure it and let checkout process made aware of this (it will show up in checkout process which your users can choose to pay).

How To Develop Admin Module For Shipping Customizations

Once, you are done with the above steps, you need to inform Magento that there is a new Payment Module which needs to be configured and displayed in admin. In order to do so, you need to create app/code/local/YourCompany/NewModule/etc/system.xml file and put the following lines of code in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
 
<?xml version="1.0"?>
<config>
   <sections>
    <carriers>
        <groups>
            <newmodule translate="label" module="shipping">
                <label>Carrier Name</label>
                <frontend_type>text</frontend_type>
                <sort_order>13</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>
                   <fields>
                      <account translate="label">
                            <label>Account number</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>7</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </account>
                        <active translate="label">
                            <label>Enabled</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </active>
                        <contentdesc translate="label">
                            <label>Package Description</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>12</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </contentdesc>
                        <!--
                        If the free_shipping_enable flag enable, the system will check free_shipping_subtotal to give free shipping
                        otherwise will use shopping cart price rule behaviour
                        -->
                        <free_shipping_enable translate="label">
                            <label>Free shipping with minimum order amount</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_enabledisable</source_model>
                            <sort_order>21</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </free_shipping_enable>
                        <free_shipping_subtotal translate="label">
                            <label>Minimum order amount for free shipping</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>22</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </free_shipping_subtotal>
                        <dutiable translate="label">
                            <label>Shipment Dutiable</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>13</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </dutiable>
                        <gateway_url translate="label">
                            <label>Gateway URL</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </gateway_url>
                        <handling_type translate="label">
                            <label>Calculate Handling Fee</label>
                            <frontend_type>select</frontend_type>
                            <source_model>shipping/source_handlingType</source_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </handling_type>
                        <handling_action translate="label">
                            <label>Handling Applied</label>
                            <frontend_type>select</frontend_type>
                            <source_model>shipping/source_handlingAction</source_model>
                            <sort_order>11</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </handling_action>
                        <handling_fee translate="label">
                            <label>Handling fee</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>12</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </handling_fee>
                        <max_package_weight translate="label">
                            <label>Maximum Package Weight (Please consult your shipping carrier for maximum supported shipping weight)</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>13</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </max_package_weight>
                        <id translate="label">
                            <label>Access ID</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
                            <sort_order>5</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </id>
                        <password translate="label">
                            <label>Password</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
                            <sort_order>6</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </password>
                        <shipping_intlkey translate="label">
                            <label>Shipping key (International)</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
                            <sort_order>8</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </shipping_intlkey>
                        <shipping_key translate="label">
                            <label>Shipping key</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
                            <sort_order>8</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </shipping_key>
                        <sort_order translate="label">
                            <label>Sort order</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>100</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </sort_order>
                        <title translate="label">
                            <label>Title</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </title>
                        <sallowspecific translate="label">
                            <label>Ship to applicable countries</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>90</sort_order>
                            <frontend_class>shipping-applicable-country</frontend_class>
                            <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </sallowspecific>
                        <specificcountry translate="label">
                            <label>Ship to Specific countries</label>
                            <frontend_type>multiselect</frontend_type>
                            <sort_order>91</sort_order>
                            <source_model>adminhtml/system_config_source_country</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </specificcountry>
                        <showmethod translate="label">
                            <label>Show method if not applicable</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>92</sort_order>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </showmethod>
                        <specificerrmsg translate="label">
                            <label>Displayed Error Message</label>
                            <frontend_type>textarea</frontend_type>
                            <sort_order>80</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </specificerrmsg>
                    </fields>
                </newmodule>
            </groups>
        </carriers>
    </sections>
</config>

Once this file is saved you will find your new Payment Module under System> Configuration > Shipping Methods. It’s now up to you to add your custom fields in the fields tag, and subsequently make your configuration do something constructive.

Please leave me a comment and let me know if you run into any problems implementing the above code.

  • anon

    An important detail that you forgot to mention – default values for your module's administration fields in the config.xml, and specifically a node with xpath config/default/carriers/yourshippingmethod/model set to the classname or alias of your model so that magento actually knows what to call.
    You can also see the shipping methods that come default with magento for examples.

  • jalexantonyvijay

    I am new for Magento. I don't know how to run new module in Magento. after add a new module, where we can find the module in magento. Please give the explanation.

  • jalexantonyvijay

    I am new for Magento. I don't know how to run new module in Magento. after add a new module, where we can find the module in magento. Please give the explanation.

  • Pingback: Magento – WOW! | web2.0goodies.com

  • antony_k

    I had add your code to create new shipping module
    it works fine under System> Configuration > Shipping Methods. But the problem after creating new shipping module when you want to get invoice from sales->orders it leads to an error page.

    Please reply a solution immediately

  • A4K4A

    This error was fixed. Simply deleting all the uninstalled shipping module files (carriers/”whatever_old_shipping_carrier_name_here”) from my (core_config_data) database, refreshed the cache. Thus got rid of this error.

  • Saurabh Periwal

    How Track Order works? I mean what part of code?

  • http://www.magentomagik.com/ MagentoMagik

    This code is to understand how you can add support for new payment gateways in your default Magento installation. For example; What if you to use 2checout as your payment gateway and you don't find support for that. In such cases you can create a new module and custom code to accommodate 2checkout in as your preferred payment gateway.
    Order tracking is something different. You have to understand the core logic of orders and tracking separately.

  • http://www.magentomagik.com/ MagentoMagik

    You will find your new module at System> Configuration > Shipping Methods

  • Saurabh Periwal

    Hi,

    Do you know any such tutorial

    Regards,
    Saurabh

  • http://www.magentomagik.com/ MagentoMagik
  • My

    please delete this thread, as it is not working at all

  • http://www.magentomagik.com/ Ashish @Magento Themes

    This post was written keeping the older version in mind, now you have to make some modifications to support latest Magento version.

  • Shahidmalook

    What kind of Modification I need to support the latest magento, please help

  • http://scottsdalebusinessblog.com Mike Milster

    Extremely beneficial. Thank you.

  • Nadeem_gomal

    not show at the front side…………..
    can anyone help me.

  • Fuck cock

    OMG Fuck Fuck!

  • Magento22

    I just tried this last night on 1.5 and it shows up on the backend. When I add items to my cart and it tries to display the checkout screen with the rates, it throws an error in saying “Fatal error: Call to a member function setStore() on a non-object in shipping/model/shipping.php on line 192″

    Is this a new problem or is this related to the fact that I don’t have the “modifications” you’re referring to in your post?

  • Mike

    Any chance of helping out with what modifications are needed to make it work? Anything would be a helping hand!

  • http://www.howtosexercise.com/ Sexercise

    It really is good post, but I don’t see everything completely clear, specifically for someone not associated with that topic. Anyway quite interesting in my experience.

  • http://0creditcards.posterous.com/ Gaye Munaz

    F*ckin’ tremendous things here. I am quite glad to see your article. Thanks a great deal and i’m looking forward to contact you. Will you please drop me a e-mail?

  • Ivo Barbosa

    hello, i’m with magento 1.5 i try this but, the module appears in admin but not in fronten.
    Please help-me

  • Imran

    Hi! i tried this its working in admin panel under shipping methods but on the order page getting sort of error please help me in this……..

  • Vishnu Dutt1990

    I m using magento 1.4.1.1 and i was using ups shipping and i just add their acess key user id or pass and now it is showing no method avilable ……….PLz Reply fast as can

  • Vishnu Dutt1990

    I m using magento 1.4.1.1 and i was using ups shipping and i just add their acess key user id or pass and now it is showing no method avilable ……….PLz Reply fast as can

  • richard lapesigue

    have you enabled it in admin panel?

    Im with 1.5 as well but I used this tutorial:http://inchoo.net/ecommerce/magento/custom-shipping-method-in-magento/

  • Manish Prakash

    I have written a detailed blog here on creating a  shipping method here
    http://www.excellencemagentoblog.com/magento-create-custom-shipping-method

  • Nelly_fayez

    everything run great but it appear on admin and i enabled it but not appear when making new order

  • http://twitter.com/mosheshperling Moshe Shperling

    B”H

    hey i have created a shipping module using your example but i canot apply a cart rule on it. Could you tell please what could be the problem?

  • Sanjoy De

    Do not show the shipping method in backend and frontend  which is creating  by me as new shipping method, please help me if any wrong or changes.

  • http://www.taxresolutionprofessionals.com/ Tax Attorney

    Very well explain, I have near about same problem in magento, got alot of help from this post. 

  • Ssdev3105

     I have create new shipping module but not working

    Thanks
    Ashwani kumar Yadav
    Suarwnasoft

  • Vinhphuoc91

    fuck magento
    http://tin4teen.vn

  • http://www.fmeextensions.com/ Magento Modules

    wondeful post! keep it up

  • http://www.heartdisksolutions.com/ HeartDisk

    You did great job as well but you have to write full path of the folder

  • http://www.heartdisksolutions.com/ HeartDisk

    Nice job can you please tell me how do i add low shipping order fee in cart and in checkout

  • Bella Paris

    does anyone know how to add a handling time to magento shipping?