How to create a custom Magento 2 Shipping method

1. Description

As you all know, Magento 2 already has a shipping method, but in case the customer or shop owner needs to add a custom shipping method for the convenience and convenience of shoppers, we can create a custom shipping method. In this tutorial, I will show you how to create a custom Magento 2 shipping method.

To add a new shipping carrier to the Magento check out, first of all, create a new module.

2. Create new module

Step 1: Create a new module.

Here we take Magepow as the namespace for the module.

Create registration.phpin app/code/Magepow/CustomShipping/registration.php with the following code.


use Magento\Framework\Component\ComponentRegistrar;

Create module.xml with the sequence of 4 modules Magento_Store, Magento_Sales, Magento_Quote, Magento_SalesRule. In app/code/Magepow/CustomSipping/etc/module.xml with the following code.

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magepow_CustomShipping" >
<module name="Magento_Store"/>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_SalesRule"/>

Step 2: Create system and config files.

Create system.xml in app/code/Magepow/CustomSipping/etc/adminhtml/system.xml with the following code.

<?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="customshipping" translate="label" type="text" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Shipping</label>
<field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
<field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Method Name</label>
<field id="shipping_cost" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0" >
<label>Shipping Cost</label>
<validate>validate-number validate-zero-or-greater</validate>
<field id="sallowspecific" translate="label" type="select" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<label>Ship to Applicable Countries</label>
<field id="specificcountry" translate="label" type="multiselect" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Ship to Specific Countries</label>
<field id="showmethod" translate="label" type="select" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Show Method if Not Applicable</label>
<field id="sort_order" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Sort Order</label>

Create config.xml in app/code/Magepow/CustomSipping/etc/config.xml with the following code.

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<title>Custom Shipping Title</title>
<name>Custom Shipping Method Name</name>

Step 3: Create Shipping Model.

In this example, the Magepow \ CustomShipping \ Model \ Carrier \ Customshipping class is a frame of the model carrier. You can expand it to suit your needs.

The carrier-class implements the CarrierInterface interface and retrieves all the available carrier methods in getAllowedMethoddo. The collectorates function returns the \ Magento \ Shipping \ Model \ Rate \ Result object if the shipping method is available on the checkout counter. Otherwise, it will return false— the vendor method did not apply to the cart.

Create Customshipping.php in app/code/Magepow/CustomSipping/Model/Carrier/Customshipping.php with the following code.

namespace Magepow\CustomShipping\Model\Carrier;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
* Custom shipping model
class Customshipping extends AbstractCarrier implements CarrierInterface
* @var string
protected $_code = 'customshipping';
* @var bool
protected $_isFixed = true;
* @var \Magento\Shipping\Model\Rate\ResultFactory
private $rateResultFactory;
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
private $rateMethodFactory;
* @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 = []
) {
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
$this->rateResultFactory = $rateResultFactory;
$this->rateMethodFactory = $rateMethodFactory;
* Custom Shipping Rates Collector
* @param RateRequest $request
* @return \Magento\Shipping\Model\Rate\Result|bool
public function collectRates(RateRequest $request)
if (!$this->getConfigFlag('active')) {
return false;
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->rateResultFactory->create();
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->rateMethodFactory->create();
$shippingCost = (float)$this->getConfigData('shipping_cost');
return $result;
* @return array
public function getAllowedMethods()
return [$this->_code => $this->getConfigData('name')];

Step 4: Run the command to register the module.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

3. Config

When the upgrade is completed, start configuring the module in the admin panel.

Admin Panel > Store > Configuration > Sales > Shipping Methods > Custom Shipping.
  • Title: Title of shipping method.
  • Method Name: Name of shipping method.
  • Shipping Cost: Amount customers need to pay when choosing this shipping method.
  • Ship to Applicable Countries: Select the countries that apply to this mode of transportation.
  • Ship to Specific Countries: Options to a Specific country.
  • Sort Order: Location of the new shipping method custom.

Once configured and saved, when purchasing and checking out we have shipping method options.

Hope this article will help you in some way, You can see useful articles in the next articles.

Anything you need support from Magento 2 feels free to contact us at Alothemes and

Phone: (+84)865633728