How to use custom variables in Magento 2

Written by Jigar Patel

Oct 25, 2021

How to use custom variables in Magento 2

Variables are “containers” for storing information. Information that you can use multiple times. Magento create custom variables and custom variable use into pages, blocks, coding, and email templates. The list of allowed variables that appears when you click the Insert Variable button includes both predefined and custom variables.

Create custom variables in Magento 2

First add custom variable value from the Magento admin panel

  • On the Admin sidebar, go to System > Other Settings > Custom Variables.
  • Click Add New Variable

How to use custom variables in Magento 2

  • Variable Code: Enter an identifier for Variable Code, using all lowercase characters without spaces. This is a required field.   For example: custom_variable
  • Variable Name: Enter a Variable Name, which is used for internal reference. This is a required field. For example: Custom Variable
  • Variable HTML Value : Enter the variable value formatted with simple HTML tags. For example : <b>This Demo content of the variable.</b>
  • Variable Plain Value: Enter the variable value as plain text without formatting. For example : This Demo content of the variable.

Also read this: Add custom field at product form magento 2

Get Custom variable Data

Get custom variables value using Use Dependency Injection

When using this method, you need to write the following code in your Block class.

<?php

    protected $variable

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Variable\Model\Variable $variable
    )
    {
        $this->variable = $variable;
        parent::__construct($context);
    }
        
    public function getCustomVarible()
    {
        $variableData = $this->variable->loadByCode('custom_variable', 'base');
        return $variableData->getPlainValue();
    }
  • $this->variable->loadByCode(‘custom_variable’, ‘base’) : Here first parameter is custom-variable-code and second one is store-code.
  • $variableData->getPlainValue() get plain value if you can get HTML Value then use $variableData->getHtmlValue().

Get custom variable value using Object Manager

$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('custom_variable')->getHtmlValue();

$objectManager->get('Magento\Variable\Model\Variable')->setStoreId($storeID)->loadByCode('custom_variable')->getPlainValue();
  • getHtmlValue() : Return Html Value.
  • getPlainValue() : Return Plain Value.

Note:- Don’t use object manager directly in your code.Use View Model or Block to get value.

Insert the custom variable

Insert  variable into a page

  • Open the CMS page or block where the variable is to appear.
  • The Content section. Click Show / Hide Editor to work in HTML.
  • Position the insertion point in the editor where you want the variable to appear and click Insert Variable.
  • Select the option for the custom variable that you want to insert and click Insert Variable.

How to use custom variables in Magento 2

[dt_highlight color=”” text_color=”” bg_color=””]{{customVar code=custom_variable}}[/dt_highlight]

The variable is enclosed in curly braces and added to the code at the cursor location.

How to use custom variables in Magento 2

Like to read: Custom variable

I hope this instruction will be helpful for you.

If you have any queries regarding this blog, do consider them posting in the Comments section below!

I’m here to help.

Thank you!

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP