How to Create Custom Shipping Method in Magento 2?

Blog Overview

Shipping is indeed one of the most significant aspects of e-commerce. Shipping in eCommerce refers to all the services that are required to transport the products purchased online from a retailer to a customer’s delivery destination. Here are the steps to create a custom shipping method.

Step 1 : Create the 2 mandatory files:

Refer this blog and create module.xml file inside etc folder and registration.php file inside Code5fixer/Simpleshipping folder. Here, Simpleshipping is the name of the module folder.


Step 2 : Add the module configuration :

To add a module configuration, we have to create system.xml file. Therefore, create a folder inside etc folder and name it as adminhtml. Inside this adminhtml folder, create an xml file and name it as system.xml

The contents of Index.php file will be:



<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
        <section id="carriers" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">
            <group id="simpleshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Code5Fixer Simple Shipping Method</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Method Name</label>
                <field id="price" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                    <validate>validate-number validate-zero-or-greater</validate>
                <field id="handling_type" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                    <label>Calculate Handling Fee</label>
                <field id="handling_fee" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Handling Fee</label>
                    <validate>validate-number validate-zero-or-greater</validate>
                <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Sort Order</label>
                <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                    <label>Ship to Applicable Countries</label>
                <field id="specificcountry" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Ship to Specific Countries</label>
                <field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Show Method if Not Applicable</label>
                <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Displayed Error Message</label>

The above system.xml file declares the following custom shipping module options:

  • Enabled
  • Title
  • Method Name
  • Shipping Cost
  • Ship to Applicable Countries
  • Ship to Specific Countries
  • Show Method if Not Applicable
  • Sort Order

Step 3 : Create config.xml file

The config.xml file specifies default values for custom shipping module options. Now, we shall create config.xml file inside etc folder. Hence, create an xml file inside etc folder and name it as config.xml


The contents of this file will be:


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
                <name>Code5fixer Sample Shipping Method</name>
                <title>Code5fixer Sample Shipping Method</title>
                <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>

Step 4: Create the carrier model

This file,Shipping.php class is a skeleton of a carrier model. We can extend it as per our needs. Now, let us create a folder inside our module folder and name it as Model. Inside this Model folder create a folder and name it as Carrier. Inside this Carrier folder, create a php file and name it as Shipping.php


The contents of this file will be :


namespace Code5fixer\Simpleshipping\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;

class Shipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
     * @var string
    protected $_code = 'simpleshipping';

     * @var \Magento\Shipping\Model\Rate\ResultFactory
    protected $_rateResultFactory;

     * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
    protected $_rateMethodFactory;

     * Shipping constructor.
     * @param \Magento\Framework\App\Config\ScopeConfigInterface          $scopeConfig
     * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory  $rateErrorFactory
     * @param \Psr\Log\LoggerInterface                                    $logger
     * @param \Magento\Shipping\Model\Rate\ResultFactory                  $rateResultFactory
     * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
     * @param array                                                       $data
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
        array $data = []
    ) {
        $this->_rateResultFactory = $rateResultFactory;
        $this->_rateMethodFactory = $rateMethodFactory;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);

     * get allowed methods
     * @return array
    public function getAllowedMethods()
        return [$this->_code => $this->getConfigData('name')];

     * @return float
    private function getShippingPrice()
        $configPrice = $this->getConfigData('price');

        $shippingPrice = $this->getFinalPriceWithHandlingFee($configPrice);

        return $shippingPrice;

     * @param RateRequest $request
     * @return bool|Result
    public function collectRates(RateRequest $request)
        if (!$this->getConfigFlag('active')) {
            return false;

        $result = $this->_rateResultFactory->create();
        $method = $this->_rateMethodFactory->create();



        $amount = $this->getShippingPrice();



        return $result;

This carrier class implements the CarrierInterface interface and retrieves all available shipping methods in the getAllowedMethods() function. The collectRates function returns the \Magento\Shipping\Model\Rate\Result object if the carrier method is available on checkout. Otherwise, it returns false—the carrier method is not applicable to the shopping cart.

Step 5 : Run all the necessary commands

We need to run commands like setup:upgrade, static-content:deploy, setup:di:compile, reindex and cache:flush from the Magento root directory.

To know more about more magento commands, refer this blog.

Now, our admin will show like this

And, in the Checkout Page, we will see this Custom Shipping Method as shown below.

We hope, with the help of the above steps, you will be easily getting the product collection in Magento2. Also, share this blog with your developer friends.

You could also download the above module here

After downloading this module:

  1. Extract the downloaded zip file
  2. Paste the extracted Code5fixer folder into /app/code/ folder in your magento root directory.
  3. Run all the necessary 5 commands and check for output.

This module is developed using Magento ver. 2.4.3-p1 version. But if you try this in older or newer versions, it may give some errors.

You could also refer to this demo video for better understanding.

We hope our guide would be very useful for you. If you have any questions, feel free to reach us anytime by leaving a comment below.

Check out our new blogs here

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x