How to create custom delivery method in Magento 2 ?

Creating a custom delivery method in Magento 2.4 involves creating a new module and implementing the necessary components for your custom shipping method. Here's a step-by-step guide:

Create a new module:

Create a new folder structure for your module:

bash

Copy code

app/code/Vendor/CustomShippingMethod

Replace Vendor with your preferred vendor name, and CustomShippingMethod with your desired module name.

Register for the module:

Create a registration.php file inside the CustomShippingMethod folder:

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Vendor_CustomShippingMethod',
    __DIR__
);

Declare the module:

Create a module.xml file in the app/code/Vendor/CustomShippingMethod/etc directory:

<?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_CustomShippingMethod" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Shipping"/>
        </sequence>
    </module>
</config>


Configure the module:

Create a system.xml file in the app/code/Vendor/CustomShippingMethod/etc/adminhtml directory:

<?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="customshippingmethod" translate="label" type="text"
sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Custom 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="2"
showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Method Name</label>
                </field>
                <field id="price" translate="label" type="text" sortOrder="3"
showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                    <label>Price</label>
                    <validate>validate-number</validate>
                </field>
            </group>
        </section>
    </system>
</config>

Create a model for your custom shipping method:

Create a CustomShipping.php file in the app/code/Vendor/CustomShippingMethod/Model/Carrier directory:



namespace Vendor\CustomShippingMethod\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 = 'customshippingmethod';

    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'));
        $amount = $this->getConfigData('price');
        $method->setPrice($amount);
        $method->setCost($amount);
        $result->append($method);

        return $result;

    }



    public function getAllowedMethods()
  {
        return [$this->_code => $this->getConfigData('name')];
    }

}


Declare the model in di.xml:

Create a di.xml file in the app/code/Vendor/CustomShippingMethod/etc directory:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Shipping\Model\Config">
        <arguments>
            <argument name="carrierInstances" xsi:type="array">
                <item name="customshippingmethod"
                xsi:type="object">Vendor\CustomShippingMethod\Model\Carrier\CustomShipping</item>
            </argument>
        </arguments>
    </type>

    <type name="Vendor\CustomShippingMethod\Model\Carrier\CustomShipping">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="config_path"
                xsi:type="string">carriers/customshippingmethod</item>
            </argument>
        </arguments>
    </type>
</config>

Enable the module and clear caches:

Run the following commands in your Magento root directory:

Replace Vendor and CustomShippingMethod with the appropriate names you chose earlier.

Now, your custom shipping method should be available in the Magento 2.4

admin panel under Stores > Configuration > Sales > Shipping Methods.

Enable the method and configure it according to your requirements.

php bin/magento module:enable Vendor_CustomShippingMethod
php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush

Ready to elevate your e-commerce business?

Discuss your business objectives with us. Get in touch today to explore ways we can assist in reaching them.

Copyright © 2024 ArmMage LLC. All rights reserved.