First of all, you have to register and configure a custom module. Start with configuration and then do the module registration. In app/code/Mageants/CustomShipping/etc
create module.xml.
After that add the code mentioned below in order to do the module configuration:
<?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="Mageants_CustomShipping" setup_version="2.0.0">
</module>
</config>
In app/code/Mageants/CustomShipping
create registration.php. After that mention the code given below for the modul registration: <?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mageants_CustomShipping',
__DIR__
);
In app/code/Mageants/CustomShipping/etc/adminhtml
create systme.xml for configurtion of shipping methods and add the code we have mentioned below:
<?xml version="1.0"?>
<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="1" showInDefault="1" showInWebsite="1" showInStore="1">
<group id="mageants_customshipping" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mageants Custom 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="2" 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="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Displayed Error Message7lt;/label>
</field>
<field id="sallowspecific" translate="label" type="select" sortOrder="90" 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="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Sort Order</label>
</field>
</group>
</section>
</system>
</config>
In app/code/Mageants/CustomShipping/etc.
, create config.xml for the default configuration to set the methods of custom shipping. Add the code provided below to it:
<?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>
<mageants_customshipping>
<active>0</active>
<sallowspecific>0</sallowspecific>
<price>0</price>
<model>Mageants\CustomShipping\Model\Carrier\Customshipping</model>
<name>Custom Shipping</name>
<title>Mageants Custom Shipping</title>
<specificerrmsg>This shipping method is not available. To use this
shipping method, please contact us.</specificerrmsg>
</mageants_customshipping>
</carriers>
</default>
</config>
We in this step are going to create a class for the shipping model as we have already defined it in the above step. app/code/Mageants/CustomShipping/Model/Carrier
code we will create customshipping.php. Now please follow the code we have mentioned below and add it:
<?php
namespace Mageants\CustomShipping\Model\Carrier;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Quote\Model\Quote\Address\RateRequest;
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\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Config;
use Magento\Shipping\Model\Rate\ResultFactory;
use Psr\Log\LoggerInterface;
class Customshipping extends AbstractCarrier implements CarrierInterface {
/**
* Carrier's code
*
* @var string
*/
protected $_code = 'mageants_customshipping';
/**
* Whether this carrier has fixed rates calculation
*
* @var bool
*/
protected $_isFixed = true;
/**
* @var ResultFactory
*/
protected $_rateResultFactory;
/**
* @var MethodFactory
*/
protected $_rateMethodFactory;
/**
* @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,
ResultFactory $rateResultFactory,
MethodFactory $rateMethodFactory,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* Generates list of allowed carrier`s shipping methods
* Displays on cart price rules page
*
* @return array
* @api
*/
public function getAllowedMethods() {
return [$this->getCarrierCode() => __($this->getConfigData('name'))];
}
/**
* Collect and get rates for storefront
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @param RateRequest $request
* @return DataObject|bool|null
* @api
*/
public function collectRates(RateRequest $request) {
/**
* Make sure that Shipping method is enabled
*/
if (!$this->isActive()) {
return false;
}
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();
$shippingPrice = $this->getConfigData('price');
$method = $this->_rateMethodFactory->create();
/**
* Set carrier's method data
*/
$method->setCarrier($this->getCarrierCode());
$method->setCarrierTitle($this->getConfigData('title'));
/**
* Displayed as shipping method under Carrier
*/
$method->setMethod($this->getCarrierCode());
$method->setMethodTitle($this->getConfigData('name'));
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
return $result;
}
}
Once you get done with the creation of the model class, you need to run commands in order to enable the shipping method.
-> php bin/magento module:status
-> php bin/magento setup:upgrade
-> php bin/magento setup:di:compile
-> php bin/magento setup:static-content:deploy
-> php bin/magento cache:clean
-> php bin/magento cache:flush
For that you need to follow the steps mentioned below:
If you get this as your result then it is an implication that you have been successful at creating your custom Magento shipping method.
We hope that the above method was helpful for you and you can now easily create your custom shipping method and add it to your e-commerce Magento 2 store. A custom shipping method will allow you to attract your target customers and that will ultimately increase your conversion rates. Magento 2 is all new and it is to be used in the upcoming year as a replacement of its previous version which is going off-air now. It is better to get hands-on experience with the various features of Magento 2.
Sign In
Create New Account