How can i set global variable in Magento 2?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












The scenario is I want to set my variable to global variable from phtml file and call in module file or block file.



So any code or tutorial link that i set my variable to global and call in module or class file.










share|improve this question























  • check this link
    – Hit's
    2 hours ago
















up vote
1
down vote

favorite












The scenario is I want to set my variable to global variable from phtml file and call in module file or block file.



So any code or tutorial link that i set my variable to global and call in module or class file.










share|improve this question























  • check this link
    – Hit's
    2 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











The scenario is I want to set my variable to global variable from phtml file and call in module file or block file.



So any code or tutorial link that i set my variable to global and call in module or class file.










share|improve this question















The scenario is I want to set my variable to global variable from phtml file and call in module file or block file.



So any code or tutorial link that i set my variable to global and call in module or class file.







magento2 session global-variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 mins ago









Sourabh Kumar Sharma

423214




423214










asked 2 hours ago









jhon jhon doe

1008




1008











  • check this link
    – Hit's
    2 hours ago
















  • check this link
    – Hit's
    2 hours ago















check this link
– Hit's
2 hours ago




check this link
– Hit's
2 hours ago










3 Answers
3






active

oldest

votes

















up vote
1
down vote













Create your custom variable using System -> Custom Variables .Using this you can add your global variable and use is anywhere in magento.






share|improve this answer




















  • I want to set a variable in session and call on other page
    – jhon jhon doe
    49 mins ago










  • Hi Jhon please review this magento.stackexchange.com/questions/94265/…
    – Shorabh
    46 mins ago

















up vote
1
down vote













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(
...
MagentoCustomerModelSession $customerSession,
...
)
...
$this->customerSession = $customerSession;
...


// later in your class you can use it as below

$this->customerSession->myName('anyname');


Few other sessions in Magento 2:



  • MagentoBackendModelSession

  • MagentoCatalogModelSession

  • MagentoCheckoutModelSession

  • MagentoCustomerModelSession

  • MagentoNewsletterModelSession

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/






share|improve this answer



























    up vote
    0
    down vote













    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(
    MagentoFrameworkObjectManagerInterface $objectManager
    )
    $this->_objectManager = $objectManager;


    public function execute()

    $model = $this->_objectManager->get('MagentoVariableModelVariable')->loadByCode('test_var');//your Variable Code
    $plain_value = $model->getPlainValue();
    $html_value = $model->getHtmlValue();
    $name = $model->getName();



    you can also set like a session variable and this one






    share|improve this answer










    New contributor




    Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
      – jhon jhon doe
      43 mins ago










    • in your case, you should use session variable try this link as already mentioned in above answer
      – Hit's
      41 mins ago










    • yeah i use this links pls can you helo me that how can i echo that value in get message ?
      – jhon jhon doe
      40 mins ago










    • check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
      – Hit's
      35 mins ago










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f245162%2fhow-can-i-set-global-variable-in-magento-2%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Create your custom variable using System -> Custom Variables .Using this you can add your global variable and use is anywhere in magento.






    share|improve this answer




















    • I want to set a variable in session and call on other page
      – jhon jhon doe
      49 mins ago










    • Hi Jhon please review this magento.stackexchange.com/questions/94265/…
      – Shorabh
      46 mins ago














    up vote
    1
    down vote













    Create your custom variable using System -> Custom Variables .Using this you can add your global variable and use is anywhere in magento.






    share|improve this answer




















    • I want to set a variable in session and call on other page
      – jhon jhon doe
      49 mins ago










    • Hi Jhon please review this magento.stackexchange.com/questions/94265/…
      – Shorabh
      46 mins ago












    up vote
    1
    down vote










    up vote
    1
    down vote









    Create your custom variable using System -> Custom Variables .Using this you can add your global variable and use is anywhere in magento.






    share|improve this answer












    Create your custom variable using System -> Custom Variables .Using this you can add your global variable and use is anywhere in magento.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 1 hour ago









    Shorabh

    457214




    457214











    • I want to set a variable in session and call on other page
      – jhon jhon doe
      49 mins ago










    • Hi Jhon please review this magento.stackexchange.com/questions/94265/…
      – Shorabh
      46 mins ago
















    • I want to set a variable in session and call on other page
      – jhon jhon doe
      49 mins ago










    • Hi Jhon please review this magento.stackexchange.com/questions/94265/…
      – Shorabh
      46 mins ago















    I want to set a variable in session and call on other page
    – jhon jhon doe
    49 mins ago




    I want to set a variable in session and call on other page
    – jhon jhon doe
    49 mins ago












    Hi Jhon please review this magento.stackexchange.com/questions/94265/…
    – Shorabh
    46 mins ago




    Hi Jhon please review this magento.stackexchange.com/questions/94265/…
    – Shorabh
    46 mins ago












    up vote
    1
    down vote













    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(
    ...
    MagentoCustomerModelSession $customerSession,
    ...
    )
    ...
    $this->customerSession = $customerSession;
    ...


    // later in your class you can use it as below

    $this->customerSession->myName('anyname');


    Few other sessions in Magento 2:



    • MagentoBackendModelSession

    • MagentoCatalogModelSession

    • MagentoCheckoutModelSession

    • MagentoCustomerModelSession

    • MagentoNewsletterModelSession

    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/






    share|improve this answer
























      up vote
      1
      down vote













      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(
      ...
      MagentoCustomerModelSession $customerSession,
      ...
      )
      ...
      $this->customerSession = $customerSession;
      ...


      // later in your class you can use it as below

      $this->customerSession->myName('anyname');


      Few other sessions in Magento 2:



      • MagentoBackendModelSession

      • MagentoCatalogModelSession

      • MagentoCheckoutModelSession

      • MagentoCustomerModelSession

      • MagentoNewsletterModelSession

      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/






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        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(
        ...
        MagentoCustomerModelSession $customerSession,
        ...
        )
        ...
        $this->customerSession = $customerSession;
        ...


        // later in your class you can use it as below

        $this->customerSession->myName('anyname');


        Few other sessions in Magento 2:



        • MagentoBackendModelSession

        • MagentoCatalogModelSession

        • MagentoCheckoutModelSession

        • MagentoCustomerModelSession

        • MagentoNewsletterModelSession

        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/






        share|improve this answer












        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(
        ...
        MagentoCustomerModelSession $customerSession,
        ...
        )
        ...
        $this->customerSession = $customerSession;
        ...


        // later in your class you can use it as below

        $this->customerSession->myName('anyname');


        Few other sessions in Magento 2:



        • MagentoBackendModelSession

        • MagentoCatalogModelSession

        • MagentoCheckoutModelSession

        • MagentoCustomerModelSession

        • MagentoNewsletterModelSession

        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/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 19 mins ago









        Sourabh Kumar Sharma

        423214




        423214




















            up vote
            0
            down vote













            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(
            MagentoFrameworkObjectManagerInterface $objectManager
            )
            $this->_objectManager = $objectManager;


            public function execute()

            $model = $this->_objectManager->get('MagentoVariableModelVariable')->loadByCode('test_var');//your Variable Code
            $plain_value = $model->getPlainValue();
            $html_value = $model->getHtmlValue();
            $name = $model->getName();



            you can also set like a session variable and this one






            share|improve this answer










            New contributor




            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
              – jhon jhon doe
              43 mins ago










            • in your case, you should use session variable try this link as already mentioned in above answer
              – Hit's
              41 mins ago










            • yeah i use this links pls can you helo me that how can i echo that value in get message ?
              – jhon jhon doe
              40 mins ago










            • check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
              – Hit's
              35 mins ago














            up vote
            0
            down vote













            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(
            MagentoFrameworkObjectManagerInterface $objectManager
            )
            $this->_objectManager = $objectManager;


            public function execute()

            $model = $this->_objectManager->get('MagentoVariableModelVariable')->loadByCode('test_var');//your Variable Code
            $plain_value = $model->getPlainValue();
            $html_value = $model->getHtmlValue();
            $name = $model->getName();



            you can also set like a session variable and this one






            share|improve this answer










            New contributor




            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
              – jhon jhon doe
              43 mins ago










            • in your case, you should use session variable try this link as already mentioned in above answer
              – Hit's
              41 mins ago










            • yeah i use this links pls can you helo me that how can i echo that value in get message ?
              – jhon jhon doe
              40 mins ago










            • check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
              – Hit's
              35 mins ago












            up vote
            0
            down vote










            up vote
            0
            down vote









            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(
            MagentoFrameworkObjectManagerInterface $objectManager
            )
            $this->_objectManager = $objectManager;


            public function execute()

            $model = $this->_objectManager->get('MagentoVariableModelVariable')->loadByCode('test_var');//your Variable Code
            $plain_value = $model->getPlainValue();
            $html_value = $model->getHtmlValue();
            $name = $model->getName();



            you can also set like a session variable and this one






            share|improve this answer










            New contributor




            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            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(
            MagentoFrameworkObjectManagerInterface $objectManager
            )
            $this->_objectManager = $objectManager;


            public function execute()

            $model = $this->_objectManager->get('MagentoVariableModelVariable')->loadByCode('test_var');//your Variable Code
            $plain_value = $model->getPlainValue();
            $html_value = $model->getHtmlValue();
            $name = $model->getName();



            you can also set like a session variable and this one







            share|improve this answer










            New contributor




            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited 31 mins ago





















            New contributor




            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 1 hour ago









            Hit's

            1536




            1536




            New contributor




            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Hit's is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.











            • how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
              – jhon jhon doe
              43 mins ago










            • in your case, you should use session variable try this link as already mentioned in above answer
              – Hit's
              41 mins ago










            • yeah i use this links pls can you helo me that how can i echo that value in get message ?
              – jhon jhon doe
              40 mins ago










            • check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
              – Hit's
              35 mins ago
















            • how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
              – jhon jhon doe
              43 mins ago










            • in your case, you should use session variable try this link as already mentioned in above answer
              – Hit's
              41 mins ago










            • yeah i use this links pls can you helo me that how can i echo that value in get message ?
              – jhon jhon doe
              40 mins ago










            • check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
              – Hit's
              35 mins ago















            how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
            – jhon jhon doe
            43 mins ago




            how can i pass my own variable in it for example i save calue in variable and i want to pass that value on variable
            – jhon jhon doe
            43 mins ago












            in your case, you should use session variable try this link as already mentioned in above answer
            – Hit's
            41 mins ago




            in your case, you should use session variable try this link as already mentioned in above answer
            – Hit's
            41 mins ago












            yeah i use this links pls can you helo me that how can i echo that value in get message ?
            – jhon jhon doe
            40 mins ago




            yeah i use this links pls can you helo me that how can i echo that value in get message ?
            – jhon jhon doe
            40 mins ago












            check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
            – Hit's
            35 mins ago




            check here this is nice answer magento.stackexchange.com/a/209343 just replace setMessage or getMessage with your variable like setCalue and getCalue
            – Hit's
            35 mins ago

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f245162%2fhow-can-i-set-global-variable-in-magento-2%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery