7

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; ?> 
2
  • Whats is the issue you are getting? Commented Oct 24, 2018 at 15:30
  • Hello @Gkna, if any answer solves your concern then mark as right, which will help future readers. Commented Oct 26, 2018 at 9:19

1 Answer 1

6

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()->getId(); 

you can use this $storeID according to your condition :

if ($storeID > '1')
{
    // your logic
}
8
  • I used it but it prints wrong variable. When i open store en (1) it shows "Language1" I am trying to add the code in a header.phtml file, i dont have a block file. I think something is missing
    – Gkna
    Commented Oct 24, 2018 at 15:53
  • create block file Commented Oct 24, 2018 at 15:59
  • In your module like this Commented Oct 24, 2018 at 15:59
  • your Vendor name\your Module name\Block; Commented Oct 24, 2018 at 16:00
  • Block file is compulsory Commented Oct 24, 2018 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.