Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Add Comment
Please, Sign In to add comment