Magento Provides a pre-install shipping method to use. But sometimes Store owner required shipping method which is not available by default so In this blog post will discuss how to create Shipping Method in Magento 2.
Magento Provides the following Shipping method which we can use as per the requirements.
- Flat Rate
- Free Shipping
- Table Rate Shipping
- Magento Shipping
To create Shipping Method in Magento 2 follow below steps.
- Step 1: Create a Simple Module
- Step 2: Create system.xml File
- Step 3: Create config.xml File
- Step 4: Create a Shipping Model
Step 1: Create a Simple Module
For creating a shipping method we need a custom module Rk_StorePickup where we can define our configuration and add custom logic. Follow this link Create Module in Magento 2
Step 2: Create system.xml File
system.xml file is used to add configuration option in Admin panel under the Shipping Method section in STORES > Configuration > SALES > Shipping Methods.
Create system.xml file at app/code/Rk/StorePickup/etc/adminhtml/system.xml and add below code.
<?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="storepickup" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label"> <label>storepickup</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="shipping_cost" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="text"> <label>Price</label> <validate>validate-number validate-zero-or-greater</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>
Step 3: Create config.xml File
The config.xml file use to specifies default values for the shipping module options like price, title, status shipping model etc.
Create config.xml file at app/code/Rk/StorePickup/etc/config.xml and add below code.
<?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> <storepickup> <model>Rk\StorePickup\Model\Carrier\Storepickup</model> <active>1</active> <title>Store Pickup</title> <name>Store Pickup</name> <shipping_cost >0.00</shipping_cost > <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg> <sallowspecific>0</sallowspecific> </storepickup> </carriers> </default> </config>
Step 4: Create a Shipping Model
Shipping model is define in config.xml inn <model/> node. It is used to apply our logic for the shipping method like Shipping Price and Shipping method available or not.
Create Storepickup.php file at app/code/Rk/StorePickup/Model/Carrier/Storepickup.php
<?php namespace Rk\StorePickup\Model\Carrier; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Shipping\Model\Rate\Result; class Storepickup extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements \Magento\Shipping\Model\Carrier\CarrierInterface { protected $_code = 'storepickup'; protected $_isFixed = true; protected $_rateResultFactory; protected $_rateMethodFactory; /** * Constructor * * @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 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, array $data = [] ) { $this->_rateResultFactory = $rateResultFactory; $this->_rateMethodFactory = $rateMethodFactory; parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); } /** * {@inheritdoc} */ 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')); $shippingCost = (float)$this->getConfigData('shipping_cost'); $method->setPrice($shippingCost); $method->setCost($shippingCost); $result->append($method); return $result; } /** * getAllowedMethods * * @return array */ public function getAllowedMethods() { return [$this->_code => $this->getConfigData('name')]; } }
Run below command to install new shipping method.
php bin/magento setup:upgrade