Create Magento 2 Custom Module Development

Posted by

Magento 2, renowned for its flexibility and extensibility, empowers developers to tailor their e-commerce platforms with custom modules. Building a custom module is a fundamental aspect of Magento 2 development, allowing you to extend functionality, modify existing features, or introduce entirely new capabilities. Let’s embark on a step-by-step journey to create a custom module in Magento 2. Some developer refer as an extension and some also refer it as a plugin. However a plugin is a different concept in Magento.

Module Structure

Start by establishing the structure of your custom module. Create a directory for your module within the app/code directory. For example, if your module is named CustomModule, the path would be app/code/Vendor/CustomModule.

Mandatory files required for custom module

There are 2 files which are important while creating any module in Magento 2 and Adobe commerce. These two files are module.xml and registration.php. These files act as a base of any extension.

Module Registration

Create a registration.php file in your module directory. This file registers your module with Magento, providing essential information about its existence and location.

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

Module Declaration

Next, create a module.xml file in the etc directory of your module. This file declares your module, specifying its name, version, and other crucial details.

<?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="Vendor_CustomModule" setup_version="1.0.0" />
</config>

There is one more file which is needed but its an optional one when you are creating a custom module without needing any 3rd party code. The file name is composer.json.

Composer.json

This JSON file, residing at the root of the custom module directory, specifies essential details such as the module’s name, version, description, dependencies, and autoloading information. It plays a pivotal role in facilitating the installation and integration of the custom module with the broader Magento system. The composer.json file ensures that necessary dependencies are resolved and downloaded, contributing to a modular and maintainable Magento 2 ecosystem. Its importance lies in streamlining the deployment process, enhancing code reusability, and ensuring the seamless integration of custom modules within the larger Magento framework.

{
    "name": "Learningmagento/Logcleaner",
    "description": "This plugin is for emptying log files",
    "minimum-stability": "stable",
    "authors": [
        {
            "name": "Learning Magento",
            "email": "contactus@learningmagento.com"
        }
    ],
    "version": "1.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "Learningmagento\\Logcleaner\\": ""
        }
    }
}

Installing the newly created custom module

Installing a module in Magento 2 is a straightforward process that involves a series of commands executed via the command-line interface (CLI). After obtaining the module package, typically distributed as a compressed archive or accessible through version control, navigate to the root directory of your Magento 2 installation.

Use the composer require command, specifying the package name and version, to download and install the module and its dependencies. Once the installation is complete, run the commands php bin/magento setup:upgrade and php bin/magento setup:static-content:deploy to apply the module changes, update the database schema, and deploy static content. Finally, clear the cache with php bin/magento cache:flush to ensure that the changes take effect. This systematic approach ensures a smooth installation process, allowing the newly added module to seamlessly integrate into the Magento 2 environment.

Reference Module

In our blog we have created different extension some of them are Create Order and Quote Programmatically in Magento 2, Create Custom Shipping Method , Create Custom Payment Method and Easy way to Create Custom command. You can these article and also the git repo which is given added inside the article.

Overall

These are the 3 most important files which you need while creating a module. In addition you can also add readme.md file which will explain and give installation of the module. This file is best when you are deploying your module to Magento Marketplace or on github. To learn more please go through on our category on development.