Magento2 – How to get store information

Magento2 – How to get store information
By default store manager loads current store information. You can get store information by `store_id` parameter. You must create an instance of \Magento\Store\Model\StoreManagerInterface class to get store information.
<?php
...

    private $storeManager;

	public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager) {
		$this->storeManager = $storeManager;
	}

	/**
	 * Returns Current Store id.
	 *
	 * @return int
	 */
	public function getStoreId()
	{
		return $this->getStore()->getId();
	}

	/**
	 * Returns Current Store Code.
	 *
	 * @return string
	 */
	public function getStoreCode()
	{
		return $this->getStore()->getCode();
	}

	/**
	 * Returns Current Store Name.
	 *
	 * @return string
	 */
	public function getStoreName()
	{
		return $this->getStore()->getName();
	}

	/**
	 * Returns Store Data Model Interface.
	 *
	 * @return \Magento\Store\Api\Data\StoreInterface
	 */
	public function getStore()
	{
		return $this->storeManager->getStore();
	}

	/**
	 * Loads Store information by store id parameter.
	 *
	 * @param int $storeId
	 * @return \Magento\Store\Api\Data\StoreInterface
	 */
	public function getStoreById($storeId)
	{
		return $this->storeManager->getStore($storeId);
	}

...

Leave a Reply

Your email address will not be published. Required fields are marked *