---
title: "How to use custom variables in Magento 2"
url: "https://dolphinwebsolution.com/how-to-use-custom-variables-in-magento-2"
date: "2021-10-25T13:56:04+05:30"
modified: "2023-08-25T09:16:35+05:30"
author:
  name: "Jigar Patel"
categories:
  - "Magento"
word_count: 509
reading_time: "3 min read"
summary: "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 templa..."
description: "Variables are &#8220;containers&#8221; for storing information. Information that you can use multiple times. Magento create custom variables and custom varia..."
keywords: "custom variables in magento,Get Custom variable,Insert variable into a page,add Custom variable,create Custom variable,Magento 2 Custom variable, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Download Files in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-download-files-in-magento-2"
  - title: "Magento vs PrestaShop: Which eCommerce Platform is the Best?"
    url: "https://dolphinwebsolution.com/blog/magento-vs-prestashop/"
  - title: "Magento: How to implement custom indexing within flash time?"
    url: "https://dolphinwebsolution.com/magento-how-to-implement-custom-indexing-within-flash-time"
---

# How to use custom variables in Magento 2

_Published: October 25, 2021_  
_Author: Jigar Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2021/10/How-to-use-custom-variables-in-Magento-2-1024x536.png)

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

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/custom-varible-e1634746744360.png)

- **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](https://dolphinwebsolution.com/how-to-add-a-custom-field-at-product-form-page-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](https://dolphinwebsolution.com/content-management-system-cms-for-ecommerce) 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**.

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/custom-varible1-1-e1634891148576.png)

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

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/custom-variable2-e1634890849882.png)

Like to read: [Custom variable](https://docs.magento.com/user-guide/marketing/variables-custom.html)

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!**


---

_View the original post at: [https://dolphinwebsolution.com/how-to-use-custom-variables-in-magento-2](https://dolphinwebsolution.com/how-to-use-custom-variables-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:54:09 UTC_  
