Magento – How to programmaticlly create custom shipping method for third party API

magento2shipping

How to create custom shipping like DHL, UPS, … in magento 2 so I will be able to connect to third party API and get all the data to create package, tracking numbers and labels? Is there any skeleton Carrier class how to do such thing?

Best Answer

In the Magento, shipping carriers divided into two types: online and offline shipping.

They should implement \Magento\Shipping\Model\Carrier\CarrierInterface:

  • Check if carrier has shipping tracking option available - isTrackingAvailable()
  • Get allowed shipping methods - getAllowedMethods()

1) For the offline shipping, your custom shipping simply extends from Abstract carrier model- \Magento\Shipping\Model\Carrier\AbstractCarrier.

class CustomOfflineShipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
    \Magento\Shipping\Model\Carrier\CarrierInterface
{

A good sample here: https://github.com/ajzele/B05032-Foggyline_Shipbox

2) For the online shipping, your module should extends from the abstract online shipping carrier model - Magento\Shipping\Model\Carrier\AbstractCarrierOnline.

We should take a look the USPS shipping or DHL International:

-vendor/magento/module-usps/Model/Carrier.php
-vendor/magento/module-dhl/Model/Carrier.php

/**
 * USPS shipping
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Carrier extends AbstractCarrierOnline
    implements \Magento\Shipping\Model\Carrier\CarrierInterface
{
Related Topic