Question

I have created custom varibles in "System -> Other Settings -> Custom Variables" for different store views. How can i call them in phtml files for store views?

I am trying following code but its not working

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->get('Magento\Variable\Model\Variable')->loadByCode('storenumber');
$plain_value = $model->getPlainValue();

?>

<div class="h3"><?php echo $plain_value ?></div>
Was it helpful?

Solution 2

Following worked for me.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storenumber= $objectManager->get('Magento\Variable\Model\Variable')->setStoreId($storeID)->loadByCode('storenumber');
$storenumber_html = $storenumber->getHtmlValue();
$storenumber_plain = $storenumber->getPlainValue();

OTHER TIPS

You can use this below way :

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

$objectManager->get('Magento\Variable\Model\Variable')->setStoreId($storeID)->loadByCode('your_custom_variable')->getHtmlValue(); // Return Html Value

$objectManager->get('Magento\Variable\Model\Variable')->setStoreId($storeID)->loadByCode('your_custom_variable')->getPlainValue(); // Return Plain Value

Note : Don't use object manager directly. Use View Model or Block to get value.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top