How to Create Custom Shipping Mo

2018-05-07  本文已影响0人  跨境小白

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 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!

<input id="boone-field-comments" name="boone-comments" type="text" data-selectable="true" data-target="#builder-setting-comments_value" class="boone-field-comments" placeholder="" value="" autocomplete="off" style="margin: 0px 0px 10px; padding: 4px 6px; border: 1px solid rgb(221, 221, 221); vertical-align: middle; font-size: 16px; font-family: inherit; border-radius: 0px; background: rgb(255, 255, 255); outline: none; color: rgb(34, 34, 34); max-width: 1px !important; box-sizing: border-box; visibility: hidden; position: absolute !important; overflow: hidden; clip: rect(0px 0px 0px 0px) !important; height: 34px; width: 1px; z-index: -1 !important; min-width: 1px !important; line-height: 24px; font-style: normal; -webkit-appearance: none; display: inline;"><input id="boone-field-email" name="boone-email" type="email" data-selectable="true" data-target="#builder-setting-email_value" class="boone-field-email" placeholder="Enter your email address here..." value="" style="margin: 0px 0px 10px; padding: 4px 6px; border: 1px solid rgb(221, 221, 221); vertical-align: middle; font-size: 16px; font-family: Helvetica; border-radius: 0px; background: rgb(255, 255, 255); outline: none; color: rgb(72, 72, 72); max-width: 100%; box-sizing: border-box; width: 661.99px; line-height: 24px; overflow: hidden; height: 34px; font-style: normal; -webkit-appearance: none; display: inline;"><input id="boone-field-submit" name="boone-submit" type="submit" data-selectable="true" data-target="#builder-setting-submit_value" class="boone-field-submit" value="Get eBook" style="margin: 0px; padding: 4px 6px; border: 1px solid rgb(8, 127, 229); vertical-align: middle; font-size: 16px; font-family: Arial; border-radius: 0px; background: rgb(8, 127, 229); outline: none; color: rgb(255, 255, 255); max-width: 100%; box-sizing: border-box; cursor: pointer; -webkit-appearance: none; width: 661.99px; line-height: 24px; overflow: hidden; height: 36px; font-style: normal; display: inline; text-align: center;">

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

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="margin: 0px; padding: 0px 5px; border: 0px; vertical-align: baseline; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; overflow: hidden; background: rgb(255, 255, 255); outline: 0px; color: rgb(0, 0, 0); max-width: 100%; width: 699.99px; box-sizing: border-box; height: 79px; position: absolute; opacity: 1; border-radius: 0px; box-shadow: none; white-space: pre; word-wrap: normal; resize: none; tab-size: 4; line-height: 15px !important; z-index: 1;"></textarea>

|

1

2

3

4

5

|

<?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="Magenticians_Moduleshipping" setup_version="1.0.1">

</module>

</config>

|

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

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="margin: 0px; padding: 0px 5px; border: 0px; vertical-align: baseline; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; overflow: hidden; background: rgb(255, 255, 255); outline: 0px; color: rgb(0, 0, 0); max-width: 100%; width: 699.99px; box-sizing: border-box; height: 88px; position: absolute; opacity: 1; border-radius: 0px; box-shadow: none; white-space: pre; word-wrap: normal; resize: none; tab-size: 4; line-height: 15px !important; z-index: 1;"></textarea>

|

1

2

3

4

5

6

|

<?php

\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:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="margin: 0px; padding: 0px 5px; border: 0px; vertical-align: baseline; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; overflow: hidden; background: rgb(255, 255, 255); outline: 0px; color: rgb(0, 0, 0); max-width: 100%; width: 699.99px; box-sizing: border-box; height: 651px; position: absolute; opacity: 1; border-radius: 0px; box-shadow: none; white-space: pre; word-wrap: normal; resize: none; tab-size: 4; line-height: 15px !important; z-index: 1;"></textarea>

|

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

|

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Config/etc/system_file.xsd">

<system>

<section id="carriers" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">

<group id="magenticians_moduleshipping" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">

<label>Magenticians Custom Shipping</label>

<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">

<label>Enabled</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="0">

<label>Shipping Cost</label>

<validate>validate-number validate-zero-or-greater</validate>

</field>

<field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">

<label>Displayed Error Message</label>

</field>

<field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0">

<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" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0">

<label>Ship to Specific Countries</label>

<source_model>Magento\Directory\Model\Config\Source\Country</source_model>

<can_be_empty>1</can_be_empty>

</field>

<field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0">

<label>Show Method if Not Applicable</label>

<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>

</field>

<field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">

<label>Sort Order</label>

</field>

</group>

</section>

</system>

</config>

|

Define Shipping Model

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

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="margin: 0px; padding: 0px 5px; border: 0px; vertical-align: baseline; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; overflow: hidden; background: rgb(255, 255, 255); outline: 0px; color: rgb(0, 0, 0); max-width: 100%; width: 699.99px; box-sizing: border-box; height: 241px; position: absolute; opacity: 1; border-radius: 0px; box-shadow: none; white-space: pre; word-wrap: normal; resize: none; tab-size: 4; line-height: 15px !important; z-index: 1;"></textarea>

|

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

|

<?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>

<magenticians_moduleshipping>

<active>0</active>

<sallowspecific>0</sallowspecific>

<price>0</price>

<model>Magenticians\Moduleshipping\Model\Carrier\Customshipping</model>

<name>Custom Shipping</name>

<title>Magenticians Custom Shipping</title>

<specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>

</magenticians_moduleshipping>

</carriers>

</default>

</config>

|

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:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="margin: 0px; padding: 0px 5px; border: 0px; vertical-align: baseline; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; overflow: hidden; background: rgb(255, 255, 255); outline: 0px; color: rgb(0, 0, 0); max-width: 100%; width: 699.99px; box-sizing: border-box; height: 1745px; position: absolute; opacity: 1; border-radius: 0px; box-shadow: none; white-space: pre; word-wrap: normal; resize: none; tab-size: 4; line-height: 15px !important; z-index: 1;"></textarea>

|

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

|

<?php

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

{

/**

*/

protected $_code = 'magenticians_moduleshipping';

/**

*/

protected $_isFixed = true;

/**

*/

protected $_rateResultFactory;

/**

*/

protected $_rateMethodFactory;

/**

*/

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

}

/**

*/

public function getAllowedMethods()

{

return [$this->getCarrierCode() => __($this->getConfigData('name'))];

}

/**

*/

public function collectRates(RateRequest $request)

{

/**

*/

if (!$this->isActive()) {

return false;

}

/** @var \Magento\Shipping\Model\Rate\Result $result */

$result = $this->_rateResultFactory->create();

$shippingPrice = $this->getConfigData('price');

$method = $this->_rateMethodFactory->create();

/**

*/

$method->setCarrier($this->getCarrierCode());

$method->setCarrierTitle($this->getConfigData('title'));

/**

*/

$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:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="margin: 0px; padding: 0px 5px; border: 0px; vertical-align: baseline; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; overflow: hidden; background: rgb(255, 255, 255); outline: 0px; color: rgb(0, 0, 0); max-width: 100%; width: 699.99px; box-sizing: border-box; height: 88px; position: absolute; opacity: 0; border-radius: 0px; box-shadow: none; white-space: pre; word-wrap: normal; resize: none; tab-size: 4; line-height: 15px !important; z-index: 0;"></textarea>

|

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.

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.

magento custom shipping method

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

Select Custom shipping method 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!

上一篇 下一篇

猜你喜欢

热点阅读