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.
custom_variable
Custom Variable
<b>This Demo content of the variable.</b>
This Demo content of the variable.
Also read this: Add custom field at product form magento 2
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(); }
$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();
Note:- Don’t use object manager directly in your code.Use View Model or Block to get value.
[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.
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!