skip to Main Content

I develop Magento extension for both Magento1.x and Magento2.x.

I want to provide a source code for all version(Magento1.x, Magento2.x).

I need to check Magento version in first part.

How to check?

function getVersion() {
……
}

if(getVersion() == “2.0”){
}

if(getVersion() == “1.x”) {
}

if(getVersion() == “2.2”){
}

I need script for getVersion function.

3

Answers


  1. In Magento 1.x go to the root folder of your magento installation and type the following

    echo "Version: $(php -r "require 'app/Mage.php'; echo Mage::getVersion();")"
    

    This will output something like this:

    Version: 1.9.2.3
    

    In magento 2.x go to the root folder of your magento installation and type:

    php bin/magento --version
    

    This will output something like this:

    Magento CLI version 2.2.6
    
    Login or Signup to reply.
  2. Magento 1

    In Magento 1, you can simply find the version by this:

    Mage::getVersion();
    

    Magento 2.0

    Up to Magento 2.0.7, you can get the version from AppInterface, which is a reference to the MagentoFrameworkAppInterface::VERSION constant.

    echo MagentoFrameworkAppInterface::VERSION;
    

    Magento 2.1

    But, after the release of Magento 2.1, you have two options to get Magento version programmatically.

    First option is dependency injection (DI), by injecting MagentoFrameworkAppProductMetadataInterface into your constructor to retrieve the version, something like this:

    protected $productMetadata;
    
    public function __construct (
        ...
        MagentoFrameworkAppProductMetadataInterface $productMetadata,
        ...
    ) {
        $this->productMetadata = $productMetadata;
        parent::__construct(...);
    }
    
    // Retrieve Magento 2 version
    public function getMagentoVersion()
    {
        return $this->productMetadata->getVersion();
    }
    

    Another option is ObjectManager which is not recommended by Magento

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $productMetadata = $objectManager->get('MagentoFrameworkAppProductMetadataInterface');
    echo $productMetadata->getVersion();
    

    N.b. well, if you use MagentoFrameworkAppProductMetadata::getVersion() function, then whether you are on 2.0.x or 2.1.x, you will get the correct version.

    Login or Signup to reply.
  3. In version 2 Magento support created a URL to help them determine the version of your store: example.com/magento_version.

    Version 1 of Magento includes a Magento Connect Manager at the following URL: example.com/downloader.
    In the footer of this page the Magento Connect Manager version shown, and we know this to be the same version as the Magento install.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search