4

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

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
}
  • 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 Oct 24 '18 at 15:53
  • create block file – Aditya Shah Oct 24 '18 at 15:59
  • In your module like this – Aditya Shah Oct 24 '18 at 15:59
  • your Vendor name\your Module name\Block; – Aditya Shah Oct 24 '18 at 16:00
  • Block file is compulsory – Aditya Shah Oct 24 '18 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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