skip to Main Content

magento 2 get store id php

I need to show something based on store id

for exapmle;

					<?php if ($store == "2") {?>
<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 } ?>		

4

Answers


  1. Chosen as BEST ANSWER

    It didnt worked. What is wrong?

    <?php
    $storeManager = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoStoreModelStoreManagerInterface');
    
    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 }
    ?>		
    		


  2. $storeManager = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoStoreModelStoreManagerInterface');
    
    if ($storeManager->getStore()->getStoreId() == 1) {
        echo 'Hello';
    } else {
        echo 'Thanks';
    }
    

    The use of object manager is not recommended. It is better to arrange this code for the class of the block using the constructor

    Login or Signup to reply.
  3. <?php
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $storeManager = $objectManager->create('MagentoStoreModelStoreManagerInterface');
    $storeId = $storeManager->getStore()->getId();
    if($storeId == 1){
        echo 'Store Id 1 code here';
    }else{
        echo 'other stores code here';
    }
    ?>
    
    Login or Signup to reply.
  4. I would recommend injecting the StoreManagerInterface in your construct and avoid using the object manager.

    Example

    /**
     * @var MagentoStoreModelStoreManagerInterface
     */
    protected $storeManager;
    
    
    public function __construct(Context $context,
                                MagentoStoreModelStoreManagerInterface $storeManager)
    {
        parent::__construct($context);
        $this->storeManager = $storeManager;
    
    }
    
    public function myFunction()
    {
        $storeId = $this->storeManager->getStore()->getId();
    
        if ($storeId == 1) {
            echo 'Store Id 1 code here';
        } else {
            echo 'other stores code here';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search