This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How To Create Shipping Module In Magento 2

Updated on  26th February

6 Min Read
Reading Time: 6 minutes

A shipping module in Magento 2 defines the medium of delivery for the purchased product. It determines the shipping charge that the customer pays during checkout. The shipping fee can be fixed at $5 flat rate on any order, or a dynamically calculated cost through a real-time shipping service. This is based on the details of the order and shipment like weight, origin, destination, etc.

Magento 2 Shipping Module

Magento 2 already has many built-in shipping methods, but if you need to create a custom shipping method, then this guide is for you. Today, I am going to show you how to create a shipping module in Magento 2.

Let’s start by creating a custom module in Magento 2. If you are not familiar with it, check out this guide on Create Module in Magento 2.

First of all, create a configuration and registration file of the module. Create module.xml in Magento2Root/app/code/Cloudways/Shippingmodule/etc and paste the following code:

Cloudways Magento Hosting For Developers

Create a registration.php in app/code/Cloudways/Shippingmodule with the following code:

Now, create a class which will handle the Shipping Method. Keep in mind that the shipping method should be defined in Cloudways/Shippingmodule/etc/config.xml file. Without this file, shipping method won’t work.

In config.xml file, the parent node is <default>, and its child node is <carriers> which further defines the property with the same name as the $_code in shipping class. It also has a <model> node that defines the shipping class Cloudways\Shippingmodule\Model\Carrier\Shippingmodule.

Create config.xml file and add the following code:

As we all know, every shipping method should have configuration options in the admin panel. Let’s add shipping method options through the system.xml file that can be created in Cloudways\Shippingmodule\etc\adminhtml directory with the following code:

Last but not the least, I have to create a Shipping Class as defined in the <model> node in config.xml file. Create Shippingmodule.php file in Cloudways\Shippingmodule\Model\Carrier directory and add the following code to it:

In this class, you can see I have added some Magento 2 rules. Every Magento 2 shipping class extends \Magento\Shipping\Model\Carrier\AbstractCarrier and implements the \Magento\Shipping\Model\Carrier\CarrierInterface class. I have also defined a variable $_code with its value. This value is related to the config.xml file node structure.

Moreover, I have created two functions getAllowedMethods and collectRates. These methods are required by the abstract class and interface.

Function collectRates has a parameter $request which is the instance of RateRequest class. This Magento\Quote\Model\Quote\Address\RateRequest class contains all information about the items in the cart: quote, weight, shipping/billing address, etc. In this method, I have implemented all logic for shipping calculation.

That’s it, you’re all done! Now, to enable this extension, connect to your Magento hosting server via SSH and run the following CLI command in the root directory of your Magento 2 store:

Now let’s check out the result! Log in to your Magento 2 Admin panel, navigate to STORES > Configuration, expand the SALES section from the left panel and click Shipping Methods.

Magento Manage Sales

Here, you’ll see that the new shipping Method has been added. Enable it and set the values according to your need.

Shipping Module Enable

If you have implemented everything as it is, your Magento 2 checkout page will have a shipping method.

Magento Review Payments

I hope you found this post useful 🙂 If you have any question or need help, I’d be happy to help you out. Feel free to get in touch by commenting in the box below.

Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.