Create a New Module as MyShip
moduel.xml file
#app/code/Jeff/MyShip/etc/module.xml
registration.php file
#app/code/Jeff/MyShip/registration.php
Install the new module by typing commands in the console window:
php bin/magento module:enable Jeff_MyShip php bin/magento setup:upgrade
Create system.xml config file for the new Shipping Method
This will create the shipping configuration parameters near all the other shipping methods
#app/code/Jeff/MyShip/etc/adminhtml/system.xml Magento\Config\Model\Config\Source\Yesno Magento\Config\Model\Config\Source\Yesno Magento\Config\Model\Config\Source\Yesno shipping-applicable-country Magento\Shipping\Model\Config\Source\Allspecificcountries Magento\Directory\Model\Config\Source\Country 1
Create config.xml file to set up some default settings
#app/code/Jeff/MyShip/etc/config.xml Jeff\MyShip\Model\Carrier\MyShip 1 Myship Shipping Myship Shipping 1 Express delivery 4 1 Business delivery 5 This shipping method is currently uavailable. If you wuold like to ship using this shipping method, please contact us.
After clear cache, you can find our new shipping method is shown up at the backend as following
Adding a model with the business logic for the new shipping method
The new model class will be used for making the shipping method available, calculating the shipping costs, and setting the title in the frontend
#app/code/Jeff/MyShip/Model/Carrier/MyShp.php _logger = $logger; $this->_rateResultFactory = $rateResultFactory; $this->_rateMethodFactory = $rateMethodFactory; parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); } public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $request) { if(!$this->getConfigFlag('active')) { return false; } //$message = $this->getConfigFlag('active')? 'yes' : 'no'; $this->_logger->info($this->getConfigData('business_price')); $result = $this->_rateResultFactory->create(); //check if express method is enabled if($this->getConfigData('express_enabled')) { $method = $this->_rateMethodFactory->create(); $method->setCarrier($this->_code); $method->setCarrierTitle($this->getConfigData('name')); $method->setMethod('express'); $method->setMethodTitle($this->getConfigData('express_title')); $method->setPrice($this->getConfigData('express_price')); $method->setCost($this->getConfigData('express_price')); $result->append($method); } //check if business method is enabled if($this->getConfigData('business_enabled')) { $method = $this->_rateMethodFactory->create(); $method->setCarrier($this->_code); $method->setCarrierTitle($this->getConfigData('name')); $method->setMethod('business'); $method->setMethodTitle($this->getConfigData('business_title')); $method->setPrice($this->getConfigData('business_price')); $method->setCost($this->getConfigData('business_price')); $result->append($method); } return $result; } public function getAllowedMethods() { return ['myship' => $this->getConfigData('name')]; } public function isTrackingAvailable() { return true; } }
The frontend shipping methods will be like following
I hope my tutorial will help you create you own shipping method easily.