In this new article I’m going to show you how to create a custom shipping module for Magento 2 platform. Here below you can see the structure of the module:
1. At first let’s register our module in the system. In order to get that done we should create the following files:
app/code/BelVG/NewShipping/etc/module.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="BelVG_NewShipping" setup_version="1.0.0"> </module> </config> |
and app/code/BelVG/NewShipping/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'BelVG_NewShipping', __DIR__ ); |
Magento Custom Development
Take your online store to the next level with BelVG Magento Custom Development
Visit the page2. We should provide an ability for users to configure the new shipping method. That’s why we must create new fields in admin panel (Stores->Configuration->Sales->Shipping Methods). And that is what will help us:
app/code/BelVG/NewShipping/etc/adminhtml/system.php
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 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Config/etc/system_file.xsd"> <system> <section id="carriers" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1"> <group id="belvg_newshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1"> <label>BelVG New Shipping</label> <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Enabled</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> <field id="title" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Title</label> </field> <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Method Name</label> </field> <field id="price" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Shipping Cost</label> <validate>validate-number validate-zero-or-greater</validate> </field> <field id="sallowspecific" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0"> <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" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Ship to Specific Countries</label> <source_model>Magento\Directory\Model\Config\Source\Country</source_model> <can_be_empty>1</can_be_empty> </field> <field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Show Method if Not Applicable</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> <field id="specificerrmsg" translate="label" type="textarea" sortOrder="95" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Displayed Error Message</label> </field> <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Sort Order</label> </field> </group> </section> </system> </config> |
3. Here we create configuration file of our new module:
app/code/BelVG/NewShipping/etc/config.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Magento/Store/etc/config.xsd"> <default> <carriers> <belvg_newshipping> <active>0</active> <model>BelVG\NewShipping\Model\Carrier\Shipping</model> <title>BelVG New Shipping</title> <name>Custom Shipping</name> <price>0</price> <sallowspecific>0</sallowspecific> <specificerrmsg>This shipping method is not available. Please contact us, to use this method.</specificerrmsg> </belvg_newshipping> </carriers> </default> </config> |
4. The next step is creating a model of shipping method, where we can describe the logical circuitry of app/code/BelVG/NewShipping/Model/Carrier/Shipping.php. And here it is necessary to implement these two methods: collectRates and getAllowedMethods.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\DataObject; use Magento\Shipping\Model\Carrier\AbstractCarrier; use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Config; use Magento\Shipping\Model\Rate\ResultFactory; use Magento\Store\Model\ScopeInterface; use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory; use Magento\Quote\Model\Quote\Address\RateResult\Method; use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; use Magento\Quote\Model\Quote\Address\RateRequest; use Psr\Log\LoggerInterface; use Magento\Customer\Model\Session; use Magento\Framework\App\RequestInterface; class Shipping extends AbstractCarrier implements CarrierInterface { /** * Carrier's code * * @var string */ protected $_code = 'belvg_newshipping'; /** * Whether this carrier has fixed rates calculation * * @var bool */ protected $_isFixed = true; /** * @var ResultFactory */ protected $_rateResultFactory; /** * @var MethodFactory */ protected $_rateMethodFactory; public $_productRepo; protected $checkoutSession; protected $quoteIdMaskFactory; /** * @param ScopeConfigInterface $scopeConfig * @param ErrorFactory $rateErrorFactory * @param LoggerInterface $logger * @param ResultFactory $rateResultFactory * @param MethodFactory $rateMethodFactory * @param array $data */ public function __construct( ScopeConfigInterface $scopeConfig, ErrorFactory $rateErrorFactory, LoggerInterface $logger, \Magento\Framework\App\Helper\Context $context, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\App\Http\Context $httpContext, \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ProductRepository $productRepo, ResultFactory $rateResultFactory, MethodFactory $rateMethodFactory, array $data = [] ){ $this->_rateResultFactory = $rateResultFactory; $this->_rateMethodFactory = $rateMethodFactory; $this->_productRepo = $productRepo; $this->checkoutSession = $checkoutSession; $this->customerSession = $customerSession; $this->_storeManager = $storeManager; $this->scopeConfig = $scopeConfig; $this->quoteIdMaskFactory = $quoteIdMaskFactory; parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); } 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(); $method->setCarrier($this->_code); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod($this->_code); $method->setMethodTitle($this->getConfigData('name')); $shippingPrice = $this->getConfigData('price'); $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); return $result; } /** * Get allowed shipping methods * * @return array */ public function getAllowedMethods() { return [$this->_code => $this->getConfigData('name')]; } } |
Magento Extensions
Take your online store to the next level with BelVG Magento extensions
Visit the storeWhereafter we should enable the module by running the following commands:
php bin/magento module:enable -c BelVG_NewShipping
php bin/magento setup:upgrade
And here in admin panel we are able to configure the shipping method:
Stores-> Configuration-> Sales-> Shipping Methods
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.And what concerns the frontend, during the order placement process a user can simply select our new shipping method.
Magento 2 Migration
Take your online store to the next level with BelVG Magento 2 Migration
Visit the page