Magento 2 Custom Module Development

Custom Module Development in Magento 2

Magento development services is one of the most powerful and widely used e-commerce CMS. Since it's specifically built for e-commerce, it includes almost all the important features that are required to manage your online store. Magento 2 comes with a wide range of features, but if those features don't seem to fulfill all of your requirements, then you can always add more custom features to it.

In this article, we will be exploring this idea of adding/modifying the features of Magento 2 using Custom Module Development.

I will try to explain the Magento 2 Custom Module development as detailed as possible.

Let's first understand what the module will do. We are going to develop a custom module that will round off the total cart price, i.e if a user has added 125.5 USD worth of products in the cart, then it would get rounded off to 126 USD.

Given below is the amount that we will be rounding off.

Magento 2 Custom Module Development

In order to build this module, let’s first understand the folder structure for building a custom module

App -> Code -> Vendor Name -> Module Name

Magento 2 Custom Module Development

Here, the vendor is Sj Innovation and Total is the Module name. Please ignore the other files and folders for now. We will be exploring them in detail as we progress.

In order to create and register a custom module, we have to create 2 files

  1. registration.php ( inside Modules root folder )
  2. module.xml ( inside the etc folder )

1. Create a file named registration.php in the modules root directory

Magento 2 Custom Module Development

Now open the file and copy given below code 







Here Sjinnovation is the name of the vendor and Total is the name of the module

2. Now create a file named module.xml in etc folder

Magento 2 Custom Module Development

Copy the given content below in the module.xml file

<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

<module name="Sjinnovation_Total" setup_version="2.2.0">



Here "Sjinnovation_Total" is the name of the custom module

Now you have to run the command given below.

php bin/magento module:status

Magento 2 Custom Module Development

As soon as you run this command, you will be able to see your custom module getting listed in Magento. This means that our module is registered, but if you notice that it's still coming on the disabled list, that means that our module is registered, but not yet enabled in the system. We will be enabling it soon.

But first, let's move onto the next step, i.e to write the logic which will round off the total cart amount.

In order to achieve this, we have to override Vendor\Magento\Quote\Model\Quote\Address\Total\Grand.php which calculates the Cart’s total amount.

Please note that this is the core file of Magento 2, and we will be overriding it in our custom module.

Create a file named di.xml in etc folder

Copy the content given below in di.xml

<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Magento\Quote\Model\Quote\Address\Total\Grand" type="Sjinnovation\Total\Model\Quote\Address\Total\Grand" />


In this file, I have specified 2 things in preference

  1. Which file should be overridden ( for="Magento\Quote\Model\Quote\Address\Total\Grand" )
  2. The file which will be overriding it (  type="Sjinnovation\Total\Model\Quote\Address\Total\Grand" )

Now, since we are overriding a model, we have to create a folder named "Model" and then follow the folder structure in the core module

Magento 2 Custom Module Development

Now create a file named Grand.php ( Refer fig for the location of the file )

Magento 2 Custom Module Development

Copy the content given below in Grand.php


namespace Sjinnovation\Total\Model\Quote\Address\Total;

class Grand extends \Magento\Quote\Model\Quote\Address\Total\Grand


public function collect(

     \Magento\Quote\Model\Quote $quote,

     \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,

     \Magento\Quote\Model\Quote\Address\Total $total

) {

     $grandTotal = $total->getGrandTotal();

     $baseGrandTotal = $total->getBaseGrandTotal();

     $totals = array_sum($total->getAllTotalAmounts());

     $baseTotals = array_sum($total->getAllBaseTotalAmounts());

     /* code edited here */

     $totalCartAmount = round($grandTotal + $totals);


     /* code edit end */

     $total->setBaseGrandTotal($baseGrandTotal + $baseTotals);

     return $this;



Here in the namespace, the proper folder structure should be followed. Also, if you notice, I have extended my "Grand" class with Magento\Quote\Model\Quote\Address\Total\Grand. This is to ensure that I inherit all the methods and variables of the core Grand class.

If you don't do that, then some of the features of the core module will not work, since other methods will not be present in the newly overridden class.

Now, I have just rounded off the cart value and then assigned it as the grand total.

With that our Custom module development is done. Now we have to enable it.

For that, run the commands given below in exact sequence

php bin/magento module:enable Sjinnovation_Total

This command will enable your custom module.

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

php bin/magento cache:flush

Now check your Cart Total

And with that, you have successfully created and integrated a custom module in Magento development services. Here is the link to my Custom modules git repository :


Happy Coding.


Request a Quote now to get a complete range of Magento Development services.

Akshay Naik
Akshay Naik
Emotional Intelligence in Project Management

Emotional Intelligence in Project Management

Arif Islam
List of 8 Qualities Required to Become a Good Software Tester

Qualities to Become a Good Software Tester

Praveen Devarkar
Case Study: Eyedefmedia

Case Study: Eyedefmedia