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. Commented Oct 14, 2020 at 4:38

3 Answers 3

3

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
    Commented Oct 14, 2020 at 15:55
  • You need to pass variable one by one as parameter in your block function. Commented Oct 15, 2020 at 4:09
  • Its not working.
    – Efex
    Commented Oct 16, 2020 at 17:42
0
<?php
/* I used custom variable on checkout success page */
namespace Vendor\Checkout\ViewModel;

use Magento\Variable\Model\Variable as CustomVariable;


class Success implements \Magento\Framework\View\Element\Block\ArgumentInterface
{

    /**
     * @var CustomVariable
     */
    protected CustomVariable $customVariable;

    /**
     * Your constructor.
     *
     * @param CustomVariable $customVariable
     */
    public function __construct(
        CustomVariable $customVariable
    ) {
        $this->customVariable = $customVariable;
    }

    /**
     * @return string
     */
    public function getMyCoolUrl(): string
    {
        $url = $this->customVariable->loadByCode('your_code')->getValue();
        /* default is html - see vendor/magento/module-variable/Model/Variable.php */
        return !empty($url) ? $url : 'some_default_value';
    }
}

In template:
/** @var $viewModel Vendor\Checkout\ViewModel\Success */
<a href="<?= $viewModel->getMyCoolUrl(); ?>" class="foo">my link</a>
-1

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? Commented Oct 18, 2020 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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