Magento

How to use custom variables in Magento 2

Written by Jigar Patel

Oct 25

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

custom varible e1634746744360

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

custom varible1 1 e1634891148576

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

custom variable2 e1634890849882

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!

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP