MAGENTO MODULE: INSTALLING AND CREATING

13/03/2020 334
Magento modules
E-COMMERCE

When discussing e-Commerce development, it goes without saying that Magento would always be on top of the list of the best platforms because of its robustness and variable functions. Besides beautiful themes,  Magento module is also an important element that should be of your concern while building your e-commerce site. For Magento modules, you can either download the existing ones or create whole new ones for your needs. In this article, we will introduce steps to install and create (and rules for creating) Magento modules.

 

A. An overview of Magento module

1. What is a Magento module?

A module is a sensible gathering – that is, a catalog containing squares, controllers, partners, models – that are identified with a particular business highlight. With regards to Magento’s promise to ideal seclusion, a module encapsulates one element and has insignificant conditions on different modules.

Modules and themes are the units of customization in Magento.  While modules give business features, while subjects firmly impact client experience and storefront appearance. The two parts have an actual existence cycle that permits them to be introduced, erased, and impaired. From the point of view of the two traders and extension designers, modules are the focal unit of the Magento organization.

2. Purpose a module

The reason for a module is to give explicit items includes by executing new usefulness or expanding the usefulness of different modules. Every module is intended to work freely, so the incorporation or rejection of a specific module doesn’t commonly influence the usefulness of different modules.

 

B. Installing the existing Magento Modules

Magento 2 gives you an opportunity to manually install modules using 3 different methods: via composer, via ZIP-archive, and via a browser. The choice of a method depends on the way that a module appeared in the system. Let’s consider each of them in detail.

Here are the two ways that modules can appear in the system:

It is in the Magento store. You have 2 access keys, generated in your marketplace account.

  • You can add these keys to the Magento admin panel. Your Magento marketplace account synchronizes with it, and you can manage the installation of any purchased packages there.
  • If you are an advanced user and can use the server console, you can install a module via the composer. You will also need to enter the keys if they were not previously saved in the system.

Third-party sources installing Magento Module

  • Get it in the form of an archive.
  • Receive a link to a third-party repository, e.g.: GitHub. Installation from it can also be done via the composer.

Now let’s take a closer look at each method of module installation.

 

1. Generating “Access Keys” in a Magento account

You can log in or create a new account at magento.com. After that click on the “Marketplace” link in your account.

How to install modules manually in Magento2

Or go directly to the Magento extensions store.

In “My Profile” we are looking for the “My Products / Access Keys” section. You just need to generate the necessary keys.

 

2. Installing modules via Composer

This is a way for you if you know how to use the server console.

The composer is a file called composer.phar. You can download it at getcomposer.org. It is launched from the Magento root directory. A folder should contain the composer.json file with the configuration of the installed libraries.

php composer.phar <command>

If the composer is installed globally on the server, there is no need to download it and you can refer to it in the following form:

composer <command>

  • If the module is distributed from the Magento repository, we can just run the following commands:

php composer.phar require [vendor]/[package]

php bin/magento setup:upgrade

And they will do the rest of the job.

  • Or it could be a module from a third-party repository and we will need to specify it in the composer.json file.

We are interested in the “require” part, where we indicate the name of the module that we add.

 

If the third-party repository is, for example, GitHub, you will need to specify it in the “repositories” branch.

Usually, all this data is provided with the module.

Next, after saving the file, run the following commands:

php composer.phar update [vendor]/[package]

php bin/magento setup:upgrade

For example, let’s take a look at the installation of one of our free modules. It is located in the GitHub repository. Since the module is not installed from the Magento repository, we’ll add it to the composer.json file.

And apply our module there:

It only remains to start the updating process. The necessary modules and add-ons will be downloaded according to the changes that were made to the composer.json file.

php composer.phar update

Then start the installation of all the new modules in Magento.

php bin/magento setup:upgrade

 

3. Installing modules via ZIP-archive

When you wonder how to add the extension in Magento 2 firstly you should copy it’s code to the required directory:

How to install Magento module

Left panel shows here the basic listing of the zip-file with a module.

Right panel — Magento 2 codebase.

Current zip-file contains User_Guide.pdf file and the Install directory. Basically, the Install folder content should be copied to the app/code/ directory.

After that, we can use 2 different methods to initialize the module in a store.

 

4. Installing modules via a browser

This method requires Cron tasks set up and proper running. In case it has not performed yet — do it. You will need it further, not only while extensions installing.

So if Cron is set up correctly, just follow the path in admin panel to initialize the module:

System/ Web Setup Wizard/ Component Manager

Here you can enable all available modules.

How to install Magento module manually

In case Magento is set up properly you’ll see the notification that all of the systems run correctly.

Installing toturial

Further, before you enable the custom module in Magento 2, Backup creation will be proposed.

And finally, we will see the following screen:

Installing toturial

The module has been enabled. And now we can switch back to the module list and enable (install) the next one.

Console commands in Magento

You have already met one of them:

php bin/magento <command>

Magento CLI provides a large number of useful console commands to manage the store. And you can get the list of these commands by running: php bin/magento.

Installing toturial

But now we need to use only one:

php bin/magento setup:upgrade

This command checks all of the modules and launches schema installation or updating process (if necessary). So you just need one command to perform updating and installation of all modules.

The rights to the files

For greater security (or for other reasons), the server can be configured so that the installation of the modules is performed by a specific user.

In this case, when executing console commands:

php composer.phar <command>

php bin/magento <command>

there may be reports of insufficient rights.

You can try running the same commands on behalf of another user or simply from the administrator. For example:

sudo -u <username> php bin/magento <command>

or simply

sudo php bin/magento <command>

Also, you may need full rights for some folders. For example, when installing modules, you may not have the access to some files from the app/etc directory.

chmod 777 app/etc

chmod 644 app/etc/*.xml

Or to the var folder containing a lot of temporary files, and the rights to which should be distributed to all subdirectories (the -R parameter is recursive).

chmod -R 777 var/

 

C. Creating new Magento Modules

1. Things to remember when creating Magento module

  • Know about obscure unpredictability

It could be quick to add custom credits to Magento 2, be that as it may, there could be viewed as mind-boggling improvement being associated with such activity. You would be advised to execute custom characteristics on certain activities previously, else, it ought to be generally off base.
What is prescribed here is investing energy exploring and arranging improvement undertakings to accomplish exact estimation.
  • Follow Versioning Policy  

Make sure to stay with the versioning policy in your custom Magento 2 module. This would cause the module to turn out to be progressively steady and strong on the off chance that Magento discharges another rendition.
Additionally, the Composer bundle director assists with indicating and keep up the exceptional conditions on different bundles and Magento 2 modules.
The composer.json model with Magento 2 module conditions. 
Plus, the significant part plays conditions on other Magento 2 modules. If, suppose, composer.json from custom module has 100.1.* rendition reliance on Magento/module-index module implies that all PATCH refreshes are good with a custom module.
Practically speaking, there are more modules that don’t have clear API or full API inclusion and you will wind up having 100.1.3 reliance. Such modules in all probability use Inheritance as opposed to Composition.
  • Follow the Magento 2 center in a smart manner.

A great deal of Magento designers may state: “It is a similar methodology as in Magento 2 center and I followed (read duplicate stuck) same methodologies and it ought to be correct”. However, it ought to be sensible to follow the Magento 2 center executions. However, there are still huge pieces of code that are made arrangements for refactoring and reimplementation by the Magento Team.
For example, you locate a comparable execution in the Magento\Payment module to fabricate custom installment usage. As a feature of the module, you will discover an AbstractMethod class and instances of its usage in installment modules that meet up with Magento 2 Community Edition. Curiously, the methodology of executing installment incorporations of Magento 2 follows precisely the equivalent Magento 1, which is belittled.
Most significant is that AbstractMethod is set apart as @deprecated and shouldn’t be utilized. There is a Payment Gateway API where all parts or segments can be actualized independently from one another. The main thing you need to guarantee is to arrange these installment parts appropriately by means of the di.xml design record.
Magento Documentation or DevDocs is a decent beginning stage in the event that you don’t know about engineering or best practices for Magento 2 advancement.
  • Give Localization Support

Before you conclude that module is fit to be dispatched to Magento Marketplace or GitHub for network ensure you have limitation bolster included.
All writings situated in a custom module ought to be wrapped with __(‘My Text’) work. It ought to be utilized all over, classes, formats, JavaScript segments, email layouts, and so on.

 

  • Use Composer File

Significance of composer.json record may be thought little of. Each and every Magento module, bundle, topic, and limitation ought to incorporate the composer.json record with all conditions recorded. A great deal of Magento 2 modules was just 1 reliance list which is “Magento/system”. This is an erroneous presumption to have reliance just for Framework if your module utilizes Magento\Catalog\Api interfaces from the “Magento/module-index” bundle for instance.

Also, Magento 1 methodology was to make an uncommon class where every utility capacity/technique were found. Once in awhile these strategies have a connection between one another, in any case, in most circumstances, such an extraordinary class was holding an excessive number of duties. This class is otherwise called Helper.

Above all else, abstain from utilizing Helpers as a term and as a definition to God Give Me All class in Magento 2. It is in every case better to make 5 littler classes with clear naming and reason than just Helper.php.

For instance, in the event that you have Attribute Helper class with 2 obligations a) Merging quality qualities; b) Mapping property estimations; consider having 2 autonomous classes with its own duty:

AttributeMerger

AttributeMapper

 

  • Fundamental Menu Usage

Each time you introduce the Magento module with Magento Admin capacities you can see that another Menu Icon is added to one side primary menu.

There is some deceptive suspicion that if the shipper introduces “my” module it is the fundamental and most utilized capacities and ought to be recorded under the primary menu.

In 99% of cases custom module requires design settings and for this Magento gives extraordinary page under Magento Admin – > Stores – > Configuration menu. So if your module causes the trader to expand change in all probability “Advertising” is a decent name of the setup segment. On the off chance that the custom module modifies existing usefulness, it ought to be a piece of the current arrangement area.

 

  • Test Module with Different Deployment Modes

It is generally the excellent practice to switch among creation and advancement application modes to guarantee the custom module doesn’t break any current usefulness. As per the most recent report by the Magento Marketplace group, 70% of modules can’t aggregate with creation mode.

Take some time and plan to test with creation mode before discharging the Magento module to Magento Marketplace or anyplace else. Magento Ecosystem requires solid and stable modules.

 

  • Testing is as significant as code

Consider including various kinds of tests while building a custom module for Magento 2. With respect to now, you can discover various sorts remembered for Magento 2 including Unit Tests, Integration Tests, Functional Tests, and so forth.

Without a doubt beginning estimation of exertion will be expanded for building tests, costs for support, in any case, would be lower. Features can be included a lot quicker with a decent degree of certainty for existing ones to be functioning truly to form.

 

  • Code Review

No matter you work in a group or an engineer figuring out how to demonstrate your code to different developers and request to give code audit. It doesn’t make a difference whether you are a Senior Developer who knows completely everything or Junior Developer who just began the significance of code survey can’t be disparaged.

One little TYPO in code can prompt site personal time and an enormous income drop for a storekeeper. Companion audit could give a superior and cleaner form of the code.

 

  • Granularity Matters

It is a decent practice to assemble usefulness such that bundle or module is liable for little complete pieces of the usefulness. It will help to have a base measure of outside conditions on other Magento 2 modules.

2. Step to create a new Magento module

To create a module, you need to complete the following high-level steps: Create

  1. Module folder.
  2. etc/module.xml file.
  3. registration.php file.
  4. Run the bin/magento setup:upgrade script to install the new module.
  5. Check that the module is working.

Let’s go through each of these steps in detail.

  • Create the module folder

There are two possible locations for modules in Magento 2: the app/code folder and the vendor folder

Depending on how Magento 2 has been installed, core modules can either be located in the vendor/magento/magento-*folders (for composer installation) or in the app/code/Magento/ folder (for cloning GitHub).

Which of these locations should you choose for your new module?

If you build a module for a specific project, it is best to choose the app/code folder and commit to the project’s repository.

On the other hand, in case you build an extension to be reused, it is better to use composer to create it, and put your module in the vendor/<YOUR_VENDOR>/module-something folder.

Each module name in Magento 2 consists of two parts – the vendor and the module itself. In other words, modules are grouped into vendors, so you need to define the vendor and module names. For this example, let’s name the vendor “Learning” and the module “FirstUnit”.

Let’s create the folder app/code/Learning and inside this folder place another folder: FirstUnit. If you’re using the command line, the code would be:

  1. cd to the root folder
  2. mkdir app/code/Learning
  3. mkdir app/code/Learning/FirstUnit

Make sure you have permission to create files and folders in your installation

 

  • Create an etc/module.xml file. This file is required for the module to exist.

This file contains the following information:

– Module name & version

– Dependencies

The module name is defined by the folders we just created because in Magento 2, class names must follow the folder structure. Because we created the folders Learning/FirstUnit, our module name will be Learning_FirstUnit and all classes that belong to this module will begin with Learning\FirstUnit – for example: Learning\FirstUnit\Observer\Test.

Dependencies. If one module depends on another, the module.xml file will have a special declaration that defines a list of modules that the current module depends on. For this example, we will make our module dependent on Magento_Catalog.

Using the following command-line code, create the folder app/code/Learning/FirstUnit/etc:

mkdir app/code/Learning/FirstUnit/etc
Then put the following code into it:
1
2
3
4
5
6
<?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="Learning_FirstUnit" setup_version="0.0.1"> <sequence>
<module name="Magento_Catalog"/> </sequence>
    </module>
</config>

Note that in the XML file we specified:

– Module name: Learning_FirstUnit (based on the folders we created)

– Version: 0.0.1 (initial version of our module)

– Dependency: Magento_Catalog. We could have multiple dependencies. In this case, we would put <module name=”..” /> nodes under the sequence node.

 

  • Create the registration.php file

Each module must have this file, which tells Magento how to locate the module. Continuing our example, create the file app/code/Learning/FirstUnit/registration.php. Then put the following content into it:

1
2
3
4
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',
__DIR__
);

The registration.php is a standardized file that follows the same pattern for all modules.

The only thing that varies is the module name, which in our case is Learning_FirstUnit.

 

  • Run the “setup: upgrade” command

Running this command makes your new module active, notifying Magento of its presence.

php bin/magento setup:upgrade

It should echo a large amount of output, one line of which should be Learning_FirstUnit. Verify that this line of code is there.

 

  • Check that the new module is active

So far, we haven’t added any useful code to our module – it is still empty (and therefore invisible). In order to verify that it has been recognized, check the file app/etc/config.php. It has a list of auto-generated modules that are active.

Never change this list manually!

grep Learning_FirstUnit app/etc/config.php

Employing these steps, you can successfully create a new module in Magento 2.

 

D. CO-WELL Asia – Magento e-Commerce Development

Magento module development is a core part of any Magento development or Magento project, because at any stage you may want to integrate your own functionality/module in your existing Magento project. When working with the Magento, it is possible with Magento custom modules improvement that customizes the internet e-Commerce store delivered to our customer’s requirements. This makes the shopping experience substantially more charming for the customer’s clients. We are additionally ready to enable our customer’s clients to add things to their shopping baskets just as making singular profiles. Our customers simply need to mention to us what they require for their store and we can give it.

At CO-WELL Asia, we have a team of expert Magento module developers, and exposure to, the Magento platform. They can assemble and execute the most ideal module considering your proclaimed needs and the necessities of your clients.

Find out more of our e-Commerce solutions service here