Answers.Body: <p>I figured that i could just use <code>Craft::$app-&gt;getSession()-&gt;set( 'key', $value );</code> to store the vars in the craft session.</p>
  
  <pre><code>class somePlugin extends Plugin {
  
      public function init() {
  
          parent::init();
          self::$plugin = $this;
  
          Event::on( Order::class, Order::EVENT_BEFORE_COMPLETE_ORDER, function( Event $e ) {
              $order             = $e-&gt;sender;
              $order-&gt;customerId = $this-&gt;getCustomerId();
          } );
      }
  
      public function setUser( $userId ) {
  
          self::$plugin = $this;
  
          // get userModel by userId
          $userModel = Craft::$app-&gt;users-&gt;getUserById( $userId );
  
          // check if  commerce record for craft user exists
          if ( ! $this-&gt;customers-&gt;getCustomerByUserId( $userId ) ) {
  
              // create and save new customer
              $newCustomer       = new Customer();
              $newCustomer-&gt;user = $userModel;
              $this-&gt;customers-&gt;saveCustomer( $newCustomer, false );
          }
  
          // get and store customerId
          $customerId = $this-&gt;customers-&gt;getCustomerByUserId( $userId )-&gt;id;
          $this-&gt;setCustomerId( $customerId );
  
      }
  
      public function setCustomerId( $customerId ) {
          Craft::$app-&gt;getSession()-&gt;set( 'customerId', $customerId );
      }
  
      public function getCustomerId() {
          return Craft::$app-&gt;getSession()-&gt;get( 'customerId' );
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-07T16:09:11.777Z
Answers.LastActivityDate: 2018-12-07T16:09:11.777Z
Answers.UserDisplayName: HendrikEng
Body: <p>I am struggling with closure and saving a variable.</p>
  
  <p>In my class <code>somePlugin</code> i do have a public var <code>$customerId</code> , which should be updated after i execute my <code>setUser()</code> function. To do so i have a setter and getter function. In my <code>init()</code> function i do want to use the new var on a later <code>EVENT_BEFORE_COMPLETE_ORDER</code> event. However the variable <code>$customerId</code> does get reset to the default value after page reload.</p>
  
  <p>How would i avoid that?</p>
  
  <p>I tried using session but it seems like the session has been overwritten on reload as well</p>
  
  <p>
  
  <pre><code>class somePlugin extends Plugin {
  
      public $customerId = '1234';
  
      public function init() {
  
          parent::init();
          self::$plugin = $this;
  
          Event::on( Order::class, Order::EVENT_BEFORE_COMPLETE_ORDER, function( Event $e ) {
              $order             = $e-&gt;sender;
              $customerId        = $this-&gt;getCustomerId();
  
              // on every page reload it does get reset to the default var, what is not wanted
              $order-&gt;customerId = $customerId;
          } );
      }
  
      public function setUser( $userId ) {
  
          self::$plugin = $this;
  
          // get userModel by userId
          $userModel = Craft::$app-&gt;users-&gt;getUserById( $userId );
  
          // check if  commerce record for craft user exists
          if ( ! $this-&gt;customers-&gt;getCustomerByUserId( $userId ) ) {
  
              // create and save new customer
              $newCustomer       = new Customer();
              $newCustomer-&gt;user = $userModel;
              $this-&gt;customers-&gt;saveCustomer( $newCustomer, false );
          }
  
          // store customerId
          $customerId = $this-&gt;customers-&gt;getCustomerByUserId( $userId )-&gt;id;
  
         // sets the new and wanted it wich gets overwritten by page reload
          $this-&gt;setCustomerId( $customerId );
  
      }
  
      public function setCustomerId( $customerId ) {
          $this-&gt;customerId = $customerId;
      }
  
      public function getCustomerId() {
          return $this-&gt;customerId;
      }
  }
  </code></pre>
  
Tags: <plugin-development><craft3><php>
Title: Closure in plugin class/ saving and passing a public property
ThreadText: # Closure in plugin class/ saving and passing a public property
  
  (Asked by: username_0 on 2018-12-07)
  
  I am struggling with closure and saving a variable.
  
  In my class `somePlugin` i do have a public var `$customerId` , which should be updated after i execute my `setUser()` function. To do so i have a setter and getter function. In my `init()` function i do want to use the new var on a later `EVENT_BEFORE_COMPLETE_ORDER` event. However the variable `$customerId` does get reset to the default value after page reload.
  
  How would i avoid that?
  
  I tried using session but it seems like the session has been overwritten on reload as well
  
  ```
  class somePlugin extends Plugin {
  
      public $customerId = '1234';
  
      public function init() {
  
          parent::init();
          self::$plugin = $this;
  
          Event::on( Order::class, Order::EVENT_BEFORE_COMPLETE_ORDER, function( Event $e ) {
              $order             = $e->sender;
              $customerId        = $this->getCustomerId();
  
              // on every page reload it does get reset to the default var, what is not wanted
              $order->customerId = $customerId;
          } );
      }
  
      public function setUser( $userId ) {
  
          self::$plugin = $this;
  
          // get userModel by userId
          $userModel = Craft::$app->users->getUserById( $userId );
  
          // check if  commerce record for craft user exists
          if ( ! $this->customers->getCustomerByUserId( $userId ) ) {
  
              // create and save new customer
              $newCustomer       = new Customer();
              $newCustomer->user = $userModel;
              $this->customers->saveCustomer( $newCustomer, false );
          }
  
          // store customerId
          $customerId = $this->customers->getCustomerByUserId( $userId )->id;
  
         // sets the new and wanted it wich gets overwritten by page reload
          $this->setCustomerId( $customerId );
  
      }
  
      public function setCustomerId( $customerId ) {
          $this->customerId = $customerId;
      }
  
      public function getCustomerId() {
          return $this->customerId;
      }
  }
  
  ```
  
  **Tags:** plugin-development, craft3, php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-12-07. Score: 1 | Accepted answer
  
  I figured that i could just use `Craft::$app->getSession()->set( 'key', $value );` to store the vars in the craft session.
  
  ```
  class somePlugin extends Plugin {
  
      public function init() {
  
          parent::init();
          self::$plugin = $this;
  
          Event::on( Order::class, Order::EVENT_BEFORE_COMPLETE_ORDER, function( Event $e ) {
              $order             = $e->sender;
              $order->customerId = $this->getCustomerId();
          } );
      }
  
      public function setUser( $userId ) {
  
          self::$plugin = $this;
  
          // get userModel by userId
          $userModel = Craft::$app->users->getUserById( $userId );
  
          // check if  commerce record for craft user exists
          if ( ! $this->customers->getCustomerByUserId( $userId ) ) {
  
              // create and save new customer
              $newCustomer       = new Customer();
              $newCustomer->user = $userModel;
              $this->customers->saveCustomer( $newCustomer, false );
          }
  
          // get and store customerId
          $customerId = $this->customers->getCustomerByUserId( $userId )->id;
          $this->setCustomerId( $customerId );
  
      }
  
      public function setCustomerId( $customerId ) {
          Craft::$app->getSession()->set( 'customerId', $customerId );
      }
  
      public function getCustomerId() {
          return Craft::$app->getSession()->get( 'customerId' );
      }
  }
  
  ```
  
  
  ---
  
  