Magento 2 Shipping Methods: A Step-by-Step Guide to Custom Shipping Options
Shipping is a vital aspect of any eCommerce store, and in Magento 2, you can easily customize shipping methods to suit your business needs. This blog post will guide you through the steps required to create custom shipping methods in Magento 2, making it possible for your store to offer unique shipping options tailored to your products and customers.
In this tutorial, we’ll cover everything from module setup to configuring your custom shipping method.
Why Create Custom Shipping Methods in Magento 2?
Default Magento 2 shipping methods (like Flat Rate, Free Shipping, or Table Rates) work well for standard use cases. However, in some instances, you may need more flexibility or specific functionality. Custom shipping methods allow you to:
- Offer unique shipping rates based on product type, location, or customer groups.
- Create advanced shipping logic based on store-specific requirements.
- Add dynamic shipping rates or integrate with third-party logistics.
Step 1: Module Setup
Before creating a custom shipping method, you need a basic module setup in Magento 2. If you’re already familiar with module creation, you can skip this step, but for those who need a refresher, here’s a quick outline:
- Create module directory structure:
app/code/Vendor/CustomShipping ├── etc ├── Model ├── Setup └── view
- Define module.xml:
<!-- app/code/Vendor/CustomShipping/etc/module.xml --> <?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="Vendor_CustomShipping" setup_version="1.0.0"/> </config>
- Register the module in
registration.php
:<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_CustomShipping', __DIR__ );
Step 2: Configure system.xml
for Backend Settings
To make your shipping method configurable from the admin panel, you need to define settings using the system.xml
file.
<!-- app/code/Vendor/CustomShipping/etc/adminhtml/system.xml -->
<?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">
<group id="customshipping" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Shipping</label>
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</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="1">
<label>Shipping Price</label>
</field>
</group>
</section>
</system>
</config>
Step 3: Define Shipping Method in config.xml
Next, configure your shipping method in config.xml
, which helps Magento recognize your method and applies it to the checkout process.
<!-- app/code/Vendor/CustomShipping/etc/config.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
<default>
<carriers>
<customshipping>
<active>1</active>
<title>Custom Shipping Method</title>
<name>Custom Shipping</name>
<price>10</price>
<sort_order>100</sort_order>
</customshipping>
</carriers>
</default>
</config>
Step 4: Create the Custom Shipping Model
The heart of any custom shipping method is the model that calculates shipping rates. You’ll need to extend the Magento CarrierInterface
to create a functional shipping method.
<?php
namespace Vendor\CustomShipping\Model\Carrier;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
class CustomShipping extends AbstractCarrier implements CarrierInterface
{
protected $_code = 'customshipping';
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
$result = $this->_rateResultFactory->create();
$method = $this->_rateMethodFactory->create();
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
$method->setPrice($this->getConfigData('price'));
$method->setCost($this->getConfigData('price'));
$result->append($method);
return $result;
}
public function getAllowedMethods()
{
return [$this->_code => $this->getConfigData('name')];
}
}
Step 5: Test and Verify
Once you’ve completed the code, clear the cache and reindex Magento:
php bin/magento setup:upgrade
php bin/magento cache:flush
Next, go to Stores > Configuration > Sales > Shipping Methods and check if your custom shipping method appears. You can enable it and configure the price and title.
Conclusion
Creating a custom shipping method in Magento 2 opens up a range of possibilities to offer unique shipping options tailored to your store’s requirements. Whether you want to create dynamic shipping prices, integrate with logistics APIs, or provide specialized shipping methods based on your product types, Magento’s flexibility allows you to do it all.
Feel free to reach out if you have any issues or need further assistance while creating custom shipping methods in Magento 2!
Looking for more Magento 2 tips? Check out our other Magento Development Guide.
Liked the post, share with others: