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
-
-
- 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.
- Variable Code: Enter an identifier for Variable Code, using all lowercase characters without spaces. This is a required field. For example:
-
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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
1 2 3 4 5 6 7 | $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.
{{customVar code=custom_variable}}
The variable is enclosed in curly braces and added to the code at the cursor location.
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!