How to compare magento version programmatically?

How to compare magento version programmatically?

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to compare magento version programmatically?

Comparing Magento Version Programmatically

In this post, we’ll explore how to compare Magento versions programmatically within your custom code or extension. Understanding if your current version is greater than 1.6 can help you conditionally execute features that may not be supported in earlier releases.

Getting the Current Magento Version

Magento provides a built-in function Mage::getVersion() that returns the installed version number as a string:

$version = Mage::getVersion();
echo $version; // Output: 1.7.0.2

This will output something like ‘1.7.0.2’, which represents the major.minor.patch release level.

Comparing Magento Versions

To compare versions, you can use simple string comparison functions in PHP. However, to handle potential minor and patch version differences gracefully, it’s best to parse the version number into integers first:

function parseVersion($version) {
    $parts = explode('.', $version);
    return [
        'major' => (int)$parts[0],
        'minor' => (int)$parts[1],
        'patch' => (int)array_shift($parts),
    ];
}

$current = Mage::getVersion();
$comparison = parseVersion($current);

if ($comparison['major'] > 2 || ($comparison['major'] == 2 && $comparison['minor'] >= 0)) {
    // Version is 2.0 or greater
} else if ($comparison['major'] > 1) {
    // Version is 1.x but not 2.0+
} else {
    // Version is less than 1.6
}

This function breaks the version string into major, minor, and patch components, then casts them to integers for comparison.

A Better Approach: Mage::getEdition()

If you want to check if your Magento installation is using the Enterprise Edition or a specific CE version (like 2.x), consider using Mage::getEdition():

$edition = Mage::getEdition();

// Outputs "Enterprise" for Enterprise Edition, or "Community" for Community Edition

if ($edition == 'Enterprise') {
    // Enterprise Edition features available
}

This function returns a string representing the edition installed. You can use this to conditionally include enterprise-specific code without directly comparing version numbers.

Best Practices and Tips

  1. Encapsulate your version checking logic in a helper class or module to keep it centralized.
  2. Use proper naming conventions for your comparison variables (like $currentVersion instead of just $version).
  3. Always test your conditional logic thoroughly, especially with edge-case versions.
  4. If you need more fine-grained control over feature availability across specific minor versions, consider version-specific constants or configuration settings.

Conclusion

Comparing Magento versions programmatically allows you to introduce new features gradually and maintain backward compatibility. By using the provided functions like Mage::getVersion() and parsing the output as needed, you can create robust conditional logic that won’t break in earlier releases.

Magento 1 Security Extensions Themes

Our network