| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Wishlist; |
| 4 |
|
| 5 |
use FluentCart\Api\ModuleSettings; |
| 6 |
use FluentCart\App\Models\Customer; |
| 7 |
use FluentCart\App\Services\WishlistService; |
| 8 |
use FluentCart\App\Hooks\Handlers\BlockEditors\Buttons\WishlistButtonBlockEditor; |
| 9 |
|
| 10 |
class WishlistModule |
| 11 |
{ |
| 12 |
public function register() |
| 13 |
{ |
| 14 |
add_filter('fluent_cart/module_setting/fields', function ($fields) { |
| 15 |
$fields['wishlist'] = [ |
| 16 |
'title' => __('Wishlist', 'fluent-cart'), |
| 17 |
'description' => __('Allow customers to save products to a wishlist for later.', 'fluent-cart'), |
| 18 |
'type' => 'component', |
| 19 |
'component' => 'ModuleSettings', |
| 20 |
]; |
| 21 |
$fields['wishlist_guest'] = [ |
| 22 |
'title' => __('Guest Wishlist', 'fluent-cart'), |
| 23 |
'description' => __('Allow guest (non-logged-in) users to add products to a wishlist.', 'fluent-cart'), |
| 24 |
'type' => 'component', |
| 25 |
'component' => 'ModuleSettings', |
| 26 |
]; |
| 27 |
return $fields; |
| 28 |
}, 10, 1); |
| 29 |
|
| 30 |
add_filter('fluent_cart/module_setting/default_values', function ($values) { |
| 31 |
if (empty($values['wishlist']['active'])) { |
| 32 |
$values['wishlist']['active'] = 'yes'; |
| 33 |
} |
| 34 |
if (empty($values['wishlist_guest']['active'])) { |
| 35 |
$values['wishlist_guest']['active'] = 'no'; |
| 36 |
} |
| 37 |
return $values; |
| 38 |
}, 10, 1); |
| 39 |
|
| 40 |
if (!WishlistService::isEnabled()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Register the Gutenberg block |
| 45 |
WishlistButtonBlockEditor::register(); |
| 46 |
|
| 47 |
// Merge guest wishlist into customer's wishlist on login |
| 48 |
add_action('wp_login', [$this, 'mergeGuestWishlistOnLogin'], 10, 2); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @param string $userLogin |
| 53 |
* @param \WP_User $user |
| 54 |
*/ |
| 55 |
public function mergeGuestWishlistOnLogin($userLogin, $user) |
| 56 |
{ |
| 57 |
$sessionId = WishlistService::getGuestSessionId(); |
| 58 |
if (!$sessionId) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$customer = Customer::where('user_id', $user->ID)->first(); |
| 63 |
if (!$customer) { |
| 64 |
$customer = Customer::where('email', $user->user_email)->first(); |
| 65 |
} |
| 66 |
|
| 67 |
if ($customer) { |
| 68 |
WishlistService::mergeGuestWishlist($sessionId, $customer->id); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|