| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\FluentCart; |
| 4 |
|
| 5 |
use FluentBooking\App\Services\DateTimeHelper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Read a FluentCart customer's profile (lifetime value, orders, status) for the |
| 9 |
* booking details Customer card. All methods no-op (return null) when FluentCart |
| 10 |
* is inactive or the booking email has no matching customer. |
| 11 |
*/ |
| 12 |
class CustomerProfileService |
| 13 |
{ |
| 14 |
public static function isActive() |
| 15 |
{ |
| 16 |
return defined('FLUENTCART_VERSION'); |
| 17 |
} |
| 18 |
|
| 19 |
// customers/view is Cart Pro; on free only WP admins pass (FluentCart's own gate). |
| 20 |
public static function canView() |
| 21 |
{ |
| 22 |
return self::isActive() |
| 23 |
&& \FluentCart\App\Services\Permission\PermissionManager::userCan('customers/view'); |
| 24 |
} |
| 25 |
|
| 26 |
public static function getProfileData($email) |
| 27 |
{ |
| 28 |
if (!self::isActive() || !$email) { |
| 29 |
return null; |
| 30 |
} |
| 31 |
|
| 32 |
$customer = \FluentCart\App\Models\Customer::where('email', $email)->first(); |
| 33 |
|
| 34 |
if (!$customer) { |
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
$currency = \FluentCart\Api\CurrencySettings::get('currency'); |
| 39 |
|
| 40 |
return [ |
| 41 |
'full_name' => $customer->full_name, |
| 42 |
'status' => $customer->status, |
| 43 |
'profile_url' => self::adminCustomerUrl($customer->id), |
| 44 |
'stats' => [ |
| 45 |
'ltv' => self::formatMoney($customer->ltv, $currency), |
| 46 |
'orders' => (int) $customer->purchase_count, |
| 47 |
'aov' => self::formatMoney($customer->aov, $currency), |
| 48 |
'last_buy' => $customer->last_purchase_date |
| 49 |
? DateTimeHelper::formatToLocale($customer->last_purchase_date, 'date') |
| 50 |
: '', |
| 51 |
], |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Format a cents amount to a currency string. centsToDecimal handles |
| 57 |
* zero-decimal currencies (JPY, KRW); the sign is an HTML entity rendered |
| 58 |
* via v-html, so the result is sanitized server-side. |
| 59 |
*/ |
| 60 |
private static function formatMoney($cents, $currency) |
| 61 |
{ |
| 62 |
$sign = \FluentCart\App\Helpers\CurrenciesHelper::getCurrencySign($currency); |
| 63 |
|
| 64 |
return wp_kses_post($sign . \FluentCart\App\Helpers\CurrenciesHelper::centsToDecimal($cents, $currency)); |
| 65 |
} |
| 66 |
|
| 67 |
private static function adminCustomerUrl($id) |
| 68 |
{ |
| 69 |
$base = apply_filters('fluent_cart/admin_base_url', admin_url('admin.php?page=fluent-cart#/'), []); |
| 70 |
|
| 71 |
return $base . 'customers/' . (int) $id . '/view'; |
| 72 |
} |
| 73 |
} |
| 74 |
|