PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.4.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.4.0
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Modules / Wishlist / WishlistModule.php

WishlistModule.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.4.0, at app/Modules/Wishlist/WishlistModule.php

72 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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