Hello Devs,
During development, You may need to get some store-related information programmatically.
We can fetch information such as
- Get Current Store
- Get Store Data of the current store
- Get Current Store ID
- Get another Store by specifying the store ID
- Get the Current Store Name
- Get Current Store Status
- Get a Custom page URL / Build Store-Specific URLs
- Get Current Website ID
- Get Current URL
The above information can be fetched easily in Magento using Magento’s Magento\Store\Model\StoreManagerInterface class.
Let’s see the below example for different cases :
<?php
namespace Jigar\Tutorials\Block;
use Magento\Store\Model\StoreManagerInterface;
/**
* Class for get current store related information
*/
class Example
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}
/**
* {@inheritdoc}
*/
public function execute()
{
/* Get Current Store */
$currentStore = $this->storeManager->getStore();
/* Get Store Data of current store */
$store = $this->storeManager->getStore()->getData();
/* Get Current Store ID */
$storeId = $this->storeManager->getStore()->getId();
/* Get another Store by store ID */
$anotherStore = $this->storeManager->getStore(3);
/* Get Current Store Name */
$storeName = $this->storeManager->getStore()->getName();
/* Get Current Store Status */
$isActive = $this->storeManager->getStore()->isActive();
/* Get Custom page URL */
$customUrl = $this->storeManager->getStore()->getUrl('contact');
/* Get Current Website ID */
$websiteId = $this->storeManager->getStore()->getWebsiteId();
/* Get Current URL */
$currentUrl = $this->storeManager->getStore()->getCurrentUrl();
}
}
Isn’t it easy? See related blogs like this.
Magento 2: How to get all websites list programmatically?
Magento 2: How to get all stores list programmatically
Add Yes/No Attribute in Magento 2 programmatically
Thank you !!
Leave a Comment