This blog post will help you to retrieve information about your Magento 2 store including store ID, store name, store URL, and store website using 2 different methods
Method 1: Using Block
Step -1: Create a block file in your custom module app/code/Webiators/CustomChanges/Block/StoreInfo.php
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<?php namespace Webiators\CustomChanges\Block; class StoreInfo extends \Magento\Framework\View\Element\Template { protected $_storeManager; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, array $data = [] ) { $this->_storeManager = $storeManager; parent::__construct($context, $data); } /** * Get store identifier * * @return int */ public function getStoreId() { return $this->_storeManager->getStore()->getId(); } /** * Get website identifier * * @return string|int|null */ public function getWebsiteId() { return $this->_storeManager->getStore()->getWebsiteId(); } /** * Get Store code * * @return string */ public function getStoreCode() { return $this->_storeManager->getStore()->getCode(); } /** * Get Store name * * @return string */ public function getStoreName() { return $this->_storeManager->getStore()->getName(); } /** * Get current url for store * * @param bool|string $fromStore Include/Exclude from_store parameter from URL * @return string */ public function getStoreUrl($fromStore = true) { return $this->_storeManager->getStore()->getCurrentUrl($fromStore); } /** * Check if store is active * * @return boolean */ public function isStoreActive() { return $this->_storeManager->getStore()->isActive(); } } ?> |
Step -2: Create a phtml file in your custom module app/code/Webiators/CustomChanges/view/frontend/templates/storeinfo.phtml
1 2 3 4 5 6 7 8 9 |
<?php echo $block->getStoreId() . '<br />'; echo $block->getWebsiteId() . '<br />'; echo $block->getStoreName() . '<br />'; echo $block->getStoreUrl() . '<br />'; echo $block->getStoreCode() . '<br />'; echo $block->isStoreActive() . '<br />'; ?> |
Method 2: Using Object Manager
Step -1: Create phtml file in your custom module app/code/Webiators/CustomChanges/view/frontend/templates/storeinfo.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); echo $storeManager->getStore()->getId(); echo $storeManager->getStore()->getWebsiteId(); echo $storeManager->getStore()->getName(); echo $storeManager->getStore()->getCurrentUrl(); echo $storeManager->getStore()->getCode(); echo $storeManager->getStore()->isActive(); ?> |
Thank You!
Hit 5 Stars if you find this post helpful
0 (based on 0 Reviews)