Magento – magento 2 How to get store Id phtml

magento2store-id

I need to show something based on store id on Magento 2

for example;

<?php
$storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface');
if ($storeManager->getStore()->getStoreId() == 1):
?>
    <div class="block-bottom">
        <a class="button-sticky-bottom" href="<?php echo $this->getUrl('') ?>" >
            <i class="fa fa-flag" style="font-size: 18px;"></i>
            <span><?php echo __('Language'); ?></span>
        </a>
    </div>
<?php else: ?>
    <div class="block-bottom">
        <a class="button-sticky-bottom" href="<?php echo $this->getUrl('') ?>" >
            <i class="fa fa-flag" style="font-size: 18px;"></i>
            <span><?php echo __('Language1'); ?></span>
        </a>
    </div>
<?php endif; ?> 

Best Answer

block file


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

.phtml file


echo $block->getStoreId() . '<br />';
echo $block->getStoreCode() . '<br />';
echo $block->getWebsiteId() . '<br />';
echo $block->getStoreName() . '<br />';
echo $block->getStoreUrl() . '<br />';
echo $block->isStoreActive() . '<br />';

you can use this in your .phtml file like this

<?php if ($block->getStoreId() == 1): ?>
    <div class="block-bottom">
        <a class="button-sticky-bottom" href="<?php echo $this->getUrl('') ?>" >
            <i class="fa fa-flag" style="font-size: 18px;"></i>
            <span><?php echo __('Language'); ?></span>
        </a>
    </div>
<?php else: ?>
    <div class="block-bottom">
        <a class="button-sticky-bottom" href="<?php echo $this->getUrl('') ?>" >
            <i class="fa fa-flag" style="font-size: 18px;"></i>
            <span><?php echo __('Language1'); ?></span>
        </a>
    </div>
<?php endif; ?> 

Solution 2 - (I don't recommend this but as per your comment)

I need something easy

You can fetch the current store id and the store name in phtml file (using ObjectManager) like below

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeID       = $storeManager->getStore()->getStoreId(); 

you can use this $storeID according to your condition :

if ($storeID > '1')
{
    // your logic
}