How to Create Custom Shipping Module and Method in Magento 2

Transporting an item from one place to another is called Shipping, and to deliver an ordered product through a medium to your customer is called the Shipping Method.

Magento 2 already has built-in Shipping methods, but if you need custom Shipping Methods, then you have to create a custom Shipping Method. In this guide I am going to show you how to create a custom shipping module in Magento 2.

If you are not familiar with custom module, then first learn about it from this guide: Create Module in Magento 2.

Steps to Follow:

  • Registration and Configuration of Module
  • Create New Fields in Admin Panel
  • Define Shipping Model
  • Create Shipping Model
  • Run Commands

Registration and Configuration of Shipping Module

First of all, configuration and registration of a custom module is required. Let’s first configure and then register the module.

Subscribe to Get FREE Magento 2 eBook in Your Inbox!

Create** module.xml** in** app/code/Magenticians/Moduleshipping/etc** and add the following code in it to configure the module:

|

1

2

3

4

5

|

|

Create** registration.php** in app/code/Magenticians/Moduleshipping and add the following code in it to register the module:

|

1

2

3

4

5

6

|

\Magento\Framework\Component\ComponentRegistrar::register(

\Magento\Framework\Component\ComponentRegistrar::MODULE,

'Magenticians_Moduleshipping',

DIR

);

|

Create New Fields in Admin Panel

Create** system.xml** in app/code/Magenticians/Moduleshipping/etc/adminhtml and add this code to it:

|

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

|

Magento\Config\Model\Config\Source\Yesno

validate-number validate-zero-or-greater

shipping-applicable-country

Magento\Shipping\Model\Config\Source\Allspecificcountries

Magento\Directory\Model\Config\Source\Country

1

Magento\Config\Model\Config\Source\Yesno

|

Define Shipping Model

Now define the shipping model by creating **config.xml **in app/code/Magenticians/Moduleshipping/etc, add this code to it:

|

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

|

0

0

0

Magenticians\Moduleshipping\Model\Carrier\Customshipping

Custom Shipping

Magenticians Custom Shipping

This shipping method is not available. To use this shipping method, please contact us.

|

Create Shipping Model Class

In the previous step I defined the Shipping Model class and now I am going to create it by creating Customshipping.php in app/code/Magenticians/Moduleshipping/Model/Carrier. Let’s add this code to it:

|

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

|

namespace Magenticians\Moduleshipping\Model\Carrier;

use Magento\Framework\App\Config\ScopeConfigInterface;

use Magento\Framework\DataObject;

use Magento\Shipping\Model\Carrier\AbstractCarrier;

use Magento\Shipping\Model\Carrier\CarrierInterface;

use Magento\Shipping\Model\Config;

use Magento\Shipping\Model\Rate\ResultFactory;

use Magento\Store\Model\ScopeInterface;

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\Quote\Model\Quote\Address\RateRequest;

use Psr\Log\LoggerInterface;

class Customshipping extends AbstractCarrier implements CarrierInterface

{

/**

  • Carrier's code

  • @var string

*/

protected $_code = 'magenticians_moduleshipping';

/**

  • 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;

}

}

|

Run Commands

Great, you’re all done! Launch your SSH terminal and connect it to your store, and run the following commands in the root directory of your store:

|

1

2

3

4

5

6

|

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

|

Now let’s check out the result!

First, go to STORES → Configuration from the admin panel of your store. Then click onShipping Methods under the** SALES** tab.

How to Create Custom Shipping Module and Method in Magento 2_第1张图片
magento shipping method

Here, you will see the new shipping Method section: Magenticians Custom Shipping. Enable the Custom Shipping method and set its values according to your need.

How to Create Custom Shipping Module and Method in Magento 2_第2张图片
magento custom shipping method

Now go to the checkout page of your Magento 2 store and you will see the Magenticians Custom Shipping Method.

How to Create Custom Shipping Module and Method in Magento 2_第3张图片
Select Custom shipping method
How to Create Custom Shipping Module and Method in Magento 2_第4张图片
Review and Payments- Custom shipping method

Wrapping Up

After following this guide, you should be able to create a custom shipping method module in Magento 2. Hopefully, all the steps mentioned above were easy to follow and you didn’t get stuck anywhere. However, if you still have any issues or confusions, just leave your thoughts below!

你可能感兴趣的:(How to Create Custom Shipping Module and Method in Magento 2)