0

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>
1
  • Please add system.xml file code in question. – Hamendra Sunthwal Oct 14 '20 at 4:38
2

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.

3
  • How can i call them multiple with Block (<?php echo $block->??(vairaiblename); ?>)? – Efex Oct 14 '20 at 15:55
  • You need to pass variable one by one as parameter in your block function. – Rohan Hapani Oct 15 '20 at 4:09
  • Its not working. – Efex Oct 16 '20 at 17:42
0

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();
1
  • Could you please tell me what's the difference between my code and your code? – Rohan Hapani Oct 18 '20 at 7:42

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.