Custom variables

Variables are a handy little feature of a core Magento_Variable module. Magento allows you to create custom variables and then use them in e-mail templates, the WYSIWYG editor, or even code expressions.

The following steps outline how we can create a new variable manually:

  1. In the Magento admin area, navigate to System | Other Settings | Custom Variables.
  2. Click on the Add New Variable button.
  3. While keeping in mind the Store View switcher, fill in the required Variable Code and Variable Name options, and preferably one of the optional options, either Variable HTML Value or Variable Plain Value.
  4. Click on the Save button.

Now that we have created the custom variable, we can use it in an e-mail template or the WYSIWYG editor by calling it using the following expression:

{{customVar code=foggyline_hello}}

The preceding expression will call for the value of the custom variable with code foggyline_hello.

Variables can be used within various code expressions, though it is not recommended to rely on the existence of an individual variable, as an admin user can delete it at any point. The following example demonstrates how we can use an existing variable in the code:

$storeId =0;

$variable = $this->_variableFactory->create()->setStoreId(
    $storeId
)->loadByCode(
    'foggyline_hello'
);

$value = $variable->getValue(
    MagentoVariableModelVariable::TYPE_HTML
);

The $this->_variableFactory is an instance of MagentoVariableModelVariableFactory.

If used in the right way, variables can be useful. Storing information such as phone numbers or specialized labels that are used in CMS pages, blogs, and e-mail templates is a nice example of using custom variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.181.52