E-commerce
Adding a Custom Shipping Method in Magento 2
How to Add a Custom Shipping Method in Magento 2
Magento 2 is a powerful and flexible e-commerce platform that allows you to customize your shipping methods to meet the specific needs of your business. In this article, we will guide you through the process of adding a custom shipping method to your Magento 2 store.
Step 1: Register Your Custom Module
The first step is to register your custom module. This is done by creating a registration file with the following content:
?phpMagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Dlt_Customshipping',
__DIR__
);
?
Step 2: Module Setup
You need to declare your module using the module.xml file under the etc folder. This file will declare the module to Magento so that the Magento can recognize your custom module:
?xml version1.0?config xmlns:xsi xsi:noNamespaceSchemaLocationurn:magento:framework:Module/etc/module.xsd
module nameDlt_Customshipping setup_version1.0.0/
/config
Step 3: Module Configuration
The core of your custom shipping method is defined in the config.xml file under the etc folder. This file contains configuration nodes that help the system recognize your module:
?xml version1.0?config xmlns:xsi xsi:noNamespaceSchemaLocationurn:magento:module:Magento_Store:etc/config.xsd
default
carriers
customshipping
active1/active
titleCustom Shipping/title
modelDltCustomshipping/Carrier/Shipping/model
/customshipping
/carriers
/default
/config
Step 4: Shipping Model
The actual logic for the custom shipping method is handled in the Shipping class. This class extends the abstract MagentoShippingModelCarrierAbstractCarrier class and implements the MagentoShippingModelCarrierCarrierInterface interface:
?phpnamespace DltCustomshippingModelCarrier;
use MagentoQuoteModelQuoteAddressRateRequest;
use MagentoShippingModelRateResult;
class Shipping extends MagentoShippingModelCarrierAbstractCarrier implements MagentoShippingModelCarrierCarrierInterface
{
protected $_code 'customshipping';
protected $_rateResultFactory;
protected $_rateMethodFactory;
public function __construct(
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
PsrLogLoggerInterface $logger,
MagentoShippingModelRateResultFactory $rateResultFactory,
MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
array $data []
) {
$this->_rateResultFactory $rateResultFactory;
$this->_rateMethodFactory $rateMethodFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
public function getAllowedMethods() {
return [
$this->_code $this-getConfigData('name')
];
}
private function getShippingPrice() {
$configPrice $this-getConfigData('price');
$shippingPrice $this-getFinalPriceWithHandlingFee($configPrice);
return $shippingPrice;
}
public function collectRates(RateRequest $request) {
if (!$this-getConfigFlag('active')) {
return false;
}
$result $this-_rateResultFactory-create();
$method $this-_rateMethodFactory-create();
$method-setCarrier($this-_code);
$method-setCarrierTitle($this-getConfigData('title'));
$method-setMethod($this-_code);
$method-setMethodTitle($this-getConfigData('name'));
$amount $this-getShippingPrice();
$method-setPrice($amount);
$method-setCost($amount);
$result-append($method);
return $result;
}
}
Step 5: Admin Configuration
Each shipping method should have a configuration option in the admin panel to manage shipping rates and other configurations. This is done through the system.xml file under the etc/adminhtml folder:
?xml version1.0?config xmlns:xsi xsi:noNamespaceSchemaLocationurn:magento:module:Magento_Config:etc/system_file.xsd
sections
carriers
groups
customshipping translatelabel
labelCustom Shipping/label
fields
active translatelabel
labelEnabled/label
typetext/type
backend_modelMagentoConfigModelConfigBackendYesno/backend_model
sort_order1/sort_order
show_in_default1/show_in_default
show_in_website1/show_in_website
show_in_store0/show_in_store
/active
name translatelabel
labelName/label
typetext/type
sort_order10/sort_order
show_in_default1/show_in_default
show_in_website1/show_in_website
show_in_store0/show_in_store
/name
title translatelabel
labelTitle/label
typetext/type
sort_order20/sort_order
show_in_default1/show_in_default
show_in_website1/show_in_website
show_in_store0/show_in_store
/title
price translatelabel
labelShipping Price/label
typetext/type
sort_order30/sort_order
show_in_default1/show_in_default
show_in_website1/show_in_website
show_in_store0/show_in_store
/price
/fields
/customshipping
/groups
/carriers
/sections
/config
Final Step
To make your module active, run the following command in the terminal from the Magento root directory. You will need to execute this command every time you create a new module or make changes to the setup directory:
php bin/magento setup:upgradeBy following these steps, you can successfully add a custom shipping method to your Magento 2 store and customize it to fit your business needs.