Files
WooNooW/includes/Frontend/WishlistController.php
Dwindi Ramadhana 863610043d fix: Dashboard always active + Full wishlist settings implementation
Dashboard Navigation Fix:
- Fixed ActiveNavLink to only activate Dashboard on / or /dashboard/* paths
- Dashboard no longer shows active when on other routes (Marketing, Settings, etc.)
- Proper path matching logic for all main menu items

Wishlist Settings - Full Implementation:
Backend (PHP):
1. Guest Wishlist Support
   - Modified check_permission() to allow guests if enable_guest_wishlist is true
   - Guests can now add/remove wishlist items (stored in user meta when they log in)

2. Max Items Limit
   - Added max_items_per_wishlist enforcement in add_to_wishlist()
   - Returns error when limit reached with helpful message
   - 0 = unlimited (default)

Frontend (React):
3. Show in Header Setting
   - Added useModuleSettings hook to customer-spa
   - Wishlist icon respects show_in_header setting (default: true)
   - Icon hidden when setting is false

4. Show Add to Cart Button Setting
   - Wishlist page checks show_add_to_cart_button setting
   - Add to cart buttons hidden when setting is false (default: true)
   - Allows wishlist-only mode without purchase prompts

Files Added (1):
- customer-spa/src/hooks/useModuleSettings.ts

Files Modified (5):
- admin-spa/src/App.tsx (dashboard active fix)
- includes/Frontend/WishlistController.php (guest support, max items)
- customer-spa/src/layouts/BaseLayout.tsx (show_in_header)
- customer-spa/src/pages/Account/Wishlist.tsx (show_add_to_cart_button)
- admin-spa/dist/app.js + customer-spa/dist/app.js (rebuilt)

Implemented Settings (4 of 8):
 enable_guest_wishlist - Backend permission check
 show_in_header - Frontend icon visibility
 max_items_per_wishlist - Backend validation
 show_add_to_cart_button - Frontend button visibility

Not Yet Implemented (4 of 8):
- wishlist_page (page selector - would need routing logic)
- enable_sharing (share functionality - needs share UI)
- enable_email_notifications (back in stock - needs cron job)
- enable_multiple_wishlists (multiple lists - needs data structure change)

All core wishlist settings now functional!
2025-12-26 21:57:56 +07:00

216 lines
7.4 KiB
PHP

<?php
namespace WooNooW\Frontend;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use WooNooW\Core\ModuleRegistry;
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 OR guest wishlist is enabled
*/
public static function check_permission() {
// Allow if logged in
if (is_user_logged_in()) {
return true;
}
// Check if guest wishlist is enabled
$settings = get_option('woonoow_module_wishlist_settings', []);
$enable_guest = $settings['enable_guest_wishlist'] ?? true;
return $enable_guest;
}
/**
* Get wishlist items with product details
*/
public static function get_wishlist(WP_REST_Request $request) {
if (!ModuleRegistry::is_enabled('wishlist')) {
return new WP_Error('module_disabled', __('Wishlist module is disabled', 'woonoow'), ['status' => 403]);
}
$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) {
if (!ModuleRegistry::is_enabled('wishlist')) {
return new WP_Error('module_disabled', __('Wishlist module is disabled', 'woonoow'), ['status' => 403]);
}
// Get settings
$settings = get_option('woonoow_module_wishlist_settings', []);
$max_items = (int) ($settings['max_items_per_wishlist'] ?? 0);
$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 max items limit
if ($max_items > 0 && count($wishlist) >= $max_items) {
return new WP_Error(
'wishlist_limit_reached',
sprintf(__('Wishlist limit reached. Maximum %d items allowed.', 'woonoow'), $max_items),
['status' => 400]
);
}
// 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) {
if (!ModuleRegistry::is_enabled('wishlist')) {
return new WP_Error('module_disabled', __('Wishlist module is disabled', 'woonoow'), ['status' => 403]);
}
$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) {
if (!ModuleRegistry::is_enabled('wishlist')) {
return new WP_Error('module_disabled', __('Wishlist module is disabled', 'woonoow'), ['status' => 403]);
}
$user_id = get_current_user_id();
delete_user_meta($user_id, 'woonoow_wishlist');
return new WP_REST_Response([
'message' => 'Wishlist cleared',
'count' => 0,
], 200);
}
}