How to set and get customer session data in magento 2 Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to call a model method from controller in Magento2getting error in while running Cutom admin controller URL in magento 2Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 2: How to override newsletter Subscriber modelMagento 2 Add new field to Magento_User admin formMagento offline custom Payment method with drop down listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?

Fundamental Solution of the Pell Equation

8 Prisoners wearing hats

Around usage results

How to show element name in portuguese using elements package?

Trademark violation for app?

What would be the ideal power source for a cybernetic eye?

Did MS DOS itself ever use blinking text?

How do I stop a creek from eroding my steep embankment?

How to compare two different files line by line in unix?

Can an alien society believe that their star system is the universe?

Is it common practice to audition new musicians 1-2-1 before rehearsing with the entire band?

Find the length x such that the two distances in the triangle are the same

Should I use a zero-interest credit card for a large one-time purchase?

What is homebrew?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

How to down pick a chord with skipped strings?

Is safe to use va_start macro with this as parameter?

An adverb for when you're not exaggerating

First console to have temporary backward compatibility

What is the escape velocity of a neutron particle (not neutron star)

Why are both D and D# fitting into my E minor key?

Closed form of recurrent arithmetic series summation

Compare a given version number in the form major.minor.build.patch and see if one is less than the other

Maximum summed powersets with non-adjacent items



How to set and get customer session data in magento 2



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to call a model method from controller in Magento2getting error in while running Cutom admin controller URL in magento 2Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 2: How to override newsletter Subscriber modelMagento 2 Add new field to Magento_User admin formMagento offline custom Payment method with drop down listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?



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








10















I am struggling with magento 2 session. I have created below controller file as a sample code.



<?php
namespace vendor_namemodule_nameControllerSetGetSession;

use MagentoFrameworkAppActionAction;

class SetGetSession extends Action

protected $customerSession;

public function _construct(
MagentoCustomerModelSession $customerSession
)

$this->customerSession = $customerSession;




public function execute()





?>


Can anyone please help me with how to assign data and retrieve it from session variable?



Thank you.










share|improve this question




























    10















    I am struggling with magento 2 session. I have created below controller file as a sample code.



    <?php
    namespace vendor_namemodule_nameControllerSetGetSession;

    use MagentoFrameworkAppActionAction;

    class SetGetSession extends Action

    protected $customerSession;

    public function _construct(
    MagentoCustomerModelSession $customerSession
    )

    $this->customerSession = $customerSession;




    public function execute()





    ?>


    Can anyone please help me with how to assign data and retrieve it from session variable?



    Thank you.










    share|improve this question
























      10












      10








      10








      I am struggling with magento 2 session. I have created below controller file as a sample code.



      <?php
      namespace vendor_namemodule_nameControllerSetGetSession;

      use MagentoFrameworkAppActionAction;

      class SetGetSession extends Action

      protected $customerSession;

      public function _construct(
      MagentoCustomerModelSession $customerSession
      )

      $this->customerSession = $customerSession;




      public function execute()





      ?>


      Can anyone please help me with how to assign data and retrieve it from session variable?



      Thank you.










      share|improve this question














      I am struggling with magento 2 session. I have created below controller file as a sample code.



      <?php
      namespace vendor_namemodule_nameControllerSetGetSession;

      use MagentoFrameworkAppActionAction;

      class SetGetSession extends Action

      protected $customerSession;

      public function _construct(
      MagentoCustomerModelSession $customerSession
      )

      $this->customerSession = $customerSession;




      public function execute()





      ?>


      Can anyone please help me with how to assign data and retrieve it from session variable?



      Thank you.







      magento2 customer-session






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 27 '16 at 6:08









      Aniket ShindeAniket Shinde

      73119




      73119




















          2 Answers
          2






          active

          oldest

          votes


















          14














          You can Set and get Customer session by using MagentoCustomerModelSession



          protected $customerSession;

          public function __construct(
          MagentoCustomerModelSession $customerSession
          )
          $this->customerSession = $customerSession;


          $this->customerSession->setMyValue('test');
          $this->customerSession->getMyValue();


          Or by object manager.



           $objectManager = MagentoFrameworkAppObjectManager::getInstance();
          $customerSession = $objectManager->create('MagentoCustomerModelSession');
          $customerSession->setMyValue('test');
          $customerSession->getMyValue();



          1. Setting an information to the customer session:



            $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
            $om->get('MagentoCustomerModelSession');

            $session->setTestKey('test value');




          2. Getting an information from the customer session:



            $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
            $om->get('MagentoCustomerModelSession');
            echo $session->getTestKey();



          Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.



          Hope this answer will help you.






          share|improve this answer

























          • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

            – Aniket Shinde
            Jul 27 '16 at 6:21











          • Please check modified answer added by object manager.

            – Krishna ijjada
            Jul 27 '16 at 6:27











          • Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

            – Aniket Shinde
            Jul 27 '16 at 6:34



















          1














          You need to inject MagentoCustomerModelSession class for set and get data in customer session



          Using Dependency Injection



          protected $customerSession;

          public function _construct(
          ...
          MagentoCustomerModelSession $customerSession
          ...
          )
          ...
          $this->customerSession = $customerSession;
          ...


          public function setValue()

          return $this->customerSession->setMyValue('YourValue'); //set value in customer session


          public function getValue()

          return $this->customerSession->getMyValue(); //Get value from customer session



          Using Object Manager



          $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
          $customerSession = $objectManager->get('MagentoCustomerModelSession');

          $customerSession->setMyValue('YourValue'); //set value in customer session
          echo $customerSession->getMyValue(); //Get value from customer session





          share|improve this answer

























            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            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%2f127614%2fhow-to-set-and-get-customer-session-data-in-magento-2%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            14














            You can Set and get Customer session by using MagentoCustomerModelSession



            protected $customerSession;

            public function __construct(
            MagentoCustomerModelSession $customerSession
            )
            $this->customerSession = $customerSession;


            $this->customerSession->setMyValue('test');
            $this->customerSession->getMyValue();


            Or by object manager.



             $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $customerSession = $objectManager->create('MagentoCustomerModelSession');
            $customerSession->setMyValue('test');
            $customerSession->getMyValue();



            1. Setting an information to the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');

              $session->setTestKey('test value');




            2. Getting an information from the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');
              echo $session->getTestKey();



            Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.



            Hope this answer will help you.






            share|improve this answer

























            • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

              – Aniket Shinde
              Jul 27 '16 at 6:21











            • Please check modified answer added by object manager.

              – Krishna ijjada
              Jul 27 '16 at 6:27











            • Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

              – Aniket Shinde
              Jul 27 '16 at 6:34
















            14














            You can Set and get Customer session by using MagentoCustomerModelSession



            protected $customerSession;

            public function __construct(
            MagentoCustomerModelSession $customerSession
            )
            $this->customerSession = $customerSession;


            $this->customerSession->setMyValue('test');
            $this->customerSession->getMyValue();


            Or by object manager.



             $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $customerSession = $objectManager->create('MagentoCustomerModelSession');
            $customerSession->setMyValue('test');
            $customerSession->getMyValue();



            1. Setting an information to the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');

              $session->setTestKey('test value');




            2. Getting an information from the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');
              echo $session->getTestKey();



            Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.



            Hope this answer will help you.






            share|improve this answer

























            • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

              – Aniket Shinde
              Jul 27 '16 at 6:21











            • Please check modified answer added by object manager.

              – Krishna ijjada
              Jul 27 '16 at 6:27











            • Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

              – Aniket Shinde
              Jul 27 '16 at 6:34














            14












            14








            14







            You can Set and get Customer session by using MagentoCustomerModelSession



            protected $customerSession;

            public function __construct(
            MagentoCustomerModelSession $customerSession
            )
            $this->customerSession = $customerSession;


            $this->customerSession->setMyValue('test');
            $this->customerSession->getMyValue();


            Or by object manager.



             $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $customerSession = $objectManager->create('MagentoCustomerModelSession');
            $customerSession->setMyValue('test');
            $customerSession->getMyValue();



            1. Setting an information to the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');

              $session->setTestKey('test value');




            2. Getting an information from the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');
              echo $session->getTestKey();



            Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.



            Hope this answer will help you.






            share|improve this answer















            You can Set and get Customer session by using MagentoCustomerModelSession



            protected $customerSession;

            public function __construct(
            MagentoCustomerModelSession $customerSession
            )
            $this->customerSession = $customerSession;


            $this->customerSession->setMyValue('test');
            $this->customerSession->getMyValue();


            Or by object manager.



             $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $customerSession = $objectManager->create('MagentoCustomerModelSession');
            $customerSession->setMyValue('test');
            $customerSession->getMyValue();



            1. Setting an information to the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');

              $session->setTestKey('test value');




            2. Getting an information from the customer session:



              $om = MagentoFrameworkAppObjectManager::getInstance(); $session =
              $om->get('MagentoCustomerModelSession');
              echo $session->getTestKey();



            Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.



            Hope this answer will help you.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 13 at 7:59









            Giel Berkers

            7,17924382




            7,17924382










            answered Jul 27 '16 at 6:14









            Krishna ijjadaKrishna ijjada

            5,92542356




            5,92542356












            • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

              – Aniket Shinde
              Jul 27 '16 at 6:21











            • Please check modified answer added by object manager.

              – Krishna ijjada
              Jul 27 '16 at 6:27











            • Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

              – Aniket Shinde
              Jul 27 '16 at 6:34


















            • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

              – Aniket Shinde
              Jul 27 '16 at 6:21











            • Please check modified answer added by object manager.

              – Krishna ijjada
              Jul 27 '16 at 6:27











            • Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

              – Aniket Shinde
              Jul 27 '16 at 6:34

















            I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

            – Aniket Shinde
            Jul 27 '16 at 6:21





            I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.

            – Aniket Shinde
            Jul 27 '16 at 6:21













            Please check modified answer added by object manager.

            – Krishna ijjada
            Jul 27 '16 at 6:27





            Please check modified answer added by object manager.

            – Krishna ijjada
            Jul 27 '16 at 6:27













            Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

            – Aniket Shinde
            Jul 27 '16 at 6:34






            Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.

            – Aniket Shinde
            Jul 27 '16 at 6:34














            1














            You need to inject MagentoCustomerModelSession class for set and get data in customer session



            Using Dependency Injection



            protected $customerSession;

            public function _construct(
            ...
            MagentoCustomerModelSession $customerSession
            ...
            )
            ...
            $this->customerSession = $customerSession;
            ...


            public function setValue()

            return $this->customerSession->setMyValue('YourValue'); //set value in customer session


            public function getValue()

            return $this->customerSession->getMyValue(); //Get value from customer session



            Using Object Manager



            $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
            $customerSession = $objectManager->get('MagentoCustomerModelSession');

            $customerSession->setMyValue('YourValue'); //set value in customer session
            echo $customerSession->getMyValue(); //Get value from customer session





            share|improve this answer





























              1














              You need to inject MagentoCustomerModelSession class for set and get data in customer session



              Using Dependency Injection



              protected $customerSession;

              public function _construct(
              ...
              MagentoCustomerModelSession $customerSession
              ...
              )
              ...
              $this->customerSession = $customerSession;
              ...


              public function setValue()

              return $this->customerSession->setMyValue('YourValue'); //set value in customer session


              public function getValue()

              return $this->customerSession->getMyValue(); //Get value from customer session



              Using Object Manager



              $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
              $customerSession = $objectManager->get('MagentoCustomerModelSession');

              $customerSession->setMyValue('YourValue'); //set value in customer session
              echo $customerSession->getMyValue(); //Get value from customer session





              share|improve this answer



























                1












                1








                1







                You need to inject MagentoCustomerModelSession class for set and get data in customer session



                Using Dependency Injection



                protected $customerSession;

                public function _construct(
                ...
                MagentoCustomerModelSession $customerSession
                ...
                )
                ...
                $this->customerSession = $customerSession;
                ...


                public function setValue()

                return $this->customerSession->setMyValue('YourValue'); //set value in customer session


                public function getValue()

                return $this->customerSession->getMyValue(); //Get value from customer session



                Using Object Manager



                $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
                $customerSession = $objectManager->get('MagentoCustomerModelSession');

                $customerSession->setMyValue('YourValue'); //set value in customer session
                echo $customerSession->getMyValue(); //Get value from customer session





                share|improve this answer















                You need to inject MagentoCustomerModelSession class for set and get data in customer session



                Using Dependency Injection



                protected $customerSession;

                public function _construct(
                ...
                MagentoCustomerModelSession $customerSession
                ...
                )
                ...
                $this->customerSession = $customerSession;
                ...


                public function setValue()

                return $this->customerSession->setMyValue('YourValue'); //set value in customer session


                public function getValue()

                return $this->customerSession->getMyValue(); //Get value from customer session



                Using Object Manager



                $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
                $customerSession = $objectManager->get('MagentoCustomerModelSession');

                $customerSession->setMyValue('YourValue'); //set value in customer session
                echo $customerSession->getMyValue(); //Get value from customer session






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 days ago









                Mark Shust

                544716




                544716










                answered Apr 25 '17 at 12:38









                Prince PatelPrince Patel

                14.6k65481




                14.6k65481



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f127614%2fhow-to-set-and-get-customer-session-data-in-magento-2%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                    Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                    Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림