The scenario is I want to set my variable to a global variable from a phtml file and call in a module file or block file.
So what would be some code or tutorial link that I set my variable to global and call in a module or class file?
The scenario is I want to set my variable to a global variable from a phtml file and call in a module file or block file.
So what would be some code or tutorial link that I set my variable to global and call in a module or class file?
You can use the code as below for setting up the session below code explains how to set a session in the customer session.
protected $customerSession;
public function __construct(
...
\Magento\Customer\Model\Session $customerSession,
...
){
...
$this->customerSession = $customerSession;
...
}
// later in your class you can use it as below
$this->customerSession->myName('anyname');
Few other sessions in Magento 2:
Links to tutorials:
https://www.mageplaza.com/how-set-unset-session-magento-2.html http://blog.chapagain.com.np/magento-2-set-unset-get-session/
Create your custom variable using System -> Custom Variables
.Using this you can add your global variable and use is anywhere in magento.
To set and get global variable in Magento 2 to follow below steps
1) create the variable in admin side go to System -> Other Settings -> Custom Variables
2) add Variable Code, Variable Name, Variable HTML Value(if you want any html value into phtml file) and Variable Plain Value(if you want any plain value into phtml file)
3) get created variable value into phtml file or anywhere in your code
protected $objectManager;
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
){
$this->_objectManager = $objectManager;
}
public function execute()
{
$model = $this->_objectManager->get('Magento\Variable\Model\Variable')->loadByCode('test_var');//your Variable Code
$plain_value = $model->getPlainValue();
$html_value = $model->getHtmlValue();
$name = $model->getName();
}