Hello everyone.
There are several situations where we need to programmatically know the Version and Edition of Magento 2. You can use the below code to get both the version and edition (Community or Enterprise) of Magento.
First of all, make an instance of the class \Magento\Framework\App\ProductMetadataInterface in the construct as below
public function __construct( ... \Magento\Framework\App\ProductMetadataInterface $productMetadata, ... ) { ... $this->_productMetadata = $productMetadata; ... }
After that, you can use the following codes to print the version and edition of Magento-
public function getMagentoInformation() { echo "Magento Version => ".$this->_productMetadata->getVersion(); echo "Magento Edition => ".$this->_productMetadata->getEdition(); }
Above lines of code will print the Magento 2 Version and Magento 2 Edition as you can see in the below screenshot-
I have Used this snippet of code in Controller, but you can use this in Block or Helper files as per your need.
This is all for now. Hope it will help. 🙂
Be the first to comment.