7

Does anyone know the best way a module can check if it is being executed on Magento 2 Enterprise Edition and which exact version number is being used?

9

You have to inject \Magento\Framework\App\ProductMetadataInterface into your constructor.

protected $productMetadata;

public function __construct(
    ...
    \Magento\Framework\App\ProductMetadataInterface $productMetadata,
    ...
) {
    $this->productMetadata = $productMetadata;
    parent::__construct(...);
}

Then you can get current Magento version by (assuming that ProductMetadataInterface object is assigned to $productMetadata field):

$version = $this->productMetadata->getVersion();

And edition (Community/Enterprise) by:

$edition = $this->productMetadata->getEdition();

2

Method 1:

Use Magento standard way to get version of your site, Using Block - Template way is proper way to do call any function in magento 2,

Inside Block file,

<?php
namespace Vendor\Modulename\Block
class Version extends \Magento\Framework\View\Element\Template{

protected $_productMetadata;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\ProductMetadataInterface $productMetadata,
        array $data = []
    ) {
        parent::__construct($context,$data);
        $this->_productMetadata = $productMetadata;
    }

    public function getVersion()
    {
        return $this->_productMetadata->getVersion();
    }
}

inside template file,

echo $block->getVersion();

Using Direct Objectmanager is not proper way to use in magento 2,

  $objManager = \Magento\Framework\App\ObjectManager::getInstance();
  $magentoVersion = $objManager->get('Magento\Framework\App\ProductMetadataInterface');
  echo $magentoVersion->getVersion();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.