feat: implement wishlist feature with admin toggle

- Add WishlistController with full CRUD API
- Create wishlist page in My Account
- Add heart icon to all product card layouts (always visible)
- Implement useWishlist hook for state management
- Add wishlist toggle in admin Settings > Customer
- Fix wishlist menu visibility based on admin settings
- Fix double navigation in wishlist page
- Fix variable product navigation to use React Router
- Add TypeScript type casting fix for addresses
This commit is contained in:
Dwindi Ramadhana
2025-12-26 01:44:15 +07:00
parent 100f9cce55
commit 0b08ddefa1
9 changed files with 608 additions and 10 deletions

View File

@@ -25,6 +25,7 @@ use WooNooW\Frontend\ShopController;
use WooNooW\Frontend\CartController as FrontendCartController;
use WooNooW\Frontend\AccountController;
use WooNooW\Frontend\AddressController;
use WooNooW\Frontend\WishlistController;
use WooNooW\Frontend\HookBridge;
use WooNooW\Api\Controllers\SettingsController;
use WooNooW\Api\Controllers\CartController as ApiCartController;
@@ -127,6 +128,7 @@ class Routes {
FrontendCartController::register_routes();
AccountController::register_routes();
AddressController::register_routes();
WishlistController::register_routes();
HookBridge::register_routes();
});
}

View File

@@ -21,6 +21,7 @@ class CustomerSettingsProvider {
// General
'auto_register_members' => get_option('woonoow_auto_register_members', 'no') === 'yes',
'multiple_addresses_enabled' => get_option('woonoow_multiple_addresses_enabled', 'yes') === 'yes',
'wishlist_enabled' => get_option('woonoow_wishlist_enabled', 'yes') === 'yes',
// VIP Customer Qualification
'vip_min_spent' => floatval(get_option('woonoow_vip_min_spent', 1000)),
@@ -49,6 +50,10 @@ class CustomerSettingsProvider {
$updated = $updated && update_option('woonoow_multiple_addresses_enabled', $settings['multiple_addresses_enabled'] ? 'yes' : 'no');
}
if (isset($settings['wishlist_enabled'])) {
$updated = $updated && update_option('woonoow_wishlist_enabled', $settings['wishlist_enabled'] ? 'yes' : 'no');
}
// VIP settings
if (isset($settings['vip_min_spent'])) {
$updated = $updated && update_option('woonoow_vip_min_spent', floatval($settings['vip_min_spent']));

View File

@@ -0,0 +1,176 @@
<?php
namespace WooNooW\Frontend;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
class WishlistController {
/**
* Register REST API routes
*/
public static function register_routes() {
$namespace = 'woonoow/v1';
// Get wishlist
register_rest_route($namespace, '/account/wishlist', [
'methods' => 'GET',
'callback' => [__CLASS__, 'get_wishlist'],
'permission_callback' => [__CLASS__, 'check_permission'],
]);
// Add to wishlist
register_rest_route($namespace, '/account/wishlist', [
'methods' => 'POST',
'callback' => [__CLASS__, 'add_to_wishlist'],
'permission_callback' => [__CLASS__, 'check_permission'],
'args' => [
'product_id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
],
],
]);
// Remove from wishlist
register_rest_route($namespace, '/account/wishlist/(?P<product_id>\d+)', [
'methods' => 'DELETE',
'callback' => [__CLASS__, 'remove_from_wishlist'],
'permission_callback' => [__CLASS__, 'check_permission'],
]);
// Clear wishlist
register_rest_route($namespace, '/account/wishlist/clear', [
'methods' => 'POST',
'callback' => [__CLASS__, 'clear_wishlist'],
'permission_callback' => [__CLASS__, 'check_permission'],
]);
}
/**
* Check if user is logged in
*/
public static function check_permission() {
return is_user_logged_in();
}
/**
* Get wishlist items with product details
*/
public static function get_wishlist(WP_REST_Request $request) {
$user_id = get_current_user_id();
$wishlist = get_user_meta($user_id, 'woonoow_wishlist', true);
if (!$wishlist || !is_array($wishlist)) {
return new WP_REST_Response([], 200);
}
$items = [];
foreach ($wishlist as $item) {
$product_id = $item['product_id'];
$product = wc_get_product($product_id);
if (!$product) {
continue; // Skip if product doesn't exist
}
$items[] = [
'product_id' => $product_id,
'name' => $product->get_name(),
'slug' => $product->get_slug(),
'price' => $product->get_price(),
'regular_price'=> $product->get_regular_price(),
'sale_price' => $product->get_sale_price(),
'image' => wp_get_attachment_url($product->get_image_id()),
'on_sale' => $product->is_on_sale(),
'stock_status' => $product->get_stock_status(),
'type' => $product->get_type(),
'added_at' => $item['added_at'],
];
}
return new WP_REST_Response($items, 200);
}
/**
* Add product to wishlist
*/
public static function add_to_wishlist(WP_REST_Request $request) {
$user_id = get_current_user_id();
$product_id = $request->get_param('product_id');
// Validate product exists
$product = wc_get_product($product_id);
if (!$product) {
return new WP_Error('invalid_product', 'Product not found', ['status' => 404]);
}
$wishlist = get_user_meta($user_id, 'woonoow_wishlist', true);
if (!is_array($wishlist)) {
$wishlist = [];
}
// Check if already in wishlist
foreach ($wishlist as $item) {
if ($item['product_id'] === $product_id) {
return new WP_Error('already_in_wishlist', 'Product already in wishlist', ['status' => 400]);
}
}
// Add to wishlist
$wishlist[] = [
'product_id' => $product_id,
'added_at' => current_time('mysql'),
];
update_user_meta($user_id, 'woonoow_wishlist', $wishlist);
return new WP_REST_Response([
'message' => 'Product added to wishlist',
'count' => count($wishlist),
], 200);
}
/**
* Remove product from wishlist
*/
public static function remove_from_wishlist(WP_REST_Request $request) {
$user_id = get_current_user_id();
$product_id = (int) $request->get_param('product_id');
$wishlist = get_user_meta($user_id, 'woonoow_wishlist', true);
if (!is_array($wishlist)) {
return new WP_Error('empty_wishlist', 'Wishlist is empty', ['status' => 400]);
}
// Remove product from wishlist
$wishlist = array_filter($wishlist, function($item) use ($product_id) {
return $item['product_id'] !== $product_id;
});
// Re-index array
$wishlist = array_values($wishlist);
update_user_meta($user_id, 'woonoow_wishlist', $wishlist);
return new WP_REST_Response([
'message' => 'Product removed from wishlist',
'count' => count($wishlist),
], 200);
}
/**
* Clear entire wishlist
*/
public static function clear_wishlist(WP_REST_Request $request) {
$user_id = get_current_user_id();
delete_user_meta($user_id, 'woonoow_wishlist');
return new WP_REST_Response([
'message' => 'Wishlist cleared',
'count' => 0,
], 200);
}
}