1 | <?php |
||
2 | |||
3 | namespace Helix\Shopify; |
||
4 | |||
5 | use Helix\Shopify\Base\AbstractEntity; |
||
6 | use Helix\Shopify\Base\AbstractEntity\CrudTrait; |
||
7 | use Helix\Shopify\Base\AbstractEntity\MetafieldTrait; |
||
8 | use Helix\Shopify\Customer\Address; |
||
9 | use Helix\Shopify\Customer\Invite; |
||
10 | |||
11 | /** |
||
12 | * A customer. |
||
13 | * |
||
14 | * @see https://shopify.dev/docs/admin-api/rest/reference/customers/customer |
||
15 | * |
||
16 | * @see Shop::newCustomer() |
||
17 | * |
||
18 | * @method $this setState (string $status) @depends create-only, see the constants |
||
19 | * @method $this setSendEmailInvite (bool $invite) @depends create-only |
||
20 | * @method $this setSendEmailWelcome (bool $welcome) @depends create-only |
||
21 | * @method $this setAddresses (Address[] $addresses) @depends create-only, bulk |
||
22 | * @method $this setEmail (string $email) required, unique |
||
23 | * @method $this setFirstName (string $name) required |
||
24 | * @method $this setLastName (string $name) required |
||
25 | * |
||
26 | * @method bool getAcceptsMarketing () |
||
27 | * @method string getAcceptsMarketingUpdatedAt() |
||
28 | * @method Address[] getAddresses () |
||
29 | * @method string getCurrency () read-only |
||
30 | * @method string getCreatedAt () ISO8601 |
||
31 | * @method string getEmail () |
||
32 | * @method string getFirstName () |
||
33 | * @method string getLastName () |
||
34 | * @method string getLastOrderId () read-only |
||
35 | * @method string getLastOrderName () read-only |
||
36 | * @method string getMarketingOptInLevel () read-only |
||
37 | * @method null|string getMultipassIdentifier () |
||
38 | * @method string getNote () |
||
39 | * @method int getOrdersCount () read-only |
||
40 | * @method string getPhone () |
||
41 | * @method string getTags () |
||
42 | * @method bool isTaxExempt () |
||
43 | * @method string[] getTaxExemptions () @depends canada-only |
||
44 | * @method bool hasTaxExemptions () @depends canada-only |
||
45 | * @method string getTotalSpent () |
||
46 | * @method string getUpdatedAt () |
||
47 | * @method bool isVerifiedEmail () |
||
48 | * |
||
49 | * @method bool hasAddresses () |
||
50 | * @method bool hasPhone () |
||
51 | * |
||
52 | * @method $this setAcceptsMarketing (bool $opted) |
||
53 | * @method $this setAcceptsMarketingUpdatedAt(string $iso8601) |
||
54 | * @method $this setMultipassIdentifier (?string $ident) |
||
55 | * @method $this setNote (string $note) |
||
56 | * @method $this setPhone (string $e164) unique |
||
57 | * @method $this setTags (string $csv) |
||
58 | * @method $this setTaxExempt (bool $exempt) |
||
59 | * |
||
60 | * @method Address[] selectAddresses (callable $filter) `fn( Address $address ): bool` |
||
61 | */ |
||
62 | class Customer extends AbstractEntity |
||
63 | { |
||
64 | |||
65 | use CrudTrait; |
||
66 | use MetafieldTrait; |
||
67 | |||
68 | const TYPE = 'customer'; |
||
69 | const DIR = 'customers'; |
||
70 | |||
71 | const MAP = [ |
||
72 | 'addresses' => [Address::class], |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Customer can't log in. This is the default. |
||
77 | */ |
||
78 | const IS_DISABLED = 'disabled'; |
||
79 | |||
80 | /** |
||
81 | * Customer needs to activate their account via invite link before logging in. |
||
82 | */ |
||
83 | const IS_INVITED = 'invited'; |
||
84 | |||
85 | /** |
||
86 | * Customer can log in and shop. |
||
87 | */ |
||
88 | const IS_ENABLED = 'enabled'; |
||
89 | |||
90 | /** |
||
91 | * Customer declined the invite, has no account. |
||
92 | */ |
||
93 | const IS_DECLINED = 'declined'; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $activationUrl; |
||
99 | |||
100 | protected function _setData(array $data) |
||
101 | { |
||
102 | // redundant |
||
103 | unset($data['default_address']); |
||
104 | |||
105 | return parent::_setData($data); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getActivationUrl(): string |
||
112 | { |
||
113 | assert($this->hasId()); |
||
114 | return $this->activationUrl |
||
115 | ?? $this->activationUrl = $this->api->post("{$this}/account_activation_url")['account_activation_url']; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return null|Address |
||
120 | */ |
||
121 | public function getDefaultAddress() |
||
122 | { |
||
123 | return $this->selectAddresses(function (Address $address) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
124 | return $address->isDefault(); |
||
125 | })[0] ?? null; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return Order[] |
||
130 | */ |
||
131 | public function getOrders() |
||
132 | { |
||
133 | assert($this->hasId()); |
||
134 | return Order::loadAll($this, "{$this}/orders"); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return string[] |
||
139 | */ |
||
140 | public function getPoolKeys() |
||
141 | { |
||
142 | $keys = parent::getPoolKeys(); |
||
143 | $keys[] = $this->data['email'] ?? null; |
||
144 | $keys[] = $this->data['phone'] ?? null; |
||
145 | $keys[] = $this->data['multipass_identifier'] ?? null; |
||
146 | return array_filter($keys); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return bool |
||
151 | */ |
||
152 | final public function isDeclined(): bool |
||
153 | { |
||
154 | return $this->data['state'] === self::IS_DECLINED; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | final public function isDisabled(): bool |
||
161 | { |
||
162 | return $this->data['state'] === self::IS_DISABLED; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return bool |
||
167 | */ |
||
168 | final public function isEnabled(): bool |
||
169 | { |
||
170 | return $this->data['state'] === self::IS_ENABLED; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return bool |
||
175 | */ |
||
176 | final public function isInvited(): bool |
||
177 | { |
||
178 | return $this->data['state'] === self::IS_INVITED; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return Address |
||
183 | */ |
||
184 | public function newAddress() |
||
185 | { |
||
186 | return $this->api->factory($this, Address::class, [ |
||
187 | 'customer_id' => $this->getId() |
||
188 | ]); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Creates an invite for an existing customer. |
||
193 | * |
||
194 | * @return Invite |
||
195 | */ |
||
196 | public function newInvite() |
||
197 | { |
||
198 | return new Invite($this); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param string $password |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function setPassword(string $password) |
||
206 | { |
||
207 | $this->_set('password', $password); |
||
208 | $this->_set('password_confirmation', $password); |
||
209 | return $this; |
||
210 | } |
||
211 | } |
||
212 |