js 页面刷新location.reload…

本文介绍了JavaScript中页面刷新和替换的方法,包括location.reload和location.replace的使用细节,并探讨了它们在不同场景下的应用,比如避免POST请求时的Session过期问题。

首先介绍两个方法的语法: 
reload 方法,该方法强迫浏览器刷新当前页面。  
语法: location.reload([bForceGet])  
参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页。true, 则以 GET 方式,从服务端取最新的页面, 相当于客户端点击 F5("刷新") 

replace 方法 ,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,你不能通过“前进”和“后退”来访问已经被替换的URL。 
语法: location.replace(URL)  
在实际应用的时候,重新刷新页面的时候,我们通常使用: location.reload() 或者是 history.go(0) 来做。因为这种做法就像是客户端点F5刷新页面,所以页面的method="post"的时候,会出现“网页过期”的提示。那是因为Session的安全保护机制。可以想到: 当调用 location.reload() 方法的时候, aspx页面此时在服务端内存里已经存在, 因此必定是 IsPostback 的。如果有这种应用: 我们需要重新加载该页面,也就是说我们期望页面能够在服务端重新被创建, 我们期望是 Not IsPostback 的。这里,location.replace() 就可以完成此任务。被replace的页面每次都在服务端重新生成。你可以这么写: location.replace(location.href) 

下面是相关的应用与知识点: 
window.location.href 
window.top.location.replace("http://www.jb51.net") 
top.location.href("http://www.jb51.net") 
window.navigate ("http://www.jb51.net") 
Html: 
 
--------------------------------------- 
URL即:统一资源定位符 (Uniform Resource Locator, URL) 
完整的URL由这几个部分构成: 
scheme://host:port/path?query#fragment 
scheme:通信协议 
常用的http,ftp,maito等 
host:主机 
服务器(计算机)域名系统 (DNS) 主机名或 IP 地址。 
port:端口号 
整数,可选,省略时使用方案的默认端口,如http的默认端口为80。 
path:路径 
由零或多个"/"符号隔开的字符串,一般用来表示主机上的一个目录或文件地址。 
query:查询 
可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用"&"符号隔开,每个参数的名和值用"="符号隔开。 
fragment:信息片断 
字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。(也称为锚点.) 
对于这样一个URL 
http://www.jb51.net:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere 

我们可以用javascript获得其中的各个部分 
1, window.location.href 
整个URl字符串(在浏览器中就是完整的地址栏) 
本例返回值: http://www.jb51.net:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere 
2,window.location.protocol 
URL 的协议部分 
本例返回值:http: 
3,window.location.host 
URL 的主机部分 
本例返回值:www.jb51.net 
4,window.location.port 
URL 的端口部分 
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符 
本例返回值:"" 
5,window.location.pathname 
URL 的路径部分(就是文件地址) 
本例返回值:/fisker/post/0703/window.location.html 
6,window.location.search 
查询(参数)部分 
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值 
本例返回值:?ver=1.0&id=6 
7,window.location.hash 
锚点 
本例返回值:#imhere
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="javax.servlet.*,java.text.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="renderer" content="webkit"> <title>收入综合分析平台</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <%-- 右击菜单样式 --%> <link href="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/css/jquery.contextMenu.min.css" rel="stylesheet"/> <%-- <link href="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/css/animate.min.css" rel="stylesheet"/>--%> <link href="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/css/style.min.css" rel="stylesheet"/> <link href="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/css/skins.css" rel="stylesheet"/> <link href="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/css/ry-ui.css?v=4.6.1" rel="stylesheet"/> <%-- 图标库 --%> <link href="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/css/font-awesome.min.css" rel="stylesheet"/> <script src="${pageContext.request.contextPath}/resources/js/jquery-1.11.3.min.js?v=<%=System.currentTimeMillis()%>"></script> <script src="${pageContext.request.contextPath}/resources/bootstrap-3.3.7-dist/js/bootstrap.min.js?v=<%=System.currentTimeMillis()%>"></script> <script src="${pageContext.request.contextPath}/resources/global/global.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript"> $(function(){ $.ajax({ type: 'get', url: "dic/getUrlByUser", async: false, dataType: 'json', success: function (data) {//返回list数据并循环获取 var ht = ""; var arr = ['desktop','calculator','leanpub','map','send','bar-chart','line-chart','train','american-sign-language-interpreting','gears','user-circle-o','paper-plane','firefox']; for(var i=0;i<data.length;i++){ //循环一级菜单 ht = ht +'<li>'+ '<a href="#">' + '<i class="fa fa-'+arr[i]+'"></i> ' + '<span class="nav-label">'+data[i].mkName+'</span>' + '<span class="fa arrow"></span>' + '</a>'+ '<ul class="nav nav-second-level collapse">'; var obj = data[i].obj; if(obj[0].cdlb == ""){//该模块不存在二级菜单 for(var j=0;j<obj.length;j++){ ht = ht + '<li>\n' + '<a class="menuItem" href="'+obj[j].cdUrl+'">'+ obj[j].cdName +'</a>' + '</li>'; } ht = ht + '</ul></li>'; }else{ var cdlbArr = new Array(); for(var j=0;j<obj.length;j++){ cdlbArr.push(obj[j].cdlb); } cdlbArr = uniqueArr(cdlbArr); for(var k=0;k<cdlbArr.length;k++){ ht = ht + '<li>\n' + '<a href="#"><i class="fa fa-pied-piper"></i> '+cdlbArr[k]+'<span class="fa arrow"></span></a>\n' + '<ul class="nav nav-third-level collapse">'; for(var m=0;m<obj.length;m++){ if(cdlbArr[k] == obj[m].cdlb){ ht = ht + '<li>\n' + '<a class="menuItem" href="'+obj[m].cdUrl+'">'+ obj[m].cdName +'</a>' + '</li>'; } } ht = ht + '</ul></li>'; } } ht = ht + '</ul></li>'; } $("#side-menu").append(ht); } }); }) /* * 用于解决浏览器关闭后Cookie未失效,攻击者可在用户关闭浏览器后,通过同一cookie直接访问网站(无需重新登录),窃取用户会话; * 由于ajax是异步 使用时会造成发送不过去的现象 需要使用同步请求 * 另外 如果只是使用 window.addEventListener('beforeunload'来判断 会出现 刷野也会消除session 所以只能通过一下方法来实现 * */ window.addEventListener('beforeunload', function(e) { // 获取导航类型(替代废弃的performance.navigation.type) console.log(JSON.stringify(performance.getEntriesByType("navigation")[0].toJSON().type)) const navType = performance.getEntriesByType("navigation")[0].toJSON().type; sendLogoutRequest(navType) // 排除刷新行为,仅在离开/关闭时触发逻辑 if (navType !== 'reload') { console.log('离开页面,执行逻辑') //sendLogoutRequest() return '111'; } }); /*class PageLeaveHandler { constructor() { this.isRefreshing = false; this.pendingUnload = false; this.pendingUnloadnum = 0;//默认值 this.init(); } init() { // 监听刷新快捷键 window.addEventListener('keydown', (e) => { if (e.key === 'F5' || (e.ctrlKey && e.key === 'F5')) { this.isRefreshing = true; } }); // 监听beforeunload事件 window.addEventListener('beforeunload', this.handleBeforeUnload.bind(this)); } handleBeforeUnload(e) { if (this.isRefreshing) { this.pendingUnload = false; return; } //sendLogoutRequest(this.isRefreshing) this.pendingUnload = true; return 1; } } new PageLeaveHandler();*/ function sendLogoutRequest(navType) { const logoutUrl = '${pageContext.request.contextPath}/sys/loginoutall?navType='+navType; if (navigator.sendBeacon) { navigator.sendBeacon(logoutUrl, 'loginoutall'); } else { const xhr = new XMLHttpRequest(); xhr.open('GET', logoutUrl, false); // 同步请求 xhr.timeout = 2000; try { xhr.send(); } catch (e) { // 忽略错误 } } } /*window.addEventListener('beforeunload', function(e) { // 取消事件的默认行为(部分浏览器需要) e.preventDefault(); if(!e.isTrusted){ // 2. 拼接正确的上下文路径(关键:避免404) const logoutUrl = '${pageContext.request.contextPath}/sys/loginoutall'; // 3. 用sendBeacon发送请求,浏览器会优先保证发送完成 if (navigator.sendBeacon) { navigator.sendBeacon(logoutUrl, 'loginoutall'); } else { const xhr = new XMLHttpRequest(); xhr.open('GET', logoutUrl, false); // 同步请求 xhr.timeout = 2000; try { xhr.send(); } catch (e) { } } } });*/ </script> </head> <body class="fixed-sidebar full-height-layout gray-bg skin-blue theme-light" style="overflow: hidden"> <div id="wrapper"> <!--左侧导航开始--> <nav class="navbar-default navbar-static-side" role="navigation"> <div class="nav-close"> <i class="fa fa-times-circle"></i> </div> <a href=""> <li class="logo hidden-xs"> <span class="logo-lg">欢迎${user.username}</span> </li> </a> <div class="sidebar-collapse"> <ul class="nav" id="side-menu"> <li> <div class="user-panel"> <%-- 该位置换谁个人中心地址 --%> <a class="menuItem noactive" title="个人中心" href=""> <div class="hide" text="个人中心"></div> <div class="pull-left image"> <%--<img src="resources/ruoyi/favicon.ico" class="img-circle" alt="User Image">--%> <img src="resources/image/eee.png" class="img-circle" alt="User Image"> </div> </a> <div class="pull-left info"> <%-- 该位置可以加上el表达式 然后读取成登录名即可 --%> <p></p> <a href="#"><i class="fa fa-circle text-success"></i> 在线</a> <%-- 该位置发起退出请求 换上即可 --%> <a href="sys/loginout" style="padding-left:5px;"><i class="fa fa-sign-out text-danger"></i> 注销</a> </div> </div> </li> <li> <a class="menuItem" href="web/main/sy.jsp"><i class="fa fa-home"></i> <span class="nav-label">首页</span></a> </li> </ul> </div> </nav> <!--左侧导航结束--> <!--右侧部分开始--> <div id="page-wrapper" class="gray-bg dashbard-1"> <div class="row border-bottom"> <nav class="navbar navbar-static-top" role="navigation" style="margin-bottom: 0"> <div class="navbar-header"> <a class="navbar-minimalize minimalize-styl-2" style="color:#FFF;" href="#" title="收起菜单"> <i class="fa fa-bars"></i> </a> </div> <ul class="nav navbar-top-links navbar-right welcome-message"> <%--<li><a data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="开发文档" href="http://doc.ruoyi.vip/ruoyi" target="_blank"><i class="fa fa-question-circle"></i> 文档</a></li> <li><a data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="锁定屏幕" href="#" id="lockScreen"><i class="fa fa-lock"></i> 锁屏</a></li>--%> <li><a data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="全屏显示" href="#" id="fullScreen"><i class="fa fa-arrows-alt"></i> 全屏</a></li> <%--<li class="dropdown user-menu"> <a href="javascript:void(0)" class="dropdown-toggle" data-hover="dropdown"> <img src="resources/image/eee.png" class="user-image"> <%– 该位置用el表达式读取登录名 –%> <span class="hidden-xs">海来怡天</span> </a> <ul class="dropdown-menu"> <li class="mt5"> <%– 该位置是个人中心超链接 –%> <a href="" class="menuItem noactive"> <i class="fa fa-user"></i> 个人中心</a> </li> <li> <a onclick="resetPwd()"> <i class="fa fa-key"></i> 修改密码</a> </li> <li> <a onclick="switchSkin()"> <%–<a id="btn" >–%> <i class="fa fa-dashboard"></i> 切换主题 </a> </li> <li class="divider"></li> <li> <%– 该位置是推出登录请求 –%> <a href=""> <i class="fa fa-sign-out"></i> 退出登录</a> </li> </ul> </li>--%> </ul> </nav> </div> <div class="row content-tabs"> <button class="roll-nav roll-left tabLeft"> <i class="fa fa-backward"></i> </button> <nav class="page-tabs menuTabs"> <div class="page-tabs-content"> <a href="javascript:" class="active menuTab" data-id="web/main/sy.jsp">首页</a> </div> </nav> <button class="roll-nav roll-right tabRight"> <i class="fa fa-forward"></i> </button> <a href="javascript:void(0);" class="roll-nav roll-right tabReload"><i class="fa fa-refresh"></i> 刷新</a> </div> <a id="ax_close_max" class="ax_close_max" href="#" title="关闭全屏"> <i class="fa fa-times-circle-o"></i> </a> <div class="row mainContent" id="content-main" > <iframe class="RuoYi_iframe" name="iframe0" width="100%" height="100%" data-id="web/main/sy.jsp" src="web/main/sy.jsp" frameborder="0" seamless></iframe> </div> </div> <!--右侧部分结束--> </div> <!-- 全局js --> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/js/plugins/metisMenu/jquery.metisMenu.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/js/plugins/slimscroll/jquery.slimscroll.min.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/js/jquery.contextMenu.min.js?v=<%=System.currentTimeMillis()%>"></script> <%--<script src="${pageContext.request.contextPath}/resources/ruoyi/ajax/libs/layui/layui.js?v=<%=System.currentTimeMillis()%>"></script>--%> <%-- 该方法用于将页面加载到本页面 --%> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/ajax/libs/blockUI/jquery.blockUI.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/ajax/libs/layer/layer.min.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/js/ry-ui.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/js/common.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/ruoyi/index.js?v=<%=System.currentTimeMillis()%>"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/resources/ruoyi/ajax/libs/fullscreen/jquery.fullscreen.js?v=<%=System.currentTimeMillis()%>"></script> <script> /*window.history.forward(1); var ctx = "/zhfx"; // 皮肤缓存 var skin = storage.get("skin");*/ // history(表示去掉地址的#)否则地址以"#"形式展示 var mode = "history"; // 历史访问路径缓存 var historyPath = storage.get("historyPath"); // 是否页签与菜单联动 var isLinkage = true; /* * 这个js如果删了 按下F12编辑时 页面会不起作用 * */ $(function() { var lockPath = storage.get('lockPath'); if($.common.equals("history", mode) && window.performance.navigation.type == 1) { var url = storage.get('publicPath'); if ($.common.isNotEmpty(url)) { applyPath(url); } } else if($.common.isNotEmpty(lockPath)) { applyPath(lockPath); storage.remove('lockPath'); } else { var hash = location.hash; if ($.common.isNotEmpty(hash)) { var url = hash.substring(1, hash.length); applyPath(url); } else { if($.common.equals("history", mode)) { storage.set('publicPath', ""); } } } $("[data-toggle='tooltip']").tooltip(); }); </script> </body> </html> 为什么 一个刷新 一个跳转 这俩 点刷新reload 再点跳转还是reload 必须第二下才是navigate 但是这样点了navigate之后 再点刷新还是navigate 必须再点刷新才是reload 这可不能这样
最新发布
11-12
cart.php <?php /** * Custom Cart Page for WooCommerce with Selective Checkout * RECOMMENDED LOCATION (Woo override): * /wp-content/themes/your-child-theme/woocommerce/cart/cart.php * ALTERNATIVE (if your site is wired this way): * /wp-content/themes/woodmart-child/cart.php */ if (!defined('ABSPATH')) { exit; } do_action('woocommerce_before_cart'); // Provide tiny context for JS (invisible; no layout change) $pc_cart_is_empty = WC()->cart->is_empty(); function pc_uid_for_view() { if (is_user_logged_in()) return 'user_' . get_current_user_id(); if (empty($_COOKIE['pc_cart_uid'])) { $token = wp_generate_uuid4(); setcookie('pc_cart_uid', $token, time() + YEAR_IN_SECONDS, COOKIEPATH ?: '/', '', is_ssl(), false); $_COOKIE['pc_cart_uid'] = $token; } return 'guest_' . sanitize_text_field(wp_unslash($_COOKIE['pc_cart_uid'])); } $pc_uid = pc_uid_for_view(); ?> <div class="cart-page-section container" style="max-width: 1200px; margin: 0 auto;"> <form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post"> <?php do_action('woocommerce_before_cart_table'); ?> <?php wp_nonce_field('woocommerce-cart', 'woocommerce-cart-nonce'); ?> <table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents"> <thead> <tr> <th class="product-select" style="width: 8%;"> <input type="checkbox" id="select-all-items" /> <label for="select-all-items" style="display: inline-block; margin-left: 5px; cursor: pointer;">全选</label> </th> <th class="product-info"><?php esc_html_e('Product', 'woocommerce'); ?></th> <th class="product-price"><?php esc_html_e('Price', 'woocommerce'); ?></th> <th class="product-quantity"><?php esc_html_e('Quantity', 'woocommerce'); ?></th> <th class="product-subtotal"><?php esc_html_e('Subtotal', 'woocommerce'); ?></th> <th class="product-remove"><?php esc_html_e('操作', 'woocommerce'); ?></th> </tr> </thead> <tbody> <?php do_action('woocommerce_before_cart_contents'); ?> <?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) : ?> <?php $_product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key); $product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key); if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_cart_item_visible', true, $cart_item, $cart_item_key) ) : $product_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($cart_item) : '', $cart_item, $cart_item_key); $line_total_value = (float) ($cart_item['line_total'] + $cart_item['line_tax']); $variation_id = isset($cart_item['variation_id']) ? (int)$cart_item['variation_id'] : 0; $variation_data = isset($cart_item['variation']) ? $cart_item['variation'] : array(); $variation_json = wp_json_encode($variation_data); ?> <tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters('woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key) ); ?>" data-cart_item_key="<?php echo esc_attr($cart_item_key); ?>" data-product_id="<?php echo esc_attr($product_id); ?>" data-variation_id="<?php echo esc_attr($variation_id); ?>" data-variation="<?php echo esc_attr($variation_json); ?>"> <!-- Checkbox Column --> <td class="product-select" data-title="<?php esc_attr_e('Select', 'woocommerce'); ?>"> <input type="checkbox" class="item-checkbox" name="selected_items[]" value="<?php echo esc_attr($cart_item_key); ?>" data-price="<?php echo esc_attr($line_total_value); ?>" /> </td> <!-- Product Info Column --> <td class="product-info" data-title="<?php esc_attr_e('Product', 'woocommerce'); ?>"> <div class="product-image"> <?php $thumbnail = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key); if ( ! $product_permalink ) : echo $thumbnail; // PHPCS: XSS ok. else : printf('<a href="%s">%s</a>', esc_url($product_permalink), $thumbnail); // PHPCS: XSS ok. endif; ?> </div> <div class="product-name"> <?php if ( ! $product_permalink ) : echo wp_kses_post( apply_filters('woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key) . ' ' ); else : echo wp_kses_post( apply_filters('woocommerce_cart_item_name', sprintf('<a href="%s">%s</a>', esc_url($product_permalink), $_product->get_name()), $cart_item, $cart_item_key) ); endif; do_action('woocommerce_after_cart_item_name', $cart_item, $cart_item_key); echo wc_get_formatted_cart_item_data($cart_item); // PHPCS: XSS ok. ?> </div> </td> <!-- Price Column --> <td class="product-price" data-title="<?php esc_attr_e('Price', 'woocommerce'); ?>"> <?php echo apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key); // PHPCS: XSS ok. ?> </td> <!-- Quantity Column --> <td class="product-quantity" data-title="<?php esc_attr_e('Quantity', 'woocommerce'); ?>"> <?php if ( $_product->is_sold_individually() ) : $product_quantity = sprintf('1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key); else : $product_quantity = woocommerce_quantity_input( array( 'input_name' => "cart[{$cart_item_key}][qty]", 'input_value' => $cart_item['quantity'], 'max_value' => $_product->get_max_purchase_quantity(), 'min_value' => '0', 'product_name' => $_product->get_name(), ), $_product, false ); endif; echo apply_filters('woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item); // PHPCS: XSS ok. ?> <small class="qty-status" style="display:none;margin-left:8px;color:#666;">保存中…</small> </td> <!-- Subtotal Column --> <td class="product-subtotal" data-title="<?php esc_attr_e('Subtotal', 'woocommerce'); ?>"> <?php echo apply_filters('woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal($_product, $cart_item['quantity']), $cart_item, $cart_item_key); // PHPCS: XSS ok. ?> </td> <!-- Remove Item Column --> <td class="product-remove" data-title="<?php esc_attr_e('操作', 'woocommerce'); ?>"> <?php echo apply_filters('woocommerce_cart_item_remove_link', sprintf( '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">×</a>', esc_url( wc_get_cart_remove_url($cart_item_key) ), esc_attr__('Remove this item', 'woocommerce'), esc_attr($product_id), esc_attr($_product->get_sku()) ), $cart_item_key); ?> </td> </tr> <?php endif; ?> <?php endforeach; ?> <?php do_action('woocommerce_after_cart_contents'); ?> </tbody> </table> <?php do_action('woocommerce_after_cart_table'); ?> </form> </div> <!-- Sticky Footer --> <div class="cart-footer-actions sticky-footer" style="position: sticky; bottom: 0; background: white; padding: 15px; border-top: 1px solid #ddd; max-width: 1200px; margin: 0 auto;"> <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;"> <div class="footer-left"> <input type="checkbox" id="footer-select-all"> <label for="footer-select-all" style="display: inline-block; margin-left: 5px; cursor: pointer;">全选</label> <button type="button" class="button" id="remove-selected-items">刪除選中的商品</button> <button type="button" class="button" id="clear-cart">清空購物車</button> </div> <div class="coupon-section"> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="输入优惠券代码" style="padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; margin-right: 5px;" /> <button type="button" class="button" id="apply-coupon">应用优惠券</button> </div> </div> <div style="display: flex; justify-content: space-between; align-items: center;"> <div class="selected-summary" style="font-size: 16px; font-weight: bold;"> 已选商品: <span id="selected-count">0</span> 件,共计: <span id="selected-total">RM0.00</span> </div> <a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="checkout-button button alt wc-forward" id="partial-checkout">结算</a> </div> </div> <?php do_action('woocommerce_after_cart'); ?> <style> /* Your original styles exactly untouched */ <?php /* Keeping exactly your CSS content */ ?> .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; text-align: center; vertical-align: middle; white-space: nowrap; } .cart-page-section table.cart td.product-info { width: 37%; vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1); z-index: 1000; display: flex; flex-direction: column; gap: 15px; } .footer-left { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; } .coupon-section { display: flex; align-items: center; gap: 5px; } .selected-summary { font-size: 16px; font-weight: bold; flex: 1; } .cart-footer-actions .button { padding: 10px 20px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; white-space: nowrap; } .cart-footer-actions .button:hover { background-color: #d32f2f; } #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; transition: background-color 0.3s; white-space: nowrap; text-align: center; font-weight: bold; } #partial-checkout:hover { background-color: #0b7dda; } #coupon_code { padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; transition: border-color 0.3s; } #coupon_code:focus { border-color: #2196F3; outline: none; } @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } .cart-page-section table.cart td.product-select::before { content: "选择"; } .cart-footer-actions { flex-direction: column; align-items: flex-start; } .footer-left { width: 100%; justify-content: space-between; } .coupon-section { width: 100%; margin-top: 10px; } .coupon-section input { flex: 1; } .cart-footer-actions .footer-bottom-row { flex-direction: column; align-items: stretch; } .selected-summary { text-align: center; margin-bottom: 10px; } #partial-checkout { width: 100%; padding: 15px; } .cart-footer-actions .button { padding: 12px 15px; margin: 5px 0; width: 100%; text-align: center; } } @media (max-width: 480px) { .cart-page-section { padding: 15px; } .cart-page-section table.cart td { padding-left: 45%; } .cart-page-section table.cart td::before { font-size: 14px; } .cart-footer-actions { padding: 10px; } #coupon_code { width: 100%; } } </style> <script> jQuery(function($){ // Context var PC = { ajax_url: (window.wc_cart_params && window.wc_cart_params.ajax_url) || '<?php echo esc_url( admin_url("admin-ajax.php") ); ?>', security: (function(){ var n = $('input[name="woocommerce-cart-nonce"]').val(); if (n) return n; if (window.wc_cart_params && window.wc_cart_params.cart_nonce) return window.wc_cart_params.cart_nonce; return '<?php echo wp_create_nonce("woocommerce-cart"); ?>'; })(), cart_is_empty: <?php echo $pc_cart_is_empty ? 'true' : 'false'; ?>, cart_uid: '<?php echo esc_js($pc_uid); ?>' }; // Keys function lsKey(name){ return 'pc_' + name + '_' + PC.cart_uid; } var LS_SELECTION = lsKey('selected_items'); var LS_MASTER = lsKey('cart_master'); // Utilities function getSelectedKeys(){ return $('.item-checkbox:checked').map(function(){ return this.value; }).get(); } function fmtRM(n){ n = isNaN(n) ? 0 : n; return 'RM' + Number(n).toFixed(2); } // Selection persistence function readSelection(){ try { return JSON.parse(localStorage.getItem(LS_SELECTION) || '[]'); } catch(e){ return []; } } function writeSelection(keys){ try { localStorage.setItem(LS_SELECTION, JSON.stringify(keys||[])); } catch(e){} } function restoreSelectionFromLS(){ var saved = readSelection(); if (!Array.isArray(saved)) saved = []; $('.item-checkbox').each(function(){ $(this).prop('checked', saved.indexOf(this.value) !== -1); }); } window.addEventListener('storage', function(ev){ if (ev.key === LS_SELECTION) { restoreSelectionFromLS(); updateSelectedSummary(); } }); // Selected summary function updateSelectedSummary(){ var total = 0, count = 0; $('.item-checkbox:checked').each(function(){ var price = parseFloat($(this).data('price')); if (!isNaN(price)) { total += price; count++; } }); $('#selected-count').text(count); $('#selected-total').text(fmtRM(total)); var totalCbs = $('.item-checkbox').length; var checkedCbs = $('.item-checkbox:checked').length; var allChecked = totalCbs > 0 && checkedCbs === totalCbs; $('#select-all-items, #footer-select-all').prop('checked', allChecked); writeSelection(getSelectedKeys()); } // Select all $('#select-all-items, #footer-select-all').off('change.sc').on('change.sc', function(){ var checked = $(this).prop('checked'); $('.item-checkbox').prop('checked', checked); updateSelectedSummary(); }); $(document).on('change', '.item-checkbox', updateSelectedSummary); // Snapshot cart DOM -> localStorage (guest resilience) function snapshotCartFromDOM() { var items = []; $('tr.cart_item').each(function(){ var $row = $(this); var pid = parseInt($row.attr('data-product_id'),10)||0; var vid = parseInt($row.attr('data-variation_id'),10)||0; var qty = parseFloat($row.find('input.qty').val())||0; var variation = {}; try { variation = JSON.parse($row.attr('data-variation')||'{}'); } catch(e){ variation = {}; } if (pid && qty > 0) { items.push({ product_id: pid, variation_id: vid, variation: variation, quantity: qty }); } }); try { localStorage.setItem(LS_MASTER, JSON.stringify({ ts: Date.now(), items: items })); } catch(e){} } function maybeRehydrateFromLocal() { if (!PC.cart_is_empty) return; var raw = localStorage.getItem(LS_MASTER); if (!raw) return; var data; try { data = JSON.parse(raw); } catch(e){ return; } var items = (data && data.items) ? data.items : []; if (!items.length) return; $.ajax({ url: PC.ajax_url, method: 'POST', dataType: 'json', data: { action: 'pc_rehydrate_cart', security: PC.security, items: JSON.stringify(items) } }).done(function(res){ if (res && res.success) { // The current page is cart; reload to display rehydrated items location.reload(); } }); } // Remove selected $('#remove-selected-items').off('click.sc').on('click.sc', function(){ var keys = getSelectedKeys(); if (!keys.length) { alert('请选择要删除的商品'); return; } if (!confirm('确定要删除选中的商品吗?')) return; $.ajax({ url: PC.ajax_url, method: 'POST', dataType: 'json', data: { action: 'remove_selected_cart_items', selected_items: keys, security: PC.security } }).done(function(res){ if (res && res.success) { var saved = readSelection().filter(function(k){ return keys.indexOf(k) === -1; }); writeSelection(saved); location.reload(); } else { alert((res && res.data && (res.data.message || res.data)) || '删除失败,请重试'); } }).fail(function(){ alert('删除失败,请重试'); }); }); // Clear cart $('#clear-cart').off('click.sc').on('click.sc', function(){ if (!confirm('确定要清空购物车吗?')) return; $.ajax({ url: PC.ajax_url, method: 'POST', dataType: 'json', data: { action: 'empty_cart', security: PC.security } }).done(function(res){ if (res && res.success) { writeSelection([]); localStorage.removeItem(LS_MASTER); location.reload(); } else { alert((res && res.data && (res.data.message || res.data)) || '清空购物车失败,请重试'); } }).fail(function(){ alert('清空购物车失败,请重试'); }); }); // Apply coupon $('#apply-coupon').off('click.sc').on('click.sc', function(){ var code = ($.trim($('#coupon_code').val()) || ''); if (!code) { alert('请输入优惠券代码'); return; } var $btn = $(this).prop('disabled', true).text('处理中...'); $.ajax({ url: PC.ajax_url, method: 'POST', dataType: 'json', data: { action: 'apply_coupon', coupon_code: code, security: PC.security } }).done(function(res){ if (res && res.success) { location.reload(); } else { alert((res && res.data && (res.data.message || res.data)) || '优惠券应用失败,请重试'); $btn.prop('disabled', false).text('应用优惠券'); } }).fail(function(){ alert('发生错误,请重试'); $btn.prop('disabled', false).text('应用优惠券'); }); }); // Quantity auto-save (per-row only; no page hold) function parseCartKeyFromInputName(n){ // Matches: cart[<cart_item_key>][qty] var m = (n||'').match(/^cart$(.+?)$$qty$$/); return m ? m[1] : null; } var qtyTimers = {}; $(document).on('input change', '.qty', function(){ var $input = $(this); var key = parseCartKeyFromInputName($input.attr('name')); if (!key) return; var row = $input.closest('tr'); var $status = row.find('.qty-status'); var val = parseInt($input.val(), 10); if (isNaN(val) || val < 0) val = 0; if (qtyTimers[key]) clearTimeout(qtyTimers[key]); qtyTimers[key] = setTimeout(function(){ // Row-only loading state $status.show().text('保存中…'); $input.prop('disabled', true); $.ajax({ url: PC.ajax_url, method: 'POST', dataType: 'json', data: { action: 'update_cart_item_qty', cart_item_key: key, qty: val, security: PC.security } }).done(function(res){ if (res && res.success && res.data){ if (res.data.subtotal_html) { row.find('td.product-subtotal').html(res.data.subtotal_html); } // Update checkbox data-price used for selected total var $cb = row.find('.item-checkbox'); if ($cb.length && typeof res.data.line_total_incl_tax === 'number') { $cb.attr('data-price', res.data.line_total_incl_tax); } updateSelectedSummary(); // If removed, refresh page to remove row only then if (val === 0 || res.data.removed) { location.reload(); } else { // Re-snapshot cart for guest resilience snapshotCartFromDOM(); } } else { alert((res && res.data && (res.data.message || res.data)) || '数量更新失败,请重试'); } }).fail(function(){ alert('数量更新失败,请重试'); }).always(function(){ $status.hide(); $input.prop('disabled', false); }); }, 450); // debounce }); // Partial checkout -> regular checkout page $('#partial-checkout').off('click.sc').on('click.sc', function(e){ e.preventDefault(); var keys = getSelectedKeys(); if (!keys.length) { alert('请至少选择一件商品结算'); return; } var $btn = $(this), t = $btn.text(); $btn.prop('disabled', true).text('创建订单中...'); // Snapshot first for safety snapshotCartFromDOM(); $.ajax({ url: PC.ajax_url, method: 'POST', dataType: 'json', data: { action: 'create_direct_order', selected_items: keys, security: PC.security } }).done(function(res){ if (res && res.success && res.data && res.data.checkout_url) { window.location.href = res.data.checkout_url; // This is /checkout/?pc_token=... } else { alert((res && res.data && (res.data.message || res.data)) || '创建订单失败,请重试'); $btn.prop('disabled', false).text(t); } }).fail(function(xhr){ var msg = '创建订单失败'; if (xhr && xhr.responseJSON && xhr.responseJSON.data) { msg += ':' + (xhr.responseJSON.data.message || xhr.responseJSON.data); } alert(msg); $btn.prop('disabled', false).text(t); }); }); // Keep LS selection after removing via the "x" link and update cart snapshot $(document).on('click', 'a.remove', function(){ var key = $(this).closest('tr.cart_item').attr('data-cart_item_key'); if (key) { var saved = readSelection().filter(function(k){ return k !== key; }); writeSelection(saved); snapshotCartFromDOM(); } }); // Init restoreSelectionFromLS(); updateSelectedSummary(); snapshotCartFromDOM(); maybeRehydrateFromLocal(); }); </script> functions.php <?php defined('ABSPATH') || exit; /** * Robust partial checkout to regular /checkout/ with durable cart snapshot * - Snapshot full cart before virtualizing selection for checkout * - Do not remove anything until order is created * - On success (thank-you), rebuild cart as (snapshot - purchased) * - On cancel/back (visit cart), restore snapshot * - Guest resilience: localStorage + rehydrate AJAX */ /* ------------------------------------------------- * Helpers * ------------------------------------------------- */ function pc_get_cart_uid() { if (is_user_logged_in()) { return 'user_' . get_current_user_id(); } if (empty($_COOKIE['pc_cart_uid'])) { $token = wp_generate_uuid4(); setcookie('pc_cart_uid', $token, time() + YEAR_IN_SECONDS, COOKIEPATH ?: '/', '', is_ssl(), false); $_COOKIE['pc_cart_uid'] = $token; } return 'guest_' . sanitize_text_field(wp_unslash($_COOKIE['pc_cart_uid'])); } function pc_build_item_key($product_id, $variation_id = 0) { return (int)$product_id . '|' . (int)$variation_id; } function pc_snapshot_current_cart() { if (!WC()->cart) wc_load_cart(); $items = array(); foreach (WC()->cart->get_cart() as $ci_key => $ci) { $pid = isset($ci['product_id']) ? (int)$ci['product_id'] : 0; $vid = isset($ci['variation_id']) ? (int)$ci['variation_id'] : 0; $qty = isset($ci['quantity']) ? wc_stock_amount($ci['quantity']) : 0; $var = isset($ci['variation']) && is_array($ci['variation']) ? $ci['variation'] : array(); if ($pid && $qty > 0) { $items[] = array( 'product_id' => $pid, 'variation_id' => $vid, 'variation' => array_map('wc_clean', $var), 'quantity' => $qty, ); } } return $items; } function pc_restore_cart_from_items($items) { if (!WC()->cart) wc_load_cart(); WC()->cart->empty_cart(); foreach ((array)$items as $it) { $pid = isset($it['product_id']) ? (int)$it['product_id'] : 0; $vid = isset($it['variation_id']) ? (int)$it['variation_id'] : 0; $qty = isset($it['quantity']) ? wc_stock_amount($it['quantity']) : 0; $var = isset($it['variation']) && is_array($it['variation']) ? array_map('wc_clean', $it['variation']) : array(); if ($pid && $qty > 0) { WC()->cart->add_to_cart($pid, $qty, $vid, $var); } } WC()->cart->calculate_totals(); } function pc_transient_key($token) { return 'pc_partial_payload_' . sanitize_key($token); } /* ------------------------------------------------- * AJAX: Local rehydrate when Woo cart is empty * ------------------------------------------------- */ add_action('wp_ajax_pc_rehydrate_cart', 'pc_rehydrate_cart'); add_action('wp_ajax_nopriv_pc_rehydrate_cart', 'pc_rehydrate_cart'); function pc_rehydrate_cart() { check_ajax_referer('woocommerce-cart', 'security'); $raw = isset($_POST['items']) ? wp_unslash($_POST['items']) : ''; $items = is_string($raw) ? json_decode($raw, true) : (array)$raw; if (!is_array($items)) { wp_send_json_error(array('message' => 'Invalid items.'), 400); } if (!WC()->cart) wc_load_cart(); if (!WC()->cart->is_empty()) { wp_send_json_success(array('message' => 'Cart not empty.')); } foreach ($items as $it) { $pid = isset($it['product_id']) ? (int)$it['product_id'] : 0; $vid = isset($it['variation_id']) ? (int)$it['variation_id'] : 0; $qty = isset($it['quantity']) ? wc_stock_amount($it['quantity']) : 0; $var = isset($it['variation']) && is_array($it['variation']) ? array_map('wc_clean', $it['variation']) : array(); if ($pid && $qty > 0) { WC()->cart->add_to_cart($pid, $qty, $vid, $var); } } WC()->cart->calculate_totals(); wp_send_json_success(array('rehydrated' => true)); } /* ------------------------------------------------- * AJAX: Update qty (per-row; no page reload) * ------------------------------------------------- */ add_action('wp_ajax_update_cart_item_qty', 'pc_update_cart_item_qty'); add_action('wp_ajax_nopriv_update_cart_item_qty', 'pc_update_cart_item_qty'); function pc_update_cart_item_qty() { check_ajax_referer('woocommerce-cart', 'security'); $key = isset($_POST['cart_item_key']) ? wc_clean(wp_unslash($_POST['cart_item_key'])) : ''; $qty = isset($_POST['qty']) ? wc_stock_amount($_POST['qty']) : null; if (!$key || $qty === null) { wp_send_json_error(array('message' => 'Missing params.'), 400); } if (!WC()->cart) wc_load_cart(); if ($qty <= 0) { $removed = WC()->cart->remove_cart_item($key); WC()->cart->calculate_totals(); wp_send_json_success(array('removed' => (bool)$removed)); } else { $set = WC()->cart->set_quantity($key, $qty, true); WC()->cart->calculate_totals(); $cart_item = WC()->cart->get_cart_item($key); if (!$cart_item) { wp_send_json_error(array('message' => 'Cart item not found after update.'), 404); } $_product = $cart_item['data']; $subtotal_html = apply_filters('woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal($_product, $cart_item['quantity']), $cart_item, $key); // Use line_total + line_tax (after totals) for your checkbox data-price $line_total_incl_tax = (float)($cart_item['line_total'] + $cart_item['line_tax']); wp_send_json_success(array( 'subtotal_html' => $subtotal_html, 'line_total_incl_tax' => $line_total_incl_tax, )); } } /* ------------------------------------------------- * AJAX: Remove selected * ------------------------------------------------- */ add_action('wp_ajax_remove_selected_cart_items', 'pc_remove_selected_cart_items'); add_action('wp_ajax_nopriv_remove_selected_cart_items', 'pc_remove_selected_cart_items'); function pc_remove_selected_cart_items() { check_ajax_referer('woocommerce-cart', 'security'); $keys = isset($_POST['selected_items']) ? (array) $_POST['selected_items'] : array(); if (!WC()->cart) wc_load_cart(); foreach ($keys as $k) { $k = wc_clean(wp_unslash($k)); WC()->cart->remove_cart_item($k); } WC()->cart->calculate_totals(); wp_send_json_success(true); } /* ------------------------------------------------- * AJAX: Empty cart * ------------------------------------------------- */ add_action('wp_ajax_empty_cart', 'pc_empty_cart'); add_action('wp_ajax_nopriv_empty_cart', 'pc_empty_cart'); function pc_empty_cart() { check_ajax_referer('woocommerce-cart', 'security'); if (!WC()->cart) wc_load_cart(); WC()->cart->empty_cart(); wp_send_json_success(true); } /* ------------------------------------------------- * AJAX: Apply coupon * ------------------------------------------------- */ add_action('wp_ajax_apply_coupon', 'pc_apply_coupon'); add_action('wp_ajax_nopriv_apply_coupon', 'pc_apply_coupon'); function pc_apply_coupon() { check_ajax_referer('woocommerce-cart', 'security'); $code = isset($_POST['coupon_code']) ? wc_format_coupon_code(wp_unslash($_POST['coupon_code'])) : ''; if (!$code) { wp_send_json_error(array('message' => __('请输入优惠券代码', 'woocommerce')), 400); } if (!WC()->cart) wc_load_cart(); $applied = WC()->cart->apply_coupon($code); WC()->cart->calculate_totals(); if (is_wp_error($applied)) { wp_send_json_error(array('message' => $applied->get_error_message()), 400); } if (!$applied) { // Woo often pushes notices instead of bool; we return generic message wp_send_json_error(array('message' => __('优惠券应用失败', 'woocommerce')), 400); } wp_send_json_success(true); } /* ------------------------------------------------- * AJAX: Start partial checkout to regular checkout page * - Save snapshot + selected items in a transient keyed by token * - Put token in session * - Return /checkout/?pc_token=... * ------------------------------------------------- */ add_action('wp_ajax_create_direct_order', 'pc_create_direct_order'); add_action('wp_ajax_nopriv_create_direct_order', 'pc_create_direct_order'); function pc_create_direct_order() { check_ajax_referer('woocommerce-cart', 'security'); $selected_keys = isset($_POST['selected_items']) ? (array) $_POST['selected_items'] : array(); if (empty($selected_keys)) { wp_send_json_error(array('message' => __('请选择要结算的商品', 'woocommerce')), 400); } if (!WC()->cart) wc_load_cart(); // Snapshot full cart $snapshot = pc_snapshot_current_cart(); // Build selected items from current cart based on cart_item_key list $selected = array(); foreach (WC()->cart->get_cart() as $ci_key => $ci) { if (!in_array($ci_key, $selected_keys, true)) { continue; } $pid = (int)$ci['product_id']; $vid = (int)$ci['variation_id']; $qty = wc_stock_amount($ci['quantity']); $var = isset($ci['variation']) && is_array($ci['variation']) ? array_map('wc_clean', $ci['variation']) : array(); if ($pid && $qty > 0) { $selected[] = array( 'product_id' => $pid, 'variation_id' => $vid, 'variation' => $var, 'quantity' => $qty, ); } } if (empty($selected)) { wp_send_json_error(array('message' => __('没有可结算的商品', 'woocommerce')), 400); } $token = wp_generate_uuid4(); $payload = array( 'uid' => pc_get_cart_uid(), 'snapshot' => $snapshot, 'selected' => $selected, 'created' => time(), ); set_transient(pc_transient_key($token), $payload, 2 * DAY_IN_SECONDS); // Put token in session (used across checkout AJAX calls) if (method_exists(WC()->session, 'set')) { WC()->session->set('pc_partial_token', $token); } $checkout_url = add_query_arg('pc_token', rawurlencode($token), wc_get_checkout_url()); wp_send_json_success(array('checkout_url' => $checkout_url)); } /* ------------------------------------------------- * Virtualize cart on the regular checkout for token * - On initial checkout load with ?pc_token=... * - Ensure re-virtualization before checkout processing * - Tag order with token * - On thank-you, rebuild cart = snapshot - purchased * - On returning to cart without completing: restore snapshot * ------------------------------------------------- */ // Entering checkout with token: virtualize cart to selected items add_action('woocommerce_before_checkout_form', function() { if (!isset($_GET['pc_token'])) return; $token = sanitize_text_field(wp_unslash($_GET['pc_token'])); $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload['selected'])) return; if (!WC()->cart) wc_load_cart(); // Virtualize to selected items only pc_restore_cart_from_items($payload['selected']); // Persist token in session for all subsequent checkout AJAX calls if (method_exists(WC()->session, 'set')) { WC()->session->set('pc_partial_token', $token); } }, 1); // Safety: just-in-time re-virtualization before order processing add_action('woocommerce_before_checkout_process', function() { if (!method_exists(WC()->session, 'get')) return; $token = WC()->session->get('pc_partial_token'); if (!$token) return; $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload['selected'])) return; // Ensure cart still equals selected set (another tab might have restored) pc_restore_cart_from_items($payload['selected']); }, 1); // Tag order with token so we can resolve on thank-you add_action('woocommerce_checkout_create_order', function($order /* WC_Order */) { $token = null; if (isset($_GET['pc_token'])) { $token = sanitize_text_field(wp_unslash($_GET['pc_token'])); } elseif (method_exists(WC()->session, 'get')) { $token = WC()->session->get('pc_partial_token'); } if ($token) { $order->update_meta_data('_pc_partial_token', $token); } }, 10, 1); // On thank-you: rebuild cart as (snapshot - purchased), then cleanup add_action('woocommerce_thankyou', function($order_id) { $order = wc_get_order($order_id); if (!$order) return; $token = $order->get_meta('_pc_partial_token'); if (!$token) return; $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload['snapshot'])) { // Nothing to rebuild, just cleanup session token if (method_exists(WC()->session, 'set')) { WC()->session->set('pc_partial_token', null); } delete_transient(pc_transient_key($token)); return; } // Build purchased map pid|vid => qty $purchased = array(); foreach ($order->get_items('line_item') as $item) { $pid = (int)$item->get_product_id(); $vid = (int)$item->get_variation_id(); $qty = (int)$item->get_quantity(); $k = pc_build_item_key($pid, $vid); if (!isset($purchased[$k])) $purchased[$k] = 0; $purchased[$k] += $qty; } // Remainder = snapshot - purchased $remainder = array(); foreach ($payload['snapshot'] as $it) { $pid = (int)$it['product_id']; $vid = (int)$it['variation_id']; $qty = wc_stock_amount($it['quantity']); $var = isset($it['variation']) ? $it['variation'] : array(); $k = pc_build_item_key($pid, $vid); $take = isset($purchased[$k]) ? (int)$purchased[$k] : 0; $left = max(0, $qty - $take); if ($left > 0) { $remainder[] = array( 'product_id' => $pid, 'variation_id' => $vid, 'variation' => $var, 'quantity' => $left, ); $purchased[$k] = max(0, $take - $qty); // if over-purchased (unlikely), keep non-negative } } // Rebuild cart to remainder pc_restore_cart_from_items($remainder); // Cleanup token if (method_exists(WC()->session, 'set')) { WC()->session->set('pc_partial_token', null); } delete_transient(pc_transient_key($token)); // Also update localStorage best-effort on the thank-you page (client side) is optional; // your cart.php JS already re-snapshots on next visit. }, 20); // If user visits the cart page with an active partial token (abandoned/cancelled), restore full snapshot add_action('woocommerce_before_cart', function() { if (!method_exists(WC()->session, 'get')) return; $token = WC()->session->get('pc_partial_token'); if (!$token) return; $payload = get_transient(pc_transient_key($token)); if (empty($payload) || empty($payload['snapshot'])) return; // Restore full snapshot so cart page always shows everything pc_restore_cart_from_items($payload['snapshot']); // Keep token so user can retry checkout; or clear it if you prefer ending the flow here: // WC()->session->set('pc_partial_token', null); }, 1); I want to achieve auto-save on my quantity input box where after i edit quantity (no matter by "+" / "-" / type in number), it will auto save after 0.5 second delay. At the same time, i dont want the whole page to refresh / freeze. I want only the quantity input box and freeze / refreshed when saving. But now, it doesnt auto-save, which part went wrong?
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值