{"id":7844,"date":"2022-01-28T10:33:17","date_gmt":"2022-01-28T10:33:17","guid":{"rendered":"https:\/\/www.bay20.com\/?p=7844"},"modified":"2025-05-21T14:38:10","modified_gmt":"2025-05-21T08:53:10","slug":"eine-versandmethode-erstellen-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.bay20.com\/de\/eine-versandmethode-erstellen-in-magento-2\/","title":{"rendered":"Erstellen einer Versandmethode in Magento 2"},"content":{"rendered":"\n<p><strong>Schritt 1: <\/strong>Erstellen Sie ein neues Modul mit<strong> Vendor_CustomShipping<\/strong><\/p>\n\n\n\n<p>app\/code\/Vendor\/CustomShipping\/registration.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    'Vendor_CustomShipping',\n    __DIR__\n);\n\n<\/code><\/pre>\n\n\n\n<p><strong>Schritt 2: <\/strong>Erstellen Sie die Datei<strong> composer.json.<\/strong><\/p>\n\n\n\n<p>app\/code\/Vendor\/CustomShipping\/composer.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"vendor\/custom-shipping\",\n    \"description\": \"Custom shipping module\",\n    \"require\": {\n        \"php\": \"~7.2.0||~7.3.0\",\n        \"magento\/framework\": \"102.0.*\",\n        \"magento\/module-backend\": \"101.0.*\",\n        \"magento\/module-catalog\": \"103.0.*\",\n        \"magento\/module-config\": \"101.1.*\",\n        \"magento\/module-directory\": \"100.3.*\",\n        \"magento\/module-quote\": \"101.1.*\",\n        \"magento\/module-sales\": \"102.0.*\",\n        \"magento\/module-sales-rule\": \"101.1.*\",\n        \"magento\/module-shipping\": \"100.3.*\",\n        \"magento\/module-store\": \"101.0.*\"\n    },\n    \"type\": \"magento2-module\",\n    \"license\": &#91;\n        \"OSL-3.0\",\n        \"AFL-3.0\"\n    ],\n    \"autoload\": {\n        \"files\": &#91;\n            \"registration.php\"\n        ],\n        \"psr-4\": {\n            \"Vendor\\\\CustomShipping\\\\\": \"\"\n        }\n    },\n    \"version\": \"1.0.0\"\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Schritt 3: <\/strong>Erstellen Sie die Datei <strong>module.xml.<\/strong><\/p>\n\n\n\n<p>app\/code\/Vendor\/CustomShipping\/etc\/module.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\n    &lt;module name=\"Vendor_CustomShipping\" setup_version=\"1.0.0\"&gt;\n        &lt;sequence&gt;\n            &lt;module name=\"Magento_Store\"\/&gt;\n            &lt;module name=\"Magento_Sales\"\/&gt;\n            &lt;module name=\"Magento_Quote\"\/&gt;\n            &lt;module name=\"Magento_SalesRule\"\/&gt;\n        &lt;\/sequence&gt;\n    &lt;\/module&gt;\n&lt;\/config&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Schritt 4: <\/strong>Erstellen Sie die Datei<strong> system.xml.<\/strong><\/p>\n\n\n\n<p>app\/code\/Vendor\/CustomShipping\/etc\/adminhtml\/system.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\"&gt;\n    &lt;system&gt;\n        &lt;section id=\"carriers\" translate=\"label\" type=\"text\" sortOrder=\"320\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\n            &lt;group id=\"customshipping\" translate=\"label\" type=\"text\" sortOrder=\"900\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\n                &lt;label&gt;Custom Shipping Module&lt;\/label&gt;\n                &lt;field id=\"active\" translate=\"label\" type=\"select\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\" canRestore=\"1\"&gt;\n                    &lt;label&gt;Enabled&lt;\/label&gt;\n                    &lt;source_model&gt;Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"title\" translate=\"label\" type=\"text\" sortOrder=\"20\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\"&gt;\n                    &lt;label&gt;Title&lt;\/label&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"name\" translate=\"label\" type=\"text\" sortOrder=\"30\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\"&gt;\n                    &lt;label&gt;Method Name&lt;\/label&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"shipping_cost\" translate=\"label\" type=\"text\" sortOrder=\"40\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\" &gt;\n                    &lt;label&gt;Shipping Cost&lt;\/label&gt;\n                    &lt;validate&gt;validate-number validate-zero-or-greater&lt;\/validate&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"sallowspecific\" translate=\"label\" type=\"select\" sortOrder=\"60\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\" canRestore=\"1\"&gt;\n                    &lt;label&gt;Ship to Applicable Countries&lt;\/label&gt;\n                    &lt;frontend_class&gt;shipping-applicable-country&lt;\/frontend_class&gt;\n                    &lt;source_model&gt;Magento\\Shipping\\Model\\Config\\Source\\Allspecificcountries&lt;\/source_model&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"specificcountry\" translate=\"label\" type=\"multiselect\" sortOrder=\"70\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\"&gt;\n                    &lt;label&gt;Ship to Specific Countries&lt;\/label&gt;\n                    &lt;source_model&gt;Magento\\Directory\\Model\\Config\\Source\\Country&lt;\/source_model&gt;\n                    &lt;can_be_empty&gt;1&lt;\/can_be_empty&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"showmethod\" translate=\"label\" type=\"select\" sortOrder=\"80\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\"&gt;\n                    &lt;label&gt;Show Method if Not Applicable&lt;\/label&gt;\n                    &lt;source_model&gt;Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model&gt;\n                    &lt;frontend_class&gt;shipping-skip-hide&lt;\/frontend_class&gt;\n                &lt;\/field&gt;\n                &lt;field id=\"sort_order\" translate=\"label\" type=\"text\" sortOrder=\"90\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\"&gt;\n                    &lt;label&gt;Sort Order&lt;\/label&gt;\n                &lt;\/field&gt;\n            &lt;\/group&gt;\n        &lt;\/section&gt;\n    &lt;\/system&gt;\n&lt;\/config&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Schritt 5: <\/strong>Erstellen Sie die Datei<strong> config.xml.<\/strong><\/p>\n\n\n\n<p>app\/code\/Vendor\/CustomShipping\/etc\/config.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Store:etc\/config.xsd\"&gt;\n    &lt;default&gt;\n        &lt;carriers&gt;\n            &lt;customshipping&gt;\n                &lt;active&gt;0&lt;\/active&gt;\n                &lt;title&gt;Custom Shipping Title&lt;\/title&gt;\n                &lt;name&gt;Custom Shipping Method Name&lt;\/name&gt;\n                &lt;shipping_cost&gt;10&lt;\/shipping_cost&gt;\n                &lt;sallowspecific&gt;0&lt;\/sallowspecific&gt;\n                &lt;sort_order&gt;15&lt;\/sort_order&gt;\n                &lt;model&gt;Vendor\\CustomShipping\\Model\\Carrier\\Customshipping&lt;\/model&gt;\n            &lt;\/customshipping&gt;\n        &lt;\/carriers&gt;\n    &lt;\/default&gt;\n&lt;\/config&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Schritt 6: <\/strong>Erstellen Sie die Datei<strong> Customshipping.php.<\/strong><\/p>\n\n\n\n<p>app\/code\/Vendor\/CustomShipping\/Model\/Carrier\/Customshipping.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Vendor\\CustomShipping\\Model\\Carrier;\n\nuse Magento\\Quote\\Model\\Quote\\Address\\RateRequest;\nuse Magento\\Shipping\\Model\\Carrier\\AbstractCarrier;\nuse Magento\\Shipping\\Model\\Carrier\\CarrierInterface;\n\n\/**\n * Custom shipping model\n *\/\nclass Customshipping extends AbstractCarrier implements CarrierInterface\n{\n    \/**\n     * @var string\n     *\/\n    protected $_code = 'customshipping';\n\n    \/**\n     * @var bool\n     *\/\n    protected $_isFixed = true;\n\n    \/**\n     * @var \\Magento\\Shipping\\Model\\Rate\\ResultFactory\n     *\/\n    private $rateResultFactory;\n\n    \/**\n     * @var \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory\n     *\/\n    private $rateMethodFactory;\n\n    \/**\n     * @param \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig\n     * @param \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory $rateErrorFactory\n     * @param \\Psr\\Log\\LoggerInterface $logger\n     * @param \\Magento\\Shipping\\Model\\Rate\\ResultFactory $rateResultFactory\n     * @param \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory $rateMethodFactory\n     * @param array $data\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig,\n        \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory $rateErrorFactory,\n        \\Psr\\Log\\LoggerInterface $logger,\n        \\Magento\\Shipping\\Model\\Rate\\ResultFactory $rateResultFactory,\n        \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory $rateMethodFactory,\n        array $data = &#91;]\n    ) {\n        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);\n\n        $this-&gt;rateResultFactory = $rateResultFactory;\n        $this-&gt;rateMethodFactory = $rateMethodFactory;\n    }\n\n    \/**\n     * Custom Shipping Rates Collector\n     *\n     * @param RateRequest $request\n     * @return \\Magento\\Shipping\\Model\\Rate\\Result|bool\n     *\/\n    public function collectRates(RateRequest $request)\n    {\n        if (!$this-&gt;getConfigFlag('active')) {\n            return false;\n        }\n\n        \/** @var \\Magento\\Shipping\\Model\\Rate\\Result $result *\/\n        $result = $this-&gt;rateResultFactory-&gt;create();\n\n        \/** @var \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Method $method *\/\n        $method = $this-&gt;rateMethodFactory-&gt;create();\n\n        $method-&gt;setCarrier($this-&gt;_code);\n        $method-&gt;setCarrierTitle($this-&gt;getConfigData('title'));\n\n        $method-&gt;setMethod($this-&gt;_code);\n        $method-&gt;setMethodTitle($this-&gt;getConfigData('name'));\n\n        $shippingCost = (float)$this-&gt;getConfigData('shipping_cost');\n\n        $method-&gt;setPrice($shippingCost);\n        $method-&gt;setCost($shippingCost);\n\n        $result-&gt;append($method);\n\n        return $result;\n    }\n\n    \/**\n     * @return array\n     *\/\n    public function getAllowedMethods()\n    {\n        return &#91;$this-&gt;_code =&gt; $this-&gt;getConfigData('name')];\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Schritt 7: <\/strong>Aktivieren des Moduls<\/p>\n\n\n\n<p>Run the below commands to register Vendor_Customshipping module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento module:enable Vendor_Customshipping<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:upgrade<\/code><\/pre>\n\n\n\n<p><strong>Bitte kontaktieren Sie uns unter <strong><a href=\"mailto:manish@bay20.com\">manish@bay20.com<\/a><\/strong> oder rufen Sie uns unter <strong><a href=\"https:\/\/api.whatsapp.com\/send?phone=+918800519180&amp;text=Hi, I contacted you through your website.\" title=\"+91-8800519180\">+91-8800519180<\/a><\/strong> an, wenn Sie Unterst\u00fctzung in Bezug auf Magento ben\u00f6tigen. Sie k\u00f6nnen auch die <strong><a href=\"https:\/\/www.bay20.com\/de\/magento-entwicklungsunternehmen\/\" title=\"Magento development\">Magento<strong>-Entwicklungsseite<\/strong><\/a><\/strong> besuchen, um die von uns angebotenen Dienstleistungen zu \u00fcberpr\u00fcfen.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 has a variety of shipping methods available, Create a shipping method in Magento 2<\/p>\n","protected":false},"author":25,"featured_media":10098,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,77,79],"tags":[],"class_list":["post-7844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-entwicklung","category-magento2","category-magento-anleitung"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts\/7844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/comments?post=7844"}],"version-history":[{"count":10,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts\/7844\/revisions"}],"predecessor-version":[{"id":17219,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts\/7844\/revisions\/17219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/media\/10098"}],"wp:attachment":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/media?parent=7844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/categories?post=7844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/tags?post=7844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}