Magento 2 - Kiểm tra phiên bản Magento theo chương trình (CE / EE)


Câu trả lời:


9

Bạn phải tiêm \Magento\Framework\App\ProductMetadataInterfacevào nhà xây dựng của bạn.

protected $productMetadata;

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

Sau đó, bạn có thể nhận phiên bản Magento hiện tại bằng cách (giả sử ProductMetadataInterfaceđối tượng đó được gán cho $productMetadatatrường):

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

Và phiên bản (Cộng đồng / Doanh nghiệp) bởi:

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


2

Cách 1:

Sử dụng cách tiêu chuẩn Magento để nhận phiên bản trang web của bạn, Sử dụng cách chặn - Mẫu là cách thích hợp để thực hiện bất kỳ chức năng nào trong magento 2,

Tập tin khối bên trong,

<?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();
    }
}

bên trong templatetập tin,

echo $block->getVersion();

Sử dụng Direct Objectmanager không phải là cách thích hợp để sử dụng trong magento 2,

  $objManager = \Magento\Framework\App\ObjectManager::getInstance();
  $magentoVersion = $objManager->get('Magento\Framework\App\ProductMetadataInterface');
  echo $magentoVersion->getVersion();
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.