Guest User

Untitled

a guest
Dec 7th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. class somePlugin extends Plugin {
  2.  
  3. public $customerId = '1234';
  4.  
  5. public function init() {
  6.  
  7. parent::init();
  8. self::$plugin = $this;
  9.  
  10. Event::on( Order::class, Order::EVENT_BEFORE_COMPLETE_ORDER, function( Event $e ) {
  11. $order = $e->sender;
  12. $customerId = $this->getCustomerId();
  13.  
  14. // on every page reload it does get reset to the default var, what is not wanted
  15. $order->customerId = $customerId;
  16. } );
  17. }
  18.  
  19. public function setUser( $userId ) {
  20.  
  21. self::$plugin = $this;
  22.  
  23. // get userModel by userId
  24. $userModel = Craft::$app->users->getUserById( $userId );
  25.  
  26. // check if commerce record for craft user exists
  27. if ( ! $this->customers->getCustomerByUserId( $userId ) ) {
  28.  
  29. // create and save new customer
  30. $newCustomer = new Customer();
  31. $newCustomer->user = $userModel;
  32. $this->customers->saveCustomer( $newCustomer, false );
  33. }
  34.  
  35. // store customerId
  36. $customerId = $this->customers->getCustomerByUserId( $userId )->id;
  37.  
  38. // sets the new and wanted it wich gets overwritten by page reload
  39. $this->setCustomerId( $customerId );
  40.  
  41. }
  42.  
  43. public function setCustomerId( $customerId ) {
  44. $this->customerId = $customerId;
  45. }
  46.  
  47. public function getCustomerId() {
  48. return $this->customerId;
  49. }
  50. }
Add Comment
Please, Sign In to add comment