Create a Custom Shipping Method in Magento

Introduction

Although Magento provides shipping libraries for all popular shipping carriers, you may need to attach your preferred shipping carrier to the checkout. Sometimes you may need to implement your own custom logic, in which case you'll need to create a complete custom shipping method as per your requirements.

Whatever the scenario is, it's easy to create a custom shipping method module in Magento. You just need to create the corresponding configuration files and model files to implement your custom shipping logic. In this article, we'll put this into practice by creating a simple custom module named "Customshippingmethod".

I assume that you're familiar with the basic module creation process in Magento. If not, you can read this article explaining the basics of custom module creation.

A Glance at the Setup

We'll create a simple custom module named "Customshippingmethod". Here's the list of files required for the desired setup.

  • app/etc/modules/Envato_All.xml: It's a file which is used to enable our custom module.
  • app/code/local/Envato/Customshippingmethod/etc/config.xml: It's a module configuration file in which we'll declare our custom shipping method using certain tags as per the Magento conventions.
  • app/code/local/Envato/Customshippingmethod/etc/system.xml: It's a file which is used for the back­-end configuration for our custom shipping method.
  • app/code/local/Envato/Customshippingmethod/Model/Demo.php: It's a model file which is used for the calculation of rates.
  • app/code/local/Envato/Customshippingmethod/Helper/Data.php: It's a helper file used by the Magento translation system.

Creating Files and Folders: Custom Module

First, we need to create a module enabler file. Create a file "app/etc/modules/Envato_All.xml" and paste the following contents in that file. We've used "Envato" as our module namespace and "Customshippingmethod" as our module name. It'll enable our "Customshippingmethod" module by default.

Next, we need to create a module configuration file. Create "app/code/local/Envato/Customshippingmethod/etc/config.xml" and paste the following contents in that file.

In the "config.xml" file, first we've declared the usual models and helpers under the <global> section. The important tag for us is <carriers>, declared under the <default> tag. It tells Magento that we want to attach a new shipping method as per the configuration declared under the <envato_customshippingmethod> tag. The <envato_customshippingmethod> tag should be unique so that it doesn't conflict with other shipping methods.

Other important tags are <sallowspecific> and <model>. The <sallowspecific> tag stands for "Ship to Applicable Countries". It allows you to select the specific countries for which our custom shipping method is applicable. And the <model> tag defines the location of the "Model" file for our custom shipping method.

Next, we'll create a "system.xml" file which provides the back­-end configuration for our custom shipping method. Create "app/code/local/Envato/Customshippingmethod/etc/system.xml" with the following contents.

It'll create the configuration form with the fields defined under the <fields> tag in the back-end. We'll come back to this later.

Let's create a model file "app/code/local/Envato/Customshippingmethod/Model/Demo.php" with the following contents.

Let's understand this in a bit more detail. We've set the $_code property to "envato_customshippingmethod" which was declared earlier in the "config.xml" file. The "getAllowedMethods" method simply returns the array of shipping methods you would like to display in the front­-end checkout. In our case, we have only declared a single shipping method for our carrier, so we'll return that method.

Further, the "collectRates" method is called by Magento to gather the rates for different shipping methods. So we've implemented this as per the convention of Magento. The important method for us is "_getDefaultRate", in which we've defined the logic to calculate the rate for our shipping method. Although we're just using the static price declared in "config.xml" to keep things simple, you can go ahead and calculate the price as per your requirements.

Finally, let's define the helper file at "app/code/local/Envato/Customshippingmethod/Helper/Data.php" which will be used by the Magento translation system.

Now that we've finished with all the heavy work, let's see how it looks! Head over to the back­-end of Magento, enable our custom module, and clear all the caches. Go to System > Configuration > Sales > Shipping Methods. You should see that our custom shipping method is listed with the title Envato Demo Shipping Method as shown in the following screenshot.

Back-End Configuration

Now, let's see how it looks in the front­-end checkout process.

Front-End Preview

As you can see, our shipping method is listed along with the other shipping methods! Although it's a very simple example module to demonstrate the implementation of a custom shipping method, you can go ahead and set up more complex stuff as per your requirements.

Conclusion

Today, we learned how to make a custom shipping method module in Magento. I am sure you've enriched your Magento knowledge to some extent. Shoot your feedback using the feed below!

Tags:

Comments

Related Articles