Get store information (Store ID, Code, Name) in Magento 2
Vinh Jacker | 12-18-2024
Before launching an online store, it is essential to get all information of your magento 2 store including current store ID, store code, store name, store url, store phone number, store email address and store website. This information can be used in different places such as your invoices, shipments, credit memos, or in communications between you and your customers. Providing detailed information also helps build trust and increase conversion rates. Moreover, fetching these data will make you easy to manage when you have more than one Magento 2 store in your arms, or in case of any updates in the future. In this tutorial article, the code snippets are given to allow inserting into the console admin.
Overview of getting store information in Magento 2
Step 1: Declare in Mageplaza_HelloWorld
You will use a block class of the module Mageplaza_HelloWorld
, then possibly inject the object of StoreManagerInterface
in the constructor of the module’s block class.
app/code/Chapagain/HelloWorld/Block/HelloWorld.php
<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld 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: Get store information in .phtml
file
Using the following script in the .phtml
file and print the store information.
echo $block->getStoreId() . '<br />';
echo $block->getStoreCode() . '<br />';
echo $block->getWebsiteId() . '<br />';
echo $block->getStoreName() . '<br />';
echo $block->getStoreUrl() . '<br />';
echo $block->isStoreActive() . '<br />';
Now you have got all the store information you need. I hope this guide is helpful for your business management and conversion boost. If you have any queries about the article or any questions in general, don’t hesitate to use the comment section below! Also, check these related topics for your next steps:
Related Topics