Shipping methods affect your sale to considerable extent and so it is advisable that you customize it for the products in your store. You may want to provide free shipping for specific billing amount criteria or fast delivery on products that you want to encourage the sale of. With the upgradation of Magento 1 to Magento 2, you may need guidelines on creating custom shipping methods in Magento 2.
Here are the steps to create custom shipping method in Magento 2:
- The very first step you need to follow is defining your config.xml file, which is located at:
app/code/Magecomp/Flatshipping/etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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> <flatshipping > <active>1</active> <sallowspecific>0</sallowspecific> <model>Magecomp\Flatshipping\Model\Carrier\Flatshipping</model> <name>Magecomp Flat Shipping</name> <price>5.00</price> <title>Magecomp Flat Shipping Method</title> <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg> <handling_type>F</handling_type> </flatshipping > </carriers> </default> </config> |
- Now we need to define the model, which is already defined in config.xml under the tag. In model, we calculate the shipping cost. File is located at:
app/code/local/Magecomp/Flatshipping/Model/Flatshipping.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 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 |
<?php namespace Magecomp\Flatshipping\Model\Carrier; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Shipping\Model\Rate\Result; class Flatshipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements \Magento\Shipping\Model\Carrier\CarrierInterface { /** * @var string */ protected $_code = 'flatshipping'; /** * @var \Magento\Shipping\Model\Rate\ResultFactory */ protected $_rateResultFactory; /** * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory */ protected $_rateMethodFactory; /** * @param \Magento\Framework\App\Config\ScopeConfigInterface * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory * @param \Psr\Log\LoggerInterface * @param \Magento\Shipping\Model\Rate\ResultFactory * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory * @param array */ 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; } /** @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')); $amount = $this->getShippingPrice(); $method->setPrice($amount); $method->setCost($amount); $result->append($method); return $result; } } |
-
Now We create system.xml file at app/code/Magecomp/Flatshipping/etc/adminhtml/system.xml
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 |
<?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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1"> <group id="flatshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Magecomp Flat Shipping Method</label> <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1"> <label>Enabled</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Method Name</label> </field> <field id="price" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1"> <label>Price</label> <validate>validate-number validate-zero-or-greater</validate> </field> <field id="handling_type" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1"> <label>Calculate Handling Fee</label> <source_model>Magento\Shipping\Model\Source\HandlingType</source_model> </field> <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> <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Sort Order</label> </field> <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Title</label> </field> <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1"> <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> <frontend_class>shipping-skip-hide</frontend_class> </field> <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Displayed Error Message</label> </field> </group> </section> </system> </config> |
Let me know if you have any difficulty in the implementation. Also, leave your feedback and suggestions in the comment section below.