0

I want to get the BaseURL with the StoreViewCode via Magento 2 Variables but with no success. I have the "add Storeview to URL" enabled at the Backend and using the following variable only shows the Base URL without it.

{{config path="web/unsecure/base_url"}}

Is there a variable to get the StoreviewCode as well?

  • where do you want to get url with store view code ? in cms page / blocks ? – Naveed Asim Oct 6 '18 at 12:56
  • exactly on CMS pages via Blocks – Marcel H. Oct 6 '18 at 13:01
1

use following variable to get url with store code :

{{store direct_url=""}}

Also this will work :

{{store url=''}}

| improve this answer | |
  • Perfect, that worked like a charm :) Thank you – Marcel H. Oct 6 '18 at 13:22
0

app/code/Vendor/Module/Block/HelloWorld.php

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

Get store information in .phtml file

echo $block->getStoreId();
echo $block->getStoreCode();
echo $block->getWebsiteId();
echo $block->getStoreName();
echo $block->getStoreUrl();
echo $block->isStoreActive();

In CMS

<a href="{{store url=''}}">text</a>
| improve this answer | |
  • Thank your, but i want to use it with the standard Magento 2 Blocks so i cant use php function or am i wrong ? – Marcel H. Oct 6 '18 at 13:05

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.