| @@ -98,12 +98,18 @@ | ||
| 98 | 98 | public static function create($data, $params = []) |
| 99 | 99 | { |
| 100 | 100 | $email = Arr::get($data, 'email'); |
| 101 | 101 | |
| 102 | - $customer = static::getQuery()->firstOrCreate( | |
| 103 | - ['email' => $email], | |
| 104 | - $data | |
| 105 | - ); | |
| 102 | + $ownerId = (int) Arr::get($data, 'user_id'); | |
| 103 | + if (!$ownerId || $ownerId !== get_current_user_id()) { | |
| 104 | + unset($data['user_id']); | |
| 105 | + $ownerId = 0; | |
| 106 | + } | |
| 107 | + $customer = $ownerId ? static::getQuery()->where('user_id', $ownerId)->orderBy('id')->first() : null; | |
| 108 | + if (!$customer) { | |
| 109 | + // Matching an existing email never changes its owner or profile. | |
| 110 | + $customer = static::getQuery()->firstOrCreate(['email' => $email], $data); | |
| 111 | + } | |
| 106 | 112 | |
| 107 | 113 | if ($customer) { |
| 108 | 114 | return static::makeSuccessResponse( |
| 109 | 115 | $customer, |
| @@ -280,5 +286,5 @@ | ||
| 280 | 286 | return static::makeErrorResponse([ |
| 281 | 287 | ['code' => 400, 'message' => __('Selected action is invalid', 'fluent-cart')] |
| 282 | 288 | ]); |
| 283 | 289 | } |
| 284 | -} | |
| 290 | +} | |