Home > database >  creating several custom carriers in Magento2
creating several custom carriers in Magento2

Time:09-18

I am kinda new in coding and super new in Magento. I have created a custom carrier following these instructions: https://devdocs.magento.com/guides/v2.4/howdoi/checkout/checkout-add-custom-carrier.html#create-configuration and it works like a charm. Then, I tried to create one more custom carrier by duplicating and renaming my Vendor folder. I have also altered some stuff in the code:

in registration.php

    <?php
    
    use Magento\Framework\Component\ComponentRegistrar;
    
    ComponentRegistrar::register(
        ComponentRegistrar::MODULE,
        'Vendor2_CustomShipping',
        __DIR__
    );

in composer.json

    {
        "name": "vendor2/custom-shipping",
        "description": "Custom shipping module",
        "require": {
            "php": "~7.2.0||~7.3.0",
            "magento/framework": "102.0.*",
            "magento/module-backend": "101.0.*",
            "magento/module-catalog": "103.0.*",
            "magento/module-config": "101.1.*",
            "magento/module-directory": "100.3.*",
            "magento/module-quote": "101.1.*",
            "magento/module-sales": "102.0.*",
            "magento/module-sales-rule": "101.1.*",
            "magento/module-shipping": "100.3.*",
            "magento/module-store": "101.0.*"
        },
        "type": "magento2-module",
        "license": [
            "OSL-3.0",
            "AFL-3.0"
        ],
        "autoload": {
            "files": [
                "registration.php"
            ],
            "psr-4": {
                "Vendor2\\CustomShipping\\": ""
            }
        },
        "version": "1.0.0"
    }

in module.xml:

    <?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="Vendor2_CustomShipping" >
            <sequence>
                <module name="Magento_Store"/>
                <module name="Magento_Sales"/>
                <module name="Magento_Quote"/>
                <module name="Magento_SalesRule"/>
            </sequence>
        </module>
    </config>

in 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>
                <customshipping>
                    <active>0</active>
                    <title>Custom Shipping Title</title>
                    <name>Custom Shipping Method Name</name>
                    <shipping_cost>10</shipping_cost>
                    <sallowspecific>0</sallowspecific>
                    <sort_order>15</sort_order>
                    <model>Vendor2\CustomShipping\Model\Carrier\Customshipping</model>
                </customshipping>
            </carriers>
        </default>
    </config>

and in the Customshipping.php:

namespace Vendor2\CustomShipping\Model\Carrier;

I successfully enabled the new module and upgraded magento, but when I go to my admin I can see only one custom carrier option available. I also tried removing firstly created module and that makes the second one appear. My guess is that there is a conflict but I don't know how to resolve it. thanks

CodePudding user response:

The second shipping method is overwriting the first shipping method. To prevent this, some more changes are needed:

In etc/adminhtml/system.xml change <group id="customshipping" ...> to <group id="customshipping2" ...>.

Also in etc/config.xml change <customshipping> to <customshipping2>.

In Model/Carrier/Customshipping.php change protected $_code = 'customshipping'; to protected $_code = 'customshipping2';.

With these changes, you should be able to see two new shipping methods in the Magento admin.

  • Related