-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
404 lines (351 loc) · 12.2 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* Get all collections.
*
* @return array|null Database query results.
*/
function medusawp_get_collections() {
return MedusaWP\Models\ProductCollection::all();
}
/**
* Get all countries.
*
* @return array|null Array of countries.
*/
function medusawp_get_countries() {
return MedusaWP\Models\Country::all();
}
/**
* Get all products.
*
* @return array|null Database query results.
*/
function medusawp_get_products() {
return MedusaWP\Models\Product::all();
}
/**
* Get all regions.
*
* @return array|null Database query results.
*/
function medusawp_get_regions() {
return MedusaWP\Models\Region::all();
}
/**
* Get collection by its id.
*
* @param string $id
* @return array|null|void Database query result.
*/
function medusawp_get_collection( string $id ) {
return MedusaWP\Models\ProductCollection::find( $id );
}
/**
* Get collection by its post id.
*
* @param int $post_id
* @return array|null|void Database query result.
*/
function medusawp_get_collection_by_post_id( int $post_id ) {
return MedusaWP\Models\ProductCollection::find_by( 'post_id', $post_id );
}
/**
* Get country by its id.
*
* @param string $id
* @return array|null|void Database query result.
*/
function medusawp_get_country( string $id ) {
return MedusaWP\Models\Country::find( $id );
}
/**
* Get product by its id.
*
* @param string $id
* @return array|null|void Database query result.
*/
function medusawp_get_product( string $id ) {
return MedusaWP\Models\Product::find( $id );
}
/**
* Get product by its post id.
*
* @param int $post_id
* @return array|null|void Database query result.
*/
function medusawp_get_product_by_post_id( int $post_id ) {
return MedusaWP\Models\Product::find_by( 'post_id', $post_id );
}
/**
* Get products from collection.
*
* @param string $collection_id
* @return array
*/
function medusawp_get_collection_products( string $collection_id ) {
return MedusaWP\Models\ProductCollection::get_products( $collection_id );
}
/**
* Get region by its id.
*
* @param string $id
* @return array|null|void Database query result.
*/
function medusawp_get_region( string $id ) {
return MedusaWP\Models\Region::find( $id );
}
/**
* Get variant by its id.
*
* @param string $id
* @return array|null|void Database query result.
*/
function medusawp_get_variant( string $id ) {
return MedusaWP\Models\ProductVariant::find( $id );
}
/**
* Get variants for given product id.
*
* @param string $product_id
* @return array|null Array of variants.
*/
function medusawp_get_product_variants( string $product_id ) {
return MedusaWP\Models\Product::get_variants( $product_id );
}
/**
* Get collection for given product id.
*
* @param string $id
* @return array|null|void Database query result.
*/
function medusawp_get_product_collection( string $product_id ) {
$collection_id = MedusaWP\Models\Product::get_column( 'collection_id', $product_id );
$collection = MedusaWP\Models\ProductCollection::find( $collection_id );
return $collection;
}
/**
* Get current cart object from Medusa.
*
* @return \MedusaWP\MedusaClient\Store\Model\Cart|null Cart details or `null`.
*/
function medusawp_get_cart() {
global $medusawp_cart;
return isset( $medusawp_cart ) ? $medusawp_cart : null;
}
/**
* Get current country.
*
* @return object|null Country object or `null`.
*/
function medusawp_get_current_country() {
$country_code = medusawp_get_current_country_code();
if ( ! empty( $country_code ) ) {
return MedusaWP\Models\Country::find_by( 'iso_2', strtolower( $country_code ) );
}
return null;
}
/**
* Get current cart country code.
*
* @return string|null Country code.
*/
function medusawp_get_current_country_code() {
global $medusawp_country_code;
return isset( $medusawp_country_code ) ? $medusawp_country_code : null;
}
/**
* Get current cart currency.
*
* @return string|null Currency code.
*/
function medusawp_get_current_currency() {
$cart = medusawp_get_cart();
if ( ! empty( $cart ) ) {
$region = $cart->getRegion();
if ( $region ) {
return $region->getCurrencyCode();
}
}
return null;
}
/**
* Get current cart region.
*
* @return \MedusaWP\MedusaClient\Store\Model\Region|null Region object or `null`.
*/
function medusawp_get_current_region() {
global $medusawp_region;
return isset( $medusawp_region ) ? $medusawp_region : null;
}
/**
* Takes a product variant and a region, and converts the variant's price to a localized decimal format.
*
* @link https://docs.medusajs.com/medusa-react/overview#formatvariantprice
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L16C1-L29C2
*
* @param array|null $variant The product variant.
* @param array|null $region The region.
* @param bool $include_taxes Optional. Whether the computed price should include taxes or not. Default `true`.
* @param null|int $minimum_fraction_digits Optional. The minimum number of fraction digits to use when formatting the price.
* @param null|int $maximum_fraction_digits Optional. The maximum number of fraction digits to use when formatting the price.
* @param null|string $locale Optional. Locale in which the number would be formatted. Default 'en_US'.
* @return string|false The formatted price's amount.
*/
function medusawp_format_variant_price(
?array $variant,
?array $region,
bool $include_taxes = true,
?int $minimum_fraction_digits = null,
?int $maximum_fraction_digits = null,
?string $locale = null
) {
$amount = medusawp_compute_variant_price( $variant, $region, $include_taxes );
$currency_code = isset( $region['currency_code'] ) ? $region['currency_code'] : '';
return medusawp_convert_to_locale(
$amount,
$currency_code,
$minimum_fraction_digits,
$maximum_fraction_digits,
$locale
);
}
/**
* Takes a product variant and region, and returns the variant price as a decimal number.
*
* @link https://docs.medusajs.com/medusa-react/overview#computevariantprice
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L43C1-L55C2
*
* @param array|null $variant The product variant.
* @param array|null $region The region.
* @param bool $include_taxes Optional. Whether to include taxes or not. Default `true`.
* @return float The price's amount without formatting.
*/
function medusawp_compute_variant_price( ?array $variant, ?array $region, bool $include_taxes = true ): float {
$amount = medusawp_get_variant_price( $variant, $region );
return medusawp_compute_amount( $amount, $region, $include_taxes );
}
/**
* Finds the price amount corresponding to the region selected.
*
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L63C1-L73C2
*
* @param array|null $variant The product variant.
* @param array|null $region The region.
* @return float The price's amount.
*/
function medusawp_get_variant_price( ?array $variant, ?array $region ): float {
$prices = isset( $variant['prices'] ) ? $variant['prices'] : array();
$prices_regions = is_array( $prices ) ? array_column( $prices, 'currency_code' ) : array();
$price = is_array( $prices_regions ) && isset( $region['currency_code'] )
? array_search( $region['currency_code'], $prices_regions, true )
: null;
return $prices[ $price ]['amount'] ?? 0;
}
/**
* Takes an amount and a region, and converts the amount to a localized decimal format.
*
* @link https://docs.medusajs.com/medusa-react/overview#formatamount
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L110C1-L126C2
*
* @param float $amount The number that should be used for computation.
* @param array|null $region The region.
* @param bool $include_taxes Optional. Whether to include taxes or not. Default `true`.
* @param null|int $minimum_fraction_digits Optional. The minimum number of fraction digits to use when formatting the price.
* @param null|int $maximum_fraction_digits Optional. The maximum number of fraction digits to use when formatting the price.
* @param null|string $locale Optional. Locale in which the number would be formatted. Default 'en_US'.
* @return string|false The formatted amount.
*/
function medusawp_format_amount(
float $amount,
?array $region,
bool $include_taxes = true,
?int $minimum_fraction_digits = null,
?int $maximum_fraction_digits = null,
?string $locale = null
) {
$tax_aware_amount = medusawp_compute_amount( $amount, $region, $include_taxes );
$currency_code = isset( $region['currency_code'] ) ? $region['currency_code'] : '';
return medusawp_convert_to_locale(
$tax_aware_amount,
$currency_code,
$minimum_fraction_digits,
$maximum_fraction_digits,
$locale
);
}
/**
* Takes an amount, a region, and returns the amount as a decimal including or excluding taxes.
*
* @link https://docs.medusajs.com/medusa-react/overview#computeamount
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L84C1-L96C2
*
* @param float $amount The number that should be used for computation.
* @param array|null $region The region.
* @param bool $include_taxes Optional. Whether to include taxes or not. Default `true`.
* @return float The amount without formatting.
*/
function medusawp_compute_amount( float $amount, ?array $region, bool $include_taxes = true ): float {
$to_decimal = medusawp_convert_to_decimal( $amount, $region );
$tax_rate = $include_taxes ? medusawp_get_tax_rate( $region ) : 0;
$amount_with_taxes = $to_decimal * ( 1 + $tax_rate );
return $amount_with_taxes;
}
/**
* Converts amount into decimal number.
*
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L131C1-L139C2
*
* @param float $amount The amount.
* @param array|null $region The region.
* @return float The amount as decimal number.
*/
function medusawp_convert_to_decimal( float $amount, ?array $region ): float {
$no_division_currencies = array( 'krw', 'jpy', 'vnd' );
$currency = isset( $region['currency_code'] ) ? $region['currency_code'] : null;
$currency = is_string( $currency ) ? strtolower( $currency ) : null;
$divisor = in_array( ( $currency ), $no_division_currencies, true ) ? 1 : 100;
return floor( $amount ) / $divisor;
}
/**
* Takes a region and returns its tax rate.
*
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L141C1-L143C2
*
* @param array|null $region The region.
* @return float The corresponding tax rate.
*/
function medusawp_get_tax_rate( ?array $region ): float {
return $region && ! empty( $region ) && isset( $region['tax_rate'] ) ? $region['tax_rate'] / 100 : 0;
}
/**
* Converts amount into locale format using `intl` - the `NumberFormatter` class.
*
* @link https://github.com/medusajs/medusa/blob/6ee80794c905bfa0f439a4168e3d3abd3ce36336/packages/medusa-react/src/helpers/index.ts#L145C1-L160C2
*
* @param float $amount The amount.
* @param string $currency_code The currency code.
* @param null|int $minimum_fraction_digits Optional. The minimum number of fraction digits to use when formatting the price.
* @param null|int $maximum_fraction_digits Optional. The maximum number of fraction digits to use when formatting the price.
* @param null|string $locale Optional. Locale in which the number would be formatted. Default 'en_US'.
* @return string|false The localized amount.
*/
function medusawp_convert_to_locale(
float $amount,
string $currency_code,
?int $minimum_fraction_digits = null,
?int $maximum_fraction_digits = null,
?string $locale = null
) {
$locale = $locale ?? 'en_US';
if ( $currency_code && ! empty( $currency_code ) ) {
$formatter = new \NumberFormatter( $locale, \NumberFormatter::CURRENCY );
if ( $minimum_fraction_digits ) {
$formatter->setAttribute( \NumberFormatter::MIN_FRACTION_DIGITS, $minimum_fraction_digits );
}
if ( $maximum_fraction_digits ) {
$formatter->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, $maximum_fraction_digits );
}
return $formatter->formatCurrency( $amount, $currency_code );
}
return strval( $amount );
}