WordPress/WooCommerce:进入/离开ID51页面时清空购物车故障排查
问题描述
我有一个ID为51的页面(该页面内可添加商品至购物车),需求为:
- 进入该页面时清空购物车
- 在该页面添加商品后,离开页面时也清空购物车
即每次进入或离开ID为51的页面都清空购物车。
我尝试了多种方案,以下是最简洁的代码,进入页面时购物车可被清空,但离开页面时无法清空。我使用session变量标记是否处于ID51的页面,理论上离开时应触发清空操作。
// Clear the cart when entering and leaving page with ID 51 function clear_cart_on_page_51() { // Get the current page ID $current_page_id = get_queried_object_id(); // Check if the current page has an ID of 51 if ($current_page_id == 51) { // Set a session variable to indicate that you're on page 51 $_SESSION['on_page_51'] = true; // Clear the WooCommerce cart when entering the page WC()->cart->empty_cart(); } elseif (isset($_SESSION['on_page_51']) && $_SESSION['on_page_51'] == true) { // Clear the WooCommerce cart when leaving the page with ID 51 WC()->cart->empty_cart(); // Unset the session variable to indicate that you've left the page unset($_SESSION['on_page_51']); } } // Hook the function to run during template redirection add_action('template_redirect', 'clear_cart_on_page_51'); // Add JavaScript to send a console notification when the session variable is set function add_console_notification_script() { // Check if the session variable 'on_page_51' is set if (isset($_SESSION['on_page_51']) && $_SESSION['on_page_51'] == true) { echo '<script>'; echo 'console.log("You are on or have previously visited page 51.");'; echo '</script>'; } } // Hook the JavaScript function to wp_footer add_action('wp_footer', 'add_console_notification_script');
我通过最后一个函数验证session变量已正确设置,但离开页面时购物车仍未清空。所有代码均放置在functions.php文件中。
解决方案
问题根源
原代码的核心问题有两个:
template_redirect钩子执行时,WooCommerce购物车对象可能尚未完全初始化,导致empty_cart()无法生效。- 仅依赖服务器端逻辑无法覆盖用户直接关闭页面、刷新页面等前端触发的离开场景。
修复后的完整代码
// 确保PHP Session启动 add_action('init', function() { if (!session_id()) { session_start(); } }, 1); // 服务器端处理:进入/跳转离开页面51时清空购物车 function clear_cart_on_page_51() { if (!function_exists('WC')) return; $current_page_id = get_queried_object_id(); // 进入页面51时清空购物车并标记session if ($current_page_id == 51) { $_SESSION['on_page_51'] = true; WC()->cart->empty_cart(); } // 从页面51跳转到其他页面时清空购物车并清除标记 elseif (isset($_SESSION['on_page_51']) && $_SESSION['on_page_51'] === true) { WC()->cart->empty_cart(); unset($_SESSION['on_page_51']); } } // 换用wp_loaded钩子,确保WC购物车已初始化 add_action('wp_loaded', 'clear_cart_on_page_51'); // 前端处理:页面卸载时发送AJAX请求清空购物车(覆盖关闭标签页等场景) function add_cart_clear_frontend_script() { $current_page_id = get_queried_object_id(); if ($current_page_id != 51) return; ?> <script> window.addEventListener('beforeunload', function() { // 发送AJAX请求清空购物车 fetch('<?php echo admin_url('admin-ajax.php'); ?>', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: 'action=clear_cart_on_unload' }); }); </script> <?php } add_action('wp_footer', 'add_cart_clear_frontend_script'); // AJAX处理函数 function clear_cart_on_unload() { if (!function_exists('WC')) wp_die(); WC()->cart->empty_cart(); unset($_SESSION['on_page_51']); wp_die(); } add_action('wp_ajax_clear_cart_on_unload', 'clear_cart_on_unload'); add_action('wp_ajax_nopriv_clear_cart_on_unload', 'clear_cart_on_unload');
关键修改说明
- 提前启动Session:在
init钩子中手动启动PHP Session,确保标记变量能正确存储。 - 更换钩子为wp_loaded:该钩子在WooCommerce购物车对象完全初始化后执行,保证
empty_cart()方法有效。 - 添加前端beforeunload事件:通过AJAX请求处理用户直接关闭页面、刷新等服务器端无法捕获的场景,确保所有离开操作都触发清空。
- AJAX权限兼容:同时添加
wp_ajax和wp_ajax_nopriv钩子,确保未登录用户也能触发清空操作。
内容的提问来源于stack exchange,提问作者Matei Anghel
相关产品推荐
相关产品推荐

