{"id":5941,"date":"2016-08-12T12:51:13","date_gmt":"2016-08-12T12:51:13","guid":{"rendered":"https:\/\/www.mconnectmedia.com\/blog\/?p=5941"},"modified":"2025-08-20T16:50:50","modified_gmt":"2025-08-20T11:20:50","slug":"implementation-shipping-methods-magento-2-0","status":"publish","type":"post","link":"https:\/\/www.mconnectmedia.com\/blog\/2016\/08\/12\/implementation-shipping-methods-magento-2-0\/","title":{"rendered":"Implementation of Shipping Methods in Magento\u00ae 2.0"},"content":{"rendered":"<p>The shipping methods can affect a customer\u2019s decision to buy a certain product. However, it becomes really important that the online stores have various shipping methods to add to their convenience.<\/p>\n<p>We have already seen the <span><a href=\"https:\/\/www.mconnectmedia.com\/blog\/magento-2-shipping-methods-starts-with-flat-rate-shipping\/\">four default shipping methods in Magento<sup>\u00ae<\/sup> 2.0<\/a><\/span>. Now let us show you how you can implement the custom module or new shipping method extensions in your\u00a0Magento<sup>\u00ae<\/sup> 2.0 store.<\/p>\n<h3>To add a new shipping method in Magento<sup>\u00ae<\/sup> 2 please follow below steps:<\/h3>\n<p>First, create folder hierarchy,<\/p>\n<p>app\/code\/&lt;YOUR_PACKAGE_NAME&gt;\/&lt;YOUR_MODULE_NAME&gt;<\/p>\n<p>e.g. app\/code\/Mconnect\/Customflatrate<\/p>\n<p>And then create registration.php in your module folder to register your module to Magento<sup>\u00ae<\/sup> 2.0<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>\/**<\/p>\n<p>* Copyright \u00a9 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.<\/p>\n<p>* See LICENSE.txt for license details (http:\/\/opensource.org\/licenses\/osl-3.0.php).<\/p>\n<p>*<\/p>\n<p>* Glory to Ukraine! Glory to the heroes!<\/p>\n<p>*\/<\/p>\n<p>\\Magento\\Framework\\Component\\ComponentRegistrar::register(<\/p>\n<p>\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,<\/p>\n<p>&#8216;Mconnect_Customflatrate&#8217;,<\/p>\n<p>__DIR__<\/p>\n<p>);<\/p><\/blockquote>\n<p>Create composer.json in same folder. It will provide information about your module to the composer.<\/p>\n<blockquote><p>{<\/p>\n<p>&#8220;name&#8221;: &#8220;mconnect\/module-customflatrate&#8221;,<\/p>\n<p>&#8220;description&#8221;: &#8220;N\/A&#8221;,<\/p>\n<p>&#8220;require&#8221;: {<\/p>\n<p>&#8220;php&#8221;: &#8220;~5.5.0|~5.6.0|~7.0.0&#8221;,<\/p>\n<p>&#8220;magento\/module-store&#8221;: &#8220;100.0.0&#8221;,<\/p>\n<p>&#8220;magento\/module-catalog&#8221;: &#8220;100.0.0&#8221;,<\/p>\n<p>&#8220;magento\/module-email&#8221;: &#8220;100.0.0&#8221;,<\/p>\n<p>&#8220;magento\/module-ui&#8221;: &#8220;100.0.0&#8221;,<\/p>\n<p>&#8220;magento\/module-variable&#8221;: &#8220;100.0.0&#8221;,<\/p>\n<p>&#8220;magento\/module-media-storage&#8221;: &#8220;100.0.0&#8221;,<\/p>\n<p>&#8220;magento\/framework&#8221;: &#8220;100.0.0&#8221;<\/p>\n<p>},<\/p>\n<p>&#8220;type&#8221;: &#8220;magento2-module&#8221;,<\/p>\n<p>&#8220;version&#8221;: &#8220;2.0.0&#8221;,<\/p>\n<p>&#8220;license&#8221;: [<\/p>\n<p>&#8220;OSL-3.0&#8221;,<\/p>\n<p>&#8220;AFL-3.0&#8221;<\/p>\n<p>],<\/p>\n<p>&#8220;autoload&#8221;: {<\/p>\n<p>&#8220;files&#8221;: [ &#8220;registration.php&#8221; ],<\/p>\n<p>&#8220;psr-4&#8221;: {<\/p>\n<p>&#8220;Mconnect\\\\Customflatrate\\\\&#8221;: &#8220;&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Create a folder named \u201cetc\u201d in your module root folder<\/p>\n<ul>\n<li>Create a file module.xml which will store your module version info<\/li>\n<\/ul>\n<blockquote><p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<\/p>\n<p>&lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:framework:Module\/etc\/module.xsd&#8221;&gt;<\/p>\n<p>&lt;module name=&#8221;Mconnect_Customflatrate&#8221; setup_version=&#8221;2.0.0&#8243;&gt;<\/p>\n<p>&lt;sequence&gt;<\/p>\n<p>&lt;module name=&#8221;Magento_Catalog&#8221;\/&gt;<\/p>\n<p>&lt;module name=&#8221;Magento_OfflineShipping&#8221;\/&gt;<\/p>\n<p>&lt;\/sequence&gt;<\/p>\n<p>&lt;\/module&gt;<\/p>\n<p>&lt;\/config&gt;<\/p><\/blockquote>\n<ul>\n<li>In \u201cetc\u201d folder create config.xml file as below<\/li>\n<\/ul>\n<blockquote><p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<\/p>\n<p>&lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:module:Magento_Store:etc\/config.xsd&#8221;&gt;<\/p>\n<p>&lt;default&gt;<\/p>\n<p>&lt;carriers&gt;<\/p>\n<p>&lt;mconnectcustomflatrate&gt;<\/p>\n<p>&lt;active&gt;1&lt;\/active&gt;<\/p>\n<p>&lt;model&gt;Mconnect\\Customflatrate\\Model\\Carrier\\Customflatrate&lt;\/model&gt;<\/p>\n<p>&lt;name&gt;Flat Rate&lt;\/name&gt;<\/p>\n<p>&lt;title&gt;Mconnect&lt;\/title&gt;<\/p>\n<p>&lt;shipping_default_value_enable&gt;0&lt;\/shipping_default_value_enable&gt;<\/p>\n<p>&lt;show_method&gt;0&lt;\/show_method&gt;<\/p>\n<p>&lt;allow_free_shipping&gt;0&lt;\/allow_free_shipping&gt;<\/p>\n<p>&lt;sallowspecific&gt;0&lt;\/sallowspecific&gt;<\/p>\n<p>&lt;specificerrmsg&gt;This shipping method is not available. To use this shipping method, please contact us.&lt;\/specificerrmsg&gt;<\/p>\n<p>&lt;sort_order&gt;10&lt;\/sort_order&gt;<\/p>\n<p>&lt;\/mconnectcustomflatrate&gt;<\/p>\n<p>&lt;\/carriers&gt;<\/p>\n<p>&lt;\/default&gt;<\/p>\n<p>&lt;\/config&gt;<\/p><\/blockquote>\n<ul>\n<li>For admin configuration please create folder \u201cadminhtml\u201d in \u201cetc\u201d folder and create system.xml file for admin configuration options<\/li>\n<\/ul>\n<p>It will contain below xml<\/p>\n<blockquote><p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<\/p>\n<p>&lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:module:Magento_Config:etc\/system_file.xsd&#8221;&gt;<\/p>\n<p>&lt;system&gt;<\/p>\n<p>&lt;section id=&#8221;carriers&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;320&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;1&#8243;&gt;<\/p>\n<p>&lt;group id=&#8221;mconnectcustomflatrate&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;2&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;1&#8243;&gt;<\/p>\n<p>&lt;label&gt;Mconnect Flat Rate&lt;\/label&gt;<\/p>\n<p>&lt;field id=&#8221;active&#8221; translate=&#8221;label&#8221; type=&#8221;select&#8221; sortOrder=&#8221;1&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;0&#8243;&gt;<\/p>\n<p>&lt;label&gt;Enabled&lt;\/label&gt;<\/p>\n<p>&lt;source_model&gt;Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;title&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;2&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;1&#8243;&gt;<\/p>\n<p>&lt;label&gt;Title&lt;\/label&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;name&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;3&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;1&#8243;&gt;<\/p>\n<p>&lt;label&gt;Method Name&lt;\/label&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;shipping_default_value_enable&#8221; translate=&#8221;label&#8221; type=&#8221;select&#8221; sortOrder=&#8221;6&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;0&#8243;&gt;<\/p>\n<p>&lt;label&gt;Shipping Rate&lt;\/label&gt;<\/p>\n<p>&lt;source_model&gt;Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model&gt;<\/p>\n<p>&lt;comment&gt;If set to &#8220;Yes&#8221;, the default rate will be used for products which do not have shipping rate assigned. If set to &#8220;No&#8221;, 0 will be used for all products which do not have shipping rate.&lt;\/comment&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;sallowspecific&#8221; translate=&#8221;label&#8221; type=&#8221;select&#8221; sortOrder=&#8221;90&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;0&#8243;&gt;<\/p>\n<p>&lt;label&gt;Ship to Applicable Countries&lt;\/label&gt;<\/p>\n<p>&lt;frontend_class&gt;shipping-applicable-country&lt;\/frontend_class&gt;<\/p>\n<p>&lt;source_model&gt;Magento\\Shipping\\Model\\Config\\Source\\Allspecificcountries&lt;\/source_model&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;specificcountry&#8221; translate=&#8221;label&#8221; type=&#8221;multiselect&#8221; sortOrder=&#8221;91&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;0&#8243;&gt;<\/p>\n<p>&lt;label&gt;Ship to Specific Countries&lt;\/label&gt;<\/p>\n<p>&lt;source_model&gt;Magento\\Directory\\Model\\Config\\Source\\Country&lt;\/source_model&gt;<\/p>\n<p>&lt;can_be_empty&gt;1&lt;\/can_be_empty&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;show_method&#8221; translate=&#8221;label&#8221; type=&#8221;select&#8221; sortOrder=&#8221;92&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;0&#8243;&gt;<\/p>\n<p>&lt;label&gt;Show Method if Not Applicable&lt;\/label&gt;<\/p>\n<p>&lt;source_model&gt;Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;specificerrmsg&#8221; translate=&#8221;label&#8221; type=&#8221;textarea&#8221; sortOrder=&#8221;93&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;1&#8243;&gt;<\/p>\n<p>&lt;label&gt;Displayed Error Message&lt;\/label&gt;<\/p>\n<p>&lt;depends&gt;<\/p>\n<p>&lt;field id=&#8221;show_method&#8221;&gt;1&lt;\/field&gt;<\/p>\n<p>&lt;\/depends&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;field id=&#8221;sort_order&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;100&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;0&#8243;&gt;<\/p>\n<p>&lt;label&gt;Sort Order&lt;\/label&gt;<\/p>\n<p>&lt;\/field&gt;<\/p>\n<p>&lt;\/group&gt;<\/p>\n<p>&lt;\/section&gt;<\/p>\n<p>&lt;\/system&gt;<\/p>\n<p>&lt;\/config&gt;<\/p><\/blockquote>\n<p>Create a folder named \u201cModel\/ Carrier\u201d in your module root folder<\/p>\n<p>Create one class file in above folder Customflatrate.php, this class will extend \\Magento\\Shipping\\Model\\Carrier\\AbstractCarrier class and implements \\Magento\\Shipping\\Model\\Carrier\\CarrierInterface interface<\/p>\n<p>We are going to override collectRates method and have our login inside it.<\/p>\n<p>So,\u00a0 Customflatrate.php will have below code.<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>namespace Mconnect\\Customflatrate\\Model\\Carrier;<\/p>\n<p>use Magento\\Quote\\Model\\Quote\\Address\\RateRequest;<\/p>\n<p>use Magento\\Shipping\\Model\\Rate\\Result;<\/p>\n<p>class Customflatrate extends \\Magento\\Shipping\\Model\\Carrier\\AbstractCarrier implements<\/p>\n<p>\\Magento\\Shipping\\Model\\Carrier\\CarrierInterface<\/p>\n<p>{<\/p>\n<p>protected $_code = &#8216;mconnectcustomflatrate&#8217;;<\/p>\n<p>protected $_isFixed = true;<\/p>\n<p>protected $_rateResultFactory;<\/p>\n<p>protected $_rateMethodFactory;<\/p>\n<p>public function __construct(<\/p>\n<p>\\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig,<\/p>\n<p>\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory $rateErrorFactory,<\/p>\n<p>\\Psr\\Log\\LoggerInterface $logger,<\/p>\n<p>\\Magento\\Shipping\\Model\\Rate\\ResultFactory $rateResultFactory,<\/p>\n<p>\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory $rateMethodFactory,<\/p>\n<p>array $data = []<\/p>\n<p>) {<\/p>\n<p>$this-&gt;_rateResultFactory = $rateResultFactory;<\/p>\n<p>$this-&gt;_rateMethodFactory = $rateMethodFactory;<\/p>\n<p>parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);<\/p>\n<p>}<\/p>\n<p>public function collectRates(RateRequest $request)<\/p>\n<p>{<\/p>\n<p>if (!$this-&gt;getConfigFlag(&#8216;active&#8217;)) {<\/p>\n<p>return false;<\/p>\n<p>}<\/p>\n<p>$items = $request-&gt;getAllItems();<\/p>\n<p>$perOrderRate = array();<\/p>\n<p>$shippingPrice = $this-&gt;getConfigData(&#8216;default_shipping_cost&#8217;);<\/p>\n<p>$shippingPrice = $this-&gt;getFinalPriceWithHandlingFee($shippingPrice);<\/p>\n<p>$grandTotal = $request-&gt;getBaseSubtotalInclTax();<\/p>\n<p>$free_shipping_over_total = $this-&gt;getConfigData(&#8216;free_shipping_over_total&#8217;);`<\/p>\n<p>if ($shippingPrice !== false) {<\/p>\n<p>$method = $this-&gt;_rateMethodFactory-&gt;create();<\/p>\n<p>$method-&gt;setCarrier(&#8220;mconnectcustomflatrate&#8221;);<\/p>\n<p>$method-&gt;setCarrierTitle($this-&gt;getConfigData(&#8216;title&#8217;));<\/p>\n<p>$method-&gt;setMethod(&#8220;mconnectflatrate&#8221;);<\/p>\n<p>$method-&gt;setMethodTitle($this-&gt;getConfigData(&#8216;name&#8217;));<\/p>\n<p>$method-&gt;setPrice($shippingPrice);<\/p>\n<p>$method-&gt;setCost($shippingPrice);<\/p>\n<p>$result-&gt;append($method);<\/p>\n<p>}<\/p>\n<p>return $result;<\/p>\n<p>}<\/p>\n<p>public function getAllowedMethods()<\/p>\n<p>{<\/p>\n<p>return [&#8220;mconnectflatrate&#8221; =&gt; $this-&gt;getConfigData(&#8216;name&#8217;)];<\/p>\n<p>}<\/p>\n<p>}<\/p><\/blockquote>\n<p>It\u2019s all done. That was all about the the implementation of shipping methods in Magento 2. Have you liked it ? Tell us your reviews via comments. If you need any help while adding new shipping method in your store, feel free to contact our <span><a href=\"https:\/\/www.mconnectmedia.com\/magento-2-development-solutions\">Magento<sup>\u00ae<\/sup> 2 Developers<\/a><\/span>. We will be happy to help you out! Happy Coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The shipping methods can affect a customer\u2019s decision to buy a certain product. However, it becomes really important that the online stores have various shipping<\/p>\n","protected":false},"author":1,"featured_media":5949,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[618,620,617,915,914,619,916,616],"class_list":["post-5941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ecommerce","tag-create-custom-shipping-module-in-magento2","tag-how-to-add-new-shipping-method-in-magento-2","tag-magento-2-shipping-method-implementation","tag-magento-custom-shipping-method-extension","tag-magento-shipping-module","tag-new-shipping-method-extensions-implementation-in-magento-2-0-store","tag-shipping-method-implementation","tag-shipping-methods-implementation-in-magento-2"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/posts\/5941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/comments?post=5941"}],"version-history":[{"count":5,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/posts\/5941\/revisions"}],"predecessor-version":[{"id":14954,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/posts\/5941\/revisions\/14954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/media\/5949"}],"wp:attachment":[{"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/media?parent=5941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/categories?post=5941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mconnectmedia.com\/blog\/wp-json\/wp\/v2\/tags?post=5941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}