How To

How to get Module Version Programmatically in Magento 2

Hello Magento Friends,

In our today’s blog, we will learn How to get Module Version Programmatically in Magento 2.

In Magento 2, modules are essential components that add functionality to the platform. It’s important to keep track of module versions, as updates and compatibility can affect your store’s performance.

You can display the module list in Magento 2 to know which modules are installed on your store.

Let’s find the steps to fetch the module version programmatically in Magento 2.

Steps to get Module Version Programmatically in Magento 2:

Step 1: Create a file in your Magento root directory at the below path. 

magento_root_directory\ModuleVersionCheck.php

And add the below code

<?php
use Magento\MediaStorage\Helper\File\Storage\Database;
use Magento\Framework\AppInterface;
use Magento\Framework\Component\ComponentRegistrar;

try
{
    require_once __DIR__ . '/app/bootstrap.php';
}
catch (\Exception $e)
{
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try
{
    $moduleName = 'Vendor_Extension';  /** Your extension name **/    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');
    echo getMagentoModuleVersion($moduleName);
}
catch(\Exception $e)
{
    print_r($e->getMessage());
}

function getMagentoModuleVersion(string $moduleName): string
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $componentreg = $objectManager->get('\Magento\Framework\Component\ComponentRegistrarInterface');
    $register=$objectManager->get('\Magento\Framework\Filesystem\Directory\ReadFactory');
    
    $path = $componentreg->getPath(
        ComponentRegistrar::MODULE,
        $moduleName
    );
    $directoryRead = $register->create($path);
    $composerJsonData = '';
    if ($directoryRead->isFile('composer.json')) {
        $composerJsonData = $directoryRead->readFile('composer.json');
    }
    $data = json_decode($composerJsonData);

    return !empty($data->version) ? $data->version : '';
}

Conclusion:

Being able to programmatically fetch module versions in Magento 2 is valuable for monitoring your store’s extensions and ensuring compatibility. By following the steps outlined in this blog post, you’ll be able to retrieve and display module versions with ease, helping you to maintain a well-functioning and up-to-date Magento store.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago