How to create a shipping method in Magento 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1












I'm using the Magento website 2.2.5, I want to create new custom shipping method for my store, how can I create custom shipping method or any guide about this?







share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    I'm using the Magento website 2.2.5, I want to create new custom shipping method for my store, how can I create custom shipping method or any guide about this?







    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I'm using the Magento website 2.2.5, I want to create new custom shipping method for my store, how can I create custom shipping method or any guide about this?







      share|improve this question














      I'm using the Magento website 2.2.5, I want to create new custom shipping method for my store, how can I create custom shipping method or any guide about this?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 4 at 6:58









      Dinesh Yadav

      3,1871730




      3,1871730










      asked Sep 4 at 6:48







      user71625



























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can check the document for Creating a shipping method in Magento 2 on the link : https://inchoo.net/magento-2/creating-a-shipping-method-in-magento-2/






          share|improve this answer




















          • Thanks for your answer
            – user71625
            Sep 4 at 6:49










          • No problem, let me know if you have any question
            – Vu Tran Kien
            Sep 4 at 6:50

















          up vote
          1
          down vote













          Create following files to add a custom shipping method.



          I have taken a shipping method code as "testshipping".




          app/code/Vendor/Module/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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">

          <group id="testshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Test Shipping</label>
          <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Enabled</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Method Name</label>
          </field>
          <field id="price" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Price</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="handling_type" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Calculate Handling Fee</label>
          <source_model>MagentoShippingModelSourceHandlingType</source_model>
          </field>
          <field id="handling_fee" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Handling Fee</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="sort_order" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Sort Order</label>
          </field>
          <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Title</label>
          </field>
          <field id="sallowspecific" translate="label" type="select" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Applicable Countries</label>
          <frontend_class>shipping-applicable-country</frontend_class>
          <source_model>MagentoShippingModelConfigSourceAllspecificcountries</source_model>
          </field>
          <field id="specificcountry" translate="label" type="multiselect" sortOrder="910" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Specific Countries</label>
          <source_model>MagentoDirectoryModelConfigSourceCountry</source_model>
          <can_be_empty>1</can_be_empty>
          </field>
          <field id="showmethod" translate="label" type="select" sortOrder="920" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Show Method if Not Applicable</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="specificerrmsg" translate="label" type="textarea" sortOrder="800" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Displayed Error Message</label>
          </field>
          </group>

          </section>

          </system>
          </config>



          app/code/Vendor/Module/etc/config.xml




          <?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>
          <testshipping>
          <active>1</active>
          <sallowspecific>0</sallowspecific>
          <model>VendorModuleModelCarrierTestshipping</model>
          <name>Testshipping</name>
          <price>00.00</price>
          <title>Test Shipping</title>
          <type>I</type>
          <sort_order>0</sort_order>
          <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
          </testshipping>
          </carriers>
          </default>
          </config>



          app/code/Vendor/Module/Model/Carrier/Testshipping.php




          <?php

          namespace VendorModuleModelCarrier;

          use MagentoQuoteModelQuoteAddressRateRequest;
          use MagentoShippingModelRateResult;

          class Testshipping extends MagentoShippingModelCarrierAbstractCarrier implements
          MagentoShippingModelCarrierCarrierInterface
          Result
          */
          public function collectRates(RateRequest $request)

          if (!$this->getConfigFlag('active'))
          return false;


          /** @var MagentoShippingModelRateResult $result */
          $result = $this->_rateResultFactory->create();

          /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
          $method = $this->_rateMethodFactory->create();

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

          $method->setMethod('testshipping');
          $method->setMethodTitle($this->getConfigData('name'));

          /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
          $amount = $this->getConfigData('price');

          $method->setPrice($amount);
          $method->setCost($amount);

          $result->append($method);

          return $result;







          share|improve this answer




















          • Many thanks, i will try
            – user71625
            Sep 4 at 7:17










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f240690%2fhow-to-create-a-shipping-method-in-magento-2%23new-answer', 'question_page');

          );

          Post as a guest





























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You can check the document for Creating a shipping method in Magento 2 on the link : https://inchoo.net/magento-2/creating-a-shipping-method-in-magento-2/






          share|improve this answer




















          • Thanks for your answer
            – user71625
            Sep 4 at 6:49










          • No problem, let me know if you have any question
            – Vu Tran Kien
            Sep 4 at 6:50














          up vote
          1
          down vote



          accepted










          You can check the document for Creating a shipping method in Magento 2 on the link : https://inchoo.net/magento-2/creating-a-shipping-method-in-magento-2/






          share|improve this answer




















          • Thanks for your answer
            – user71625
            Sep 4 at 6:49










          • No problem, let me know if you have any question
            – Vu Tran Kien
            Sep 4 at 6:50












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can check the document for Creating a shipping method in Magento 2 on the link : https://inchoo.net/magento-2/creating-a-shipping-method-in-magento-2/






          share|improve this answer












          You can check the document for Creating a shipping method in Magento 2 on the link : https://inchoo.net/magento-2/creating-a-shipping-method-in-magento-2/







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 4 at 6:49









          Vu Tran Kien

          1




          1











          • Thanks for your answer
            – user71625
            Sep 4 at 6:49










          • No problem, let me know if you have any question
            – Vu Tran Kien
            Sep 4 at 6:50
















          • Thanks for your answer
            – user71625
            Sep 4 at 6:49










          • No problem, let me know if you have any question
            – Vu Tran Kien
            Sep 4 at 6:50















          Thanks for your answer
          – user71625
          Sep 4 at 6:49




          Thanks for your answer
          – user71625
          Sep 4 at 6:49












          No problem, let me know if you have any question
          – Vu Tran Kien
          Sep 4 at 6:50




          No problem, let me know if you have any question
          – Vu Tran Kien
          Sep 4 at 6:50












          up vote
          1
          down vote













          Create following files to add a custom shipping method.



          I have taken a shipping method code as "testshipping".




          app/code/Vendor/Module/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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">

          <group id="testshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Test Shipping</label>
          <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Enabled</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Method Name</label>
          </field>
          <field id="price" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Price</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="handling_type" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Calculate Handling Fee</label>
          <source_model>MagentoShippingModelSourceHandlingType</source_model>
          </field>
          <field id="handling_fee" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Handling Fee</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="sort_order" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Sort Order</label>
          </field>
          <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Title</label>
          </field>
          <field id="sallowspecific" translate="label" type="select" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Applicable Countries</label>
          <frontend_class>shipping-applicable-country</frontend_class>
          <source_model>MagentoShippingModelConfigSourceAllspecificcountries</source_model>
          </field>
          <field id="specificcountry" translate="label" type="multiselect" sortOrder="910" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Specific Countries</label>
          <source_model>MagentoDirectoryModelConfigSourceCountry</source_model>
          <can_be_empty>1</can_be_empty>
          </field>
          <field id="showmethod" translate="label" type="select" sortOrder="920" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Show Method if Not Applicable</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="specificerrmsg" translate="label" type="textarea" sortOrder="800" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Displayed Error Message</label>
          </field>
          </group>

          </section>

          </system>
          </config>



          app/code/Vendor/Module/etc/config.xml




          <?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>
          <testshipping>
          <active>1</active>
          <sallowspecific>0</sallowspecific>
          <model>VendorModuleModelCarrierTestshipping</model>
          <name>Testshipping</name>
          <price>00.00</price>
          <title>Test Shipping</title>
          <type>I</type>
          <sort_order>0</sort_order>
          <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
          </testshipping>
          </carriers>
          </default>
          </config>



          app/code/Vendor/Module/Model/Carrier/Testshipping.php




          <?php

          namespace VendorModuleModelCarrier;

          use MagentoQuoteModelQuoteAddressRateRequest;
          use MagentoShippingModelRateResult;

          class Testshipping extends MagentoShippingModelCarrierAbstractCarrier implements
          MagentoShippingModelCarrierCarrierInterface
          Result
          */
          public function collectRates(RateRequest $request)

          if (!$this->getConfigFlag('active'))
          return false;


          /** @var MagentoShippingModelRateResult $result */
          $result = $this->_rateResultFactory->create();

          /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
          $method = $this->_rateMethodFactory->create();

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

          $method->setMethod('testshipping');
          $method->setMethodTitle($this->getConfigData('name'));

          /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
          $amount = $this->getConfigData('price');

          $method->setPrice($amount);
          $method->setCost($amount);

          $result->append($method);

          return $result;







          share|improve this answer




















          • Many thanks, i will try
            – user71625
            Sep 4 at 7:17














          up vote
          1
          down vote













          Create following files to add a custom shipping method.



          I have taken a shipping method code as "testshipping".




          app/code/Vendor/Module/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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">

          <group id="testshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Test Shipping</label>
          <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Enabled</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Method Name</label>
          </field>
          <field id="price" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Price</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="handling_type" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Calculate Handling Fee</label>
          <source_model>MagentoShippingModelSourceHandlingType</source_model>
          </field>
          <field id="handling_fee" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Handling Fee</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="sort_order" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Sort Order</label>
          </field>
          <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Title</label>
          </field>
          <field id="sallowspecific" translate="label" type="select" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Applicable Countries</label>
          <frontend_class>shipping-applicable-country</frontend_class>
          <source_model>MagentoShippingModelConfigSourceAllspecificcountries</source_model>
          </field>
          <field id="specificcountry" translate="label" type="multiselect" sortOrder="910" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Specific Countries</label>
          <source_model>MagentoDirectoryModelConfigSourceCountry</source_model>
          <can_be_empty>1</can_be_empty>
          </field>
          <field id="showmethod" translate="label" type="select" sortOrder="920" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Show Method if Not Applicable</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="specificerrmsg" translate="label" type="textarea" sortOrder="800" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Displayed Error Message</label>
          </field>
          </group>

          </section>

          </system>
          </config>



          app/code/Vendor/Module/etc/config.xml




          <?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>
          <testshipping>
          <active>1</active>
          <sallowspecific>0</sallowspecific>
          <model>VendorModuleModelCarrierTestshipping</model>
          <name>Testshipping</name>
          <price>00.00</price>
          <title>Test Shipping</title>
          <type>I</type>
          <sort_order>0</sort_order>
          <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
          </testshipping>
          </carriers>
          </default>
          </config>



          app/code/Vendor/Module/Model/Carrier/Testshipping.php




          <?php

          namespace VendorModuleModelCarrier;

          use MagentoQuoteModelQuoteAddressRateRequest;
          use MagentoShippingModelRateResult;

          class Testshipping extends MagentoShippingModelCarrierAbstractCarrier implements
          MagentoShippingModelCarrierCarrierInterface
          Result
          */
          public function collectRates(RateRequest $request)

          if (!$this->getConfigFlag('active'))
          return false;


          /** @var MagentoShippingModelRateResult $result */
          $result = $this->_rateResultFactory->create();

          /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
          $method = $this->_rateMethodFactory->create();

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

          $method->setMethod('testshipping');
          $method->setMethodTitle($this->getConfigData('name'));

          /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
          $amount = $this->getConfigData('price');

          $method->setPrice($amount);
          $method->setCost($amount);

          $result->append($method);

          return $result;







          share|improve this answer




















          • Many thanks, i will try
            – user71625
            Sep 4 at 7:17












          up vote
          1
          down vote










          up vote
          1
          down vote









          Create following files to add a custom shipping method.



          I have taken a shipping method code as "testshipping".




          app/code/Vendor/Module/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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">

          <group id="testshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Test Shipping</label>
          <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Enabled</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Method Name</label>
          </field>
          <field id="price" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Price</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="handling_type" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Calculate Handling Fee</label>
          <source_model>MagentoShippingModelSourceHandlingType</source_model>
          </field>
          <field id="handling_fee" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Handling Fee</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="sort_order" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Sort Order</label>
          </field>
          <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Title</label>
          </field>
          <field id="sallowspecific" translate="label" type="select" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Applicable Countries</label>
          <frontend_class>shipping-applicable-country</frontend_class>
          <source_model>MagentoShippingModelConfigSourceAllspecificcountries</source_model>
          </field>
          <field id="specificcountry" translate="label" type="multiselect" sortOrder="910" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Specific Countries</label>
          <source_model>MagentoDirectoryModelConfigSourceCountry</source_model>
          <can_be_empty>1</can_be_empty>
          </field>
          <field id="showmethod" translate="label" type="select" sortOrder="920" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Show Method if Not Applicable</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="specificerrmsg" translate="label" type="textarea" sortOrder="800" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Displayed Error Message</label>
          </field>
          </group>

          </section>

          </system>
          </config>



          app/code/Vendor/Module/etc/config.xml




          <?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>
          <testshipping>
          <active>1</active>
          <sallowspecific>0</sallowspecific>
          <model>VendorModuleModelCarrierTestshipping</model>
          <name>Testshipping</name>
          <price>00.00</price>
          <title>Test Shipping</title>
          <type>I</type>
          <sort_order>0</sort_order>
          <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
          </testshipping>
          </carriers>
          </default>
          </config>



          app/code/Vendor/Module/Model/Carrier/Testshipping.php




          <?php

          namespace VendorModuleModelCarrier;

          use MagentoQuoteModelQuoteAddressRateRequest;
          use MagentoShippingModelRateResult;

          class Testshipping extends MagentoShippingModelCarrierAbstractCarrier implements
          MagentoShippingModelCarrierCarrierInterface
          Result
          */
          public function collectRates(RateRequest $request)

          if (!$this->getConfigFlag('active'))
          return false;


          /** @var MagentoShippingModelRateResult $result */
          $result = $this->_rateResultFactory->create();

          /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
          $method = $this->_rateMethodFactory->create();

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

          $method->setMethod('testshipping');
          $method->setMethodTitle($this->getConfigData('name'));

          /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
          $amount = $this->getConfigData('price');

          $method->setPrice($amount);
          $method->setCost($amount);

          $result->append($method);

          return $result;







          share|improve this answer












          Create following files to add a custom shipping method.



          I have taken a shipping method code as "testshipping".




          app/code/Vendor/Module/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" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">

          <group id="testshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Test Shipping</label>
          <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Enabled</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Method Name</label>
          </field>
          <field id="price" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Price</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="handling_type" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Calculate Handling Fee</label>
          <source_model>MagentoShippingModelSourceHandlingType</source_model>
          </field>
          <field id="handling_fee" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Handling Fee</label>
          <validate>validate-number validate-zero-or-greater</validate>
          </field>
          <field id="sort_order" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Sort Order</label>
          </field>
          <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Title</label>
          </field>
          <field id="sallowspecific" translate="label" type="select" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Applicable Countries</label>
          <frontend_class>shipping-applicable-country</frontend_class>
          <source_model>MagentoShippingModelConfigSourceAllspecificcountries</source_model>
          </field>
          <field id="specificcountry" translate="label" type="multiselect" sortOrder="910" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Ship to Specific Countries</label>
          <source_model>MagentoDirectoryModelConfigSourceCountry</source_model>
          <can_be_empty>1</can_be_empty>
          </field>
          <field id="showmethod" translate="label" type="select" sortOrder="920" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Show Method if Not Applicable</label>
          <source_model>MagentoConfigModelConfigSourceYesno</source_model>
          </field>
          <field id="specificerrmsg" translate="label" type="textarea" sortOrder="800" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Displayed Error Message</label>
          </field>
          </group>

          </section>

          </system>
          </config>



          app/code/Vendor/Module/etc/config.xml




          <?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>
          <testshipping>
          <active>1</active>
          <sallowspecific>0</sallowspecific>
          <model>VendorModuleModelCarrierTestshipping</model>
          <name>Testshipping</name>
          <price>00.00</price>
          <title>Test Shipping</title>
          <type>I</type>
          <sort_order>0</sort_order>
          <specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
          </testshipping>
          </carriers>
          </default>
          </config>



          app/code/Vendor/Module/Model/Carrier/Testshipping.php




          <?php

          namespace VendorModuleModelCarrier;

          use MagentoQuoteModelQuoteAddressRateRequest;
          use MagentoShippingModelRateResult;

          class Testshipping extends MagentoShippingModelCarrierAbstractCarrier implements
          MagentoShippingModelCarrierCarrierInterface
          Result
          */
          public function collectRates(RateRequest $request)

          if (!$this->getConfigFlag('active'))
          return false;


          /** @var MagentoShippingModelRateResult $result */
          $result = $this->_rateResultFactory->create();

          /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
          $method = $this->_rateMethodFactory->create();

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

          $method->setMethod('testshipping');
          $method->setMethodTitle($this->getConfigData('name'));

          /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
          $amount = $this->getConfigData('price');

          $method->setPrice($amount);
          $method->setCost($amount);

          $result->append($method);

          return $result;








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 4 at 7:16









          Dinesh Yadav

          3,1871730




          3,1871730











          • Many thanks, i will try
            – user71625
            Sep 4 at 7:17
















          • Many thanks, i will try
            – user71625
            Sep 4 at 7:17















          Many thanks, i will try
          – user71625
          Sep 4 at 7:17




          Many thanks, i will try
          – user71625
          Sep 4 at 7:17

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f240690%2fhow-to-create-a-shipping-method-in-magento-2%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery