How to create a Magento 2 basic extension

By | January 5, 2023
Spread the love

In below steps, you will get how to create a basic extension. Before we start, there are some requirements that we need to check.

  1. You must have the latest Magento 2 version installed in your system.
  2. You need to disable the Magneto Cache: It needs to be done so that you can save some time on the manual flush system when you make changes in the codes.
  3. Select developer mode: It lets you see all the errors from Magento’s end.

Steps to create Extension:

  1. Create a folder in app/code/Techgroup/Addon
  2. Create registration.php file here with following contents
    <?php
    
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Techgroup_Addon',
        __DIR__
    );

     

  3. Create composer.json file in same folder. Update your correct values where is “************”.
    {
        "name" : "techgroup/addon",
        "description" : "Techgroup Addon",
        "type" : "magento2-module",
        "version" : "1.0.0",
        "license" : "GPL",
        "authors" : [{
                "name" : "**************",
                "email" : "**********",
                "homepage" : "************",
                "role" : "Developer"
            }
        ],
        "autoload" : {
            "files" : [
                "registration.php"
            ],
            "psr-4" : {
                "Techgroup\\Addon\\" : ""
            }
        }
    }
  4. Create another folder app/code/Techgroup/Addon/etc.
  5. Create a module.xml in this folder with following content.
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="Techgroup_Addon" setup_version="1.0.0">
            <sequence>
                <module name="Magento_Backend"/>
                <module name="Magento_Sales"/>
                <module name="Magento_Quote"/>
                <module name="Magento_Checkout"/>
                <module name="Magento_Cms"/>
            </sequence>
         </module>
    </config>
  6. Create another file di.xml if needed to override classes.
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    
    </config>
  7. Some other files and folder you can create like app/code/Techgroup/Addon/etc/frontend, app/code/Techgroup/Addon/etc/adminhtml etc.
  8. Run deploy commands
    sudo php bin/magento setup:upgrade
    sudo php bin/magento setup:static-content:deploy -f
    sudo php bin/magento indexer:reindex
    sudo php bin/magento setup:di:compile