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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?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 !!