Magento 2.3 – How to Get Store Information

magento2magento2.3modulePHP

Using Magento 2.3.0 with php7.1

I'm trying to use this module to get store information.

I have modified the folder names as such

app/code/Vendor/SiteInfo/Block/SiteInfo.php

And used the following code in SiteInfo.php

<?php
namespace Vendor\SiteInfo\Block;
class SiteInfo 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();
    }
}
?>

This is where I need help, it then says

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 />';

I tried using it directly in the page using

<?php echo $block->getStoreName() . '<br />';?>

that just shows

$block->getStoreName() . '
';


So how do I go about this, for example if I want to show the "Store name" in a page or a block I've created from the admin panel, how do I go about using the echo script.


Update:

I've created a file

storename.phtml

in

app/code/Vendor/Siteinfo/view/frontend/templates

With this code

<?php 
/** @var \Vendor\ModuleName\Block\Hello $block */ 
?> 
<?php 
echo $block->getStoreName(); 
?>

and used this block on a page from admin panel

{{block class="Vendor\SiteInfo\Block\SiteInfo" name="site-info"
as="site-info" template="Vendor_SiteInfo::storename.phtml" }}

flushed magento cache, and get this error

"We're sorry, an error has occurred while generating this content."

This is module I have created, that is at this path

app/code

Best Answer

There are mainly 2 issue.

  1. Your Registration.php is placed at wrong place Currently it is placed at

    Vendor/Siteinfo/etc/registration.php

it should be:

Vendor/Siteinfo/registration.php

  1. Wrong Vendor name is defined in registration.php
  2. Wrong Vendor name in module.xml

    Please follow below

    Create

    app/code/Vendor/Siteinfo/registration.php

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Siteinfo',
    __DIR__
    );
    

    app/code/Vendor/Siteinfo/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Siteinfo" setup_version="1.0.0">
    </module>
    

    app/code/Vendor/Siteinfo/Block/Siteinfo.php

    <?php
    namespace Vendor\Siteinfo\Block;
    class Siteinfo extends \Magento\Framework\View\Element\Template
    {    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        array $data = []
    )
    {            
        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();
    }
    }
    ?>
    

    app/code/Vendor/Siteinfo/view/frontend/templates/storename.phtml

    <?php echo $block->getStoreName(); ?>
    

    After that you need to run:

    php bin/magento setup:upgrade

    php bin/magento cache:flush

Finally call:

{{block class="Vendor\Siteinfo\Block\Siteinfo" name="site-info" as="site-info" template="Vendor_Siteinfo::storename.phtml" }}

You can download module at GitHub

Related Topic