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>
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.

| improve this answer | |
  • How can i call them multiple with Block (<?php echo $block->??(vairaiblename); ?>)? – Efex Oct 14 at 15:55
  • You need to pass variable one by one as parameter in your block function. – Rohan Hapani Oct 15 at 4:09
  • Its not working. – Efex Oct 16 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();
| improve this answer | |
  • Could you please tell me what's the difference between my code and your code? – Rohan Hapani Oct 18 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.