In this blog we will learn how to create custom shipping method in Magento 2. Magento is big ecommerce platform. There are few shipping methods by default provided by Magento 2. But, still it’s not full fill requirement to merchant. At that time, We need to create custom shipping method in our store.
Step 1: Create registration.php
App/code/Bluethinkinc/CustomShipping/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethinkinc_CustomShipping', __DIR__ ); |
Step 2: Create module.xml file at
app/code/Bluethinkinc/CustomShipping/etc/module.xml and add below code to this file:
1 2 3 4 5 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethinkinc_CustomShipping"> </module> </config> |
Setp 3: Create system.xml file at
app/code/Bluethinkinc/CustomShipping/etc/adminhtml/system.xml and add below code to this file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="carriers" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label"> <group id="customshipping" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label"> <label> Custom Shipping Method Group</label> <field id="active" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select"> <label>Enabled</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> <field id="name" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text"> <label>Method Name</label> </field> <field id="price" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="text"> <label>Price</label> <validate>validate-number</validate> </field> <field id="sort_order" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="40" translate="label" type="text"> <label>Sort Order</label> </field> <field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="50" translate="label" type="text"> <label>Title</label> </field> <field id="sallowspecific" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="60" translate="label" type="select"> <label>Ship to Applicable Countries</label> <frontend_class>shipping-applicable-country</frontend_class> <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model> </field> <field id="specificcountry" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="70" translate="label" type="multiselect"> <label>Ship to Specific Countries</label> <can_be_empty>1</can_be_empty> <source_model>Magento\Directory\Model\Config\Source\Country</source_model> </field> <field id="specificerrmsg" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="80" translate="label" type="textarea"> <label>Displayed Error Message</label> </field> </group> </section> </system> </config> Setp 3: Create config.xml file at app/code/Bluethinkinc/CustomShipping/etc/config.xml and add below code to this file: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <carriers> <customshipping> <active>1</active> <sallowspecific>0</sallowspecific> <model>Bluethinkinc\CustomShipping\Model\Shipping</model> <name>My Custom Shipping Method</name> <price>43.00</price> <title>My Custom Shipping Method</title> <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg> <handling_type>F</handling_type> </customshipping> </carriers> </default> </config> |
Setp 4:Create Shipping.php file at
app/code/Bluethinkinc/CustomShipping/Model/Shipping.php and add below code to this file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php namespace Bluethinkinc\CustomShipping\Model; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Rate\Result; use Magento\Ups\Helper\Config; use Magento\Shipping\Model\Carrier\AbstractCarrier; class Shipping extends AbstractCarrier implements CarrierInterface { const CODE = 'customshipping' protected $_code = self::CODE; protected $_isFixed = true; /** * @var \Magento\Shipping\Model\Rate\ResultFactory */ protected $rateResultFactory; /** * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory */ protected $rateMethodFactory; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; /** * @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 \Magento\Store\Model\StoreManagerInterface $storeManager * @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, \Magento\Store\Model\StoreManagerInterface $storeManager, array $data = [] ) { $this->logger = $logger; $this->rateResultFactory = $rateResultFactory; $this->rateMethodFactory = $rateMethodFactory; $this->storeManager = $storeManager; parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); } /** * @param RateRequest $request * @return Result|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function collectRates(RateRequest $request) { if (!$this->getConfigFlag('active')) { return false; } /** @var \Magento\Shipping\Model\Rate\Result $result */ $result = $this->rateResultFactory->create(); $storeId = $this->storeManager->getStore()->getId(); $price = $this->getConfigData('price'); $method = $this->rateMethodFactory->create(); if (isset($price)) { $method->setCost(0); $method->setPrice($price); // By default price if price value is blank } else{ $method->setCost("0"); $method->setPrice("30"); } /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */ $method->setCarrier($this->_code); $method->setCarrierTitle($this->getConfigData('title')); /* Use method name */ $method->setMethod($this->_code); $method->setMethodTitle($this->getConfigData('name')); $result->append($method); return $result; } /** * @return array */ public function getAllowedMethods() { return [$this->_code => __($this->getConfigData('name'))]; } } |
Step 5: That’s all to create a custom shipping method in Magento 2. Once you have created Magento 2 custom shipping method, it’s configuration is shown in the backend:
Setp 6: Once the custom shipping method is configured and enabled, it’s shown in the frontend:
Aman Singh
2023-11-21