Problem: no response to right-click on file in Window XP

本文介绍了解决Windows XP系统中文件右键点击无响应的问题。通过在注册表HKEY_CLASSES_ROOT*shellexContextMenuHandlers路径下移除'Openwith'项来修复该问题。
Problem: no response to right-click on file in Window XP
Solution: In HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers of regedit in Window XP, remove item of 'Openwith'.
<template> <div class="dailyAddBox"> <titlepage v-if="pathStr !== '/'" :title="pathName" :query="query" /> <!-- 统计框 --> <div class="stat-box unchecked-box" :style="{ left: uncheckedBoxPosition.x + 'px', top: uncheckedBoxPosition.y + 'px' }" @mousedown="startDrag($event, 'unchecked')" @touchstart="startDrag($event, 'unchecked')"> <div class="stat-content"> <div class="stat-number">{{ uncheckedCount }}</div> <div class="stat-label">未检查</div> </div> </div> <div class="stat-box problem-box" :style="{ left: problemBoxPosition.x + 'px', top: problemBoxPosition.y + 'px' }" @mousedown="startDrag($event, 'problem')" @touchstart="startDrag($event, 'problem')"> <div class="stat-content"> <div class="stat-number">{{ problemCount }}</div> <div class="stat-label">有问题</div> </div> </div> <div class="contentBox"> <div class="selectBox"> <p class="title">同行检查人</p> <a-select :filter-option="filterOption" @change="pairingCheckNameChange" allowClear placeholder="请选择同行检查人" showSearch style="flex:1" size="small" v-model="pairingCheckNameId" > <a-select-option :disabled="checkPer==item.realname" :key="item.id" v-for="item in userList" :value="item.id"> {{item.realname}} </a-select-option> </a-select> </div> <div class="messageBox"> <p class="titleName">检查时间</p> <p class="titleCon">{{ nowTime }}</p> <p class="titleName">项目名称</p> <p class="titleCon">{{ xmmc ? xmmc : "--" }}</p> <p class="titleName">项目地址</p> <p class="titleCon">{{ xmdz ? xmdz : "--" }}</p> </div> <div class="tableBox"> <a-collapse accordion class="firstCollapse"> <a-collapse-panel v-for="(item, i) in fristdata" :key="i"> <template #header> <van-badge style="display: flex; align-items: center; width: 100%;"> <template #content v-if="item.questionStr"> {{ getBadge(item) }} </template> <div style="width: 25%">一级指标</div> <div style="width: 50%">{{ item.name }}</div> <div class="level1-counts"> <div class="count-item unchecked-count"> 未检({{ getLevel1UncheckedCount(item) }}) </div> <div class="count-item problem-count"> 问题({{ getLevel1ProblemCount(item) }}) </div> </div> </van-badge> </template> <a-collapse accordion class="secondCollapse" v-if="item.children != []"> <a-collapse-panel v-for="(item1, i1) in item.children" :key="i1"> <template #header> <van-badge style="display: flex"> <template #content v-if="item1.questionStr"> {{ getBadge(item1) }} </template> <div style="width: 30%">二级指标</div> <div style="width: 65%">{{ item1.name }}</div> </van-badge> </template> <div class="threeDatas"> <div v-if="item1.children != []" v-for="(item,idx) in item1.children" :key="idx" > <!-- <h4 >{{ item.name }}</h4> --> <div style="display: flex; align-items: center; margin-bottom: 8px;"> <a-checkbox v-model="item.checked" @change="(e) => onCheckboxChange(e, item1.children, idx,item1)" style="margin-right: 8px;"></a-checkbox> <span :class="getIndicatorClass(item)" @click="toggleExpanded(item, item1.children, idx)" style="cursor: pointer; padding: 4px 8px; border-radius: 4px; transition: all 0.2s ease;" @mouseenter="$event.target.style.backgroundColor='rgba(0,0,0,0.05)'" @mouseleave="$event.target.style.backgroundColor=''">{{ item.name }}</span> </div> <!-- 使用手风琴效果,只能同时显示一个三级指标的详细内容 --> <div class="hasProblem" v-if="item.isExpanded"> <p class="title">检查内容</p> <p class="text" style="text-align: justify;">{{ item.inspectionContents ? item.inspectionContents.replaceAll("\n","") : ""}}</p> </div> <div class="radioBox" v-if="item.isExpanded"> <van-radio-group v-model="item.radio" @change="onRadioChange"> <van-radio name="3">无问题</van-radio> <van-radio name="0">有问题</van-radio> </van-radio-group> </div> <div class="hasProblem" v-if="item.isExpanded"> <p class="title">详细描述</p> <p class="text"> <a-input type="textarea" v-model="item['problemDescription']" :maxLength="100" placeholder="请输入问题描述" rows="3" /> </p> <p class="title">问题附件</p> <p class="text"> <a-upload accept=".jpg,.png,.jpeg,.heic,.raw,.heif" :multiple="true" :headers="headers" :action="action" :file-list="item['problemAttachment']" @change="(info) => fileChange(info, item)" :beforeUpload="(fi, fil) => beforeUpload(fi, fil, item)"> <a-button> <a-icon type="upload" /> 上传附件 </a-button> </a-upload> </p> <p class="title">整改期限</p> <p class="text"> <a-date-picker v-model="item['zgqx']" style="width: 100%" @change="dateChange($event, item)" /> </p> </div> </div> </div> </a-collapse-panel> </a-collapse> <a-table v-else :pagination="false" :columns="columns" :data-source="item"> <div slot="action" slot-scope="text, record"> <a-radio-group v-model="record.ruleId"> <a-radio :style="radioStyle" :value="radioItem.id" v-for="(radioItem, ri) in record.ruleList" :key="ri">{{ radioItem.name }} </a-radio> </a-radio-group> </div> </a-table> </a-collapse-panel> </a-collapse> </div> </div> <div class="bottonBox"> <span class="botText">检查人:{{ checkPer }} </span> <van-button size="large" type="info" @click="daliyAddFn">提交2</van-button> </div> </div> </template> <script> import moment from 'moment'; import { daliyAdd, getAllItem, getDailyManageList,pairingChexkUserList } from '@/api/dailyManager'; import { getProject2Metrics, } from "@/api/assessment"; export default { data() { return { xmmc: "", xmdz: "", fristdata: [], pairingCheckNameId:undefined, pairingCheckName:undefined, pairingCheckUsername:undefined, userList:[], checkPer: localStorage.getItem('realname'), getAllItemData: [], headers: { 'X-Access-Token': localStorage.getItem('token') }, action: '/hdygwy/sys/common/upload', pathStr: this.$route.path, pathName: this.$route.meta.title ? this.$route.meta.title : '', query: {}, nowTime: moment(new Date()).format('YYYY年MM月DD日 HH:mm:ss'), // 统计框位置 uncheckedBoxPosition: { x: 0, y: 10 }, problemBoxPosition: { x: 0, y: 10 }, // 统计数据 uncheckedCount: 0, problemCount: 0, // 是否正在拖拽 isDragging: false, // 拖拽开始时的鼠标/触摸位置 startX: 0, startY: 0, // 拖拽元素的当前位置 currentX: 0, currentY: 0, // 拖拽元素的初始位置 initialX: 0, initialY: 0, // 拖拽元素的ID draggedElementId: null, // 拖拽元素的类型 (unchecked 或 problem) draggedElementType: null, }; }, mounted() { this.getAllItemFn(); this.getUserList(); this.initDragEvents(); this.initStatisticsBoxes(); // 监听窗口大小变化,重新调整统计框位置 window.addEventListener('resize', this.initStatisticsBoxes); }, beforeDestroy() { // 清理事件监听器 document.removeEventListener('mousemove', this.onDrag); document.removeEventListener('mouseup', this.onDragEnd); document.removeEventListener('touchmove', this.onDrag); document.removeEventListener('touchend', this.onDragEnd); // 清理窗口大小监听器 window.removeEventListener('resize', this.initStatisticsBoxes); }, methods: { filterOption(input, option) { return ( option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0 ); }, pairingCheckNameChange(id){ let data=this.userList.find(tar=>tar.id==id) this.pairingCheckUsername=data.username this.pairingCheckName=data.realname }, getUserList(){ this.userList = []; pairingChexkUserList().then(res=>{ this.userList=res.result }) }, getAllItemFn() { // this.getAllItemData = []; // getDailyManageList().then((res) => { // if (res.success) { // const { result } = res; // result.forEach((item) => { // console.log(item); // const { wylyfl, childrenList } = item; // childrenList.forEach((child) => { // this.getAllItemData.push({ // ...child, // wylyfl, // checked: false, // radio: '0', // zgqx: child.zgqxDate ?? '', // problemAttachment: [], // problemDescription: '' // }); // }); // }); // } // }); const params = { projectId: this.$route.query.projectId, }; this.fristdata = []; getProject2Metrics(params).then((res) => { if (res.success) { // this.spinning = false; const { // result: { list, projectInfo }, } = res; this.xmmc = projectInfo ? projectInfo.xmmc : "--"; this.xmdz = projectInfo ? projectInfo.xmdz : "--"; // 为每个三级指标添加 isExpanded 属性 list.forEach(level1 => { if (level1.children && level1.children.length > 0) { level1.children.forEach(level2 => { if (level2.children && level2.children.length > 0) { level2.children.forEach(level3 => { // 初始化时设置默认值 if (level3.checked === undefined) { level3.checked = false; } // 不设置默认的单选框状态,让用户自己选择 if (level3.radio === undefined) { level3.radio = ''; } if (level3.problemDescription === undefined) { level3.problemDescription = ''; } if (level3.problemAttachment === undefined) { level3.problemAttachment = []; } if (level3.zgqx === undefined) { level3.zgqx = ''; } // 添加展开状态属性 level3.isExpanded = false; }); } }); } }); this.fristdata = [...list]; this.$nextTick(() => { this.getStatistics(); }); } }); }, // daliyAddFn() { // const params = []; // this.getAllItemData.forEach((item) => { // if (item.checked) { // params.push({ // defaultOption: item.jgsx, // jcnr_xxms: item.xxms, // performanceId: item.id, // problemAttachment: item.problemAttachment.map((i) => i.response.message).join(','), // problemDescription: item.problemDescription, // processState: 0, // projectId: this.$route.query.projectId, // radio: item.radio, // zgqx: item.zgqx, // pairingCheckUsername:this.pairingCheckUsername, // pairingCheckName:this.pairingCheckName // }); // const _performanceId = params.at(-1).performanceId; // params.map(item => item.performanceId = _performanceId); // } // }); // daliyAdd(params).then((res) => { // if (res.success) { // this.$message.success(res.message); // // this.$router.back(); // this.$router.push({ // path: '/dailyManagerCheck', // query: { projectId: this.$route.query.projectId } // }); // } // }); // }, daliyAddFn() { const unchecked = this.uncheckedCount; const problem = this.problemCount; if (unchecked === 0 && problem === 0) { this.handleSubmit(); } else { const messageHTML = ` 未检查指标 <span style="color: red;">${unchecked}</span> 条<br> 有问题指标 <span style="color: red;">${problem}</span> 条<br><br> 注:提交后可在【检查记录】中修改 `; this.$dialog.confirm({ title: '检查未完成', message: messageHTML, messageAlign: 'left', confirmButtonText: '提交', cancelButtonText: '取消', confirmButtonColor: '#1989fa', // 蓝色 cancelButtonColor: '#ccc', // 白色 style: { '--van-dialog-confirm-button-height': '25px', '--van-dialog-cancel-button-height': '25px', '--van-dialog-confirm-button-width': '50px', '--van-dialog-cancel-button-width': '50px', '--van-dialog-padding': '20px', '--van-dialog-font-size': '14px', }, dangerouslyUseHTMLString: true, }).then(() => { this.handleSubmit(); }).catch(() => { console.log('用户取消提交'); }); } }, handleSubmit() { const params = []; this.getAllItemData.forEach((item) => { if (item.checked) { params.push({ defaultOption: item.jgsx, jcnr_xxms: item.xxms, performanceId: item.id, problemAttachment: item.problemAttachment.map((i) => i.response.message).join(','), problemDescription: item.problemDescription, processState: 0, projectId: this.$route.query.projectId, radio: item.radio, zgqx: item.zgqx, pairingCheckUsername: this.pairingCheckUsername, pairingCheckName: this.pairingCheckName }); const _performanceId = params.at(-1).performanceId; params.map(item => item.performanceId = _performanceId); } }); daliyAdd(params).then((res) => { if (res.success) { this.$message.success(res.message); this.$router.push({ path: '/dailyManagerCheck', query: { projectId: this.$route.query.projectId } }); } }); }, fileChange(info, item) { // 直接更新当前指标的文件列表 item.problemAttachment = info.fileList; }, beforeUpload(fi, fil, item) { if (fil.length > 9) { if (fi == fil.at(-1)) { this.$message.warning('最多可上传9个文件'); } return Promise.reject(); } else { if (item.problemAttachment.length + fil.length > 9) { if (fi == fil.at(-1)) { this.$message.warning('最多可上传9个文件'); } return Promise.reject(); } return true; } }, dateChange(e, item) { // 日期变化事件,item 是当前的三级指标对象 // 日期值会自动绑定到 item.zgqx,这里不需要额外处理 }, // 获取统计数据 getStatistics() { this.uncheckedCount = 0; this.problemCount = 0; // 遍历所有三级指标计算统计 this.fristdata.forEach(level1 => { if (level1.children && level1.children.length > 0) { level1.children.forEach(level2 => { if (level2.children && level2.children.length > 0) { level2.children.forEach(level3 => { // 重新定义统计逻辑: // 1. 未处理:checked = false 或者 (checked = true 但 radio 没有值) // 2. 已处理且有问题的:checked = true 且 radio = '0' // 3. 已处理且无问题的:checked = true 且 radio = '3' if (!level3.checked || (level3.checked && (level3.radio === undefined || level3.radio === ''))) { // 未处理的指标(包括未选中和已选中但未选择单选框的) this.uncheckedCount++; } else if (level3.checked && level3.radio === '0') { // 已选中且有问题的指标 this.problemCount++; } // 已选中且无问题的指标不计入任何统计 }); } }); } }); console.log('统计更新:', { uncheckedCount: this.uncheckedCount, problemCount: this.problemCount, totalChecked: this.fristdata.reduce((total, level1) => { if (level1.children && level1.children.length > 0) { return total + level1.children.reduce((level2Total, level2) => { if (level2.children && level2.children.length > 0) { return level2Total + level2.children.filter(level3 => level3.checked).length; } return level2Total; }, 0); } return total; }, 0), totalProcessed: this.fristdata.reduce((total, level1) => { if (level1.children && level1.children.length > 0) { return total + level1.children.reduce((level2Total, level2) => { if (level2.children && level2.children.length > 0) { return level2Total + level2.children.filter(level3 => level3.checked && level3.radio !== undefined && level3.radio !== '' ).length; } return level2Total; }, 0); } return total; }, 0) }); }, // 初始化拖拽事件 initDragEvents() { document.addEventListener('mousemove', this.onDrag); document.addEventListener('mouseup', this.onDragEnd); document.addEventListener('touchmove', this.onDrag, { passive: false }); document.addEventListener('touchend', this.onDragEnd); }, // 拖拽事件处理 onDrag(event) { if (!this.isDragging) return; event.preventDefault(); const clientX = event.clientX || (event.touches && event.touches[0].clientX); const clientY = event.clientY || (event.touches && event.touches[0].clientY); const deltaX = clientX - this.startX; const deltaY = clientY - this.startY; const boxWidth = 60; // 统计框宽度 const boxHeight = 60; // 统计框高度 const margin = 20; // 边距 if (this.draggedElementType === 'unchecked') { const newX = this.uncheckedBoxPosition.x + deltaX; const newY = this.uncheckedBoxPosition.y + deltaY; this.uncheckedBoxPosition.x = Math.max(margin, Math.min(window.innerWidth - boxWidth - margin, newX)); this.uncheckedBoxPosition.y = Math.max(margin, Math.min(window.innerHeight - boxHeight - margin, newY)); } else if (this.draggedElementType === 'problem') { const newX = this.problemBoxPosition.x + deltaX; const newY = this.problemBoxPosition.y + deltaY; this.problemBoxPosition.x = Math.max(margin, Math.min(window.innerWidth - boxWidth - margin, newX)); this.problemBoxPosition.y = Math.max(margin, Math.min(window.innerHeight - boxHeight - margin, newY)); } this.startX = clientX; this.startY = clientY; }, // 拖拽结束事件处理 onDragEnd() { this.isDragging = false; this.draggedElementType = null; }, // 开始拖拽 startDrag(event, type) { this.isDragging = true; this.draggedElementType = type; const clientX = event.clientX || (event.touches && event.touches[0].clientX); const clientY = event.clientY || (event.touches && event.touches[0].clientY); this.startX = clientX; this.startY = clientY; }, // 点击指标名称展开/隐藏详情(智能处理复选框状态) toggleExpanded(item, children, parentIndex) { console.log('点击指标名称:', { name: item.name, currentExpanded: item.isExpanded, currentChecked: item.checked }); if (item.isExpanded) { // 如果当前是展开状态,则隐藏 item.isExpanded = false; console.log('隐藏指标:', item.name); } else { // 如果当前是隐藏状态 if (!item.checked) { // 如果复选框未选中,则同时选中复选框 item.checked = true; console.log('自动选中复选框:', item.name); // 注意:这里不立即更新统计,因为只是选中了复选框,没有做任何实际处理 // 统计会在用户选择单选框或填写详情后更新 } else { console.log('复选框已选中,仅展开:', item.name); } // 展开当前指标(同时隐藏其他指标) children.forEach((level3, index) => { if (index !== parentIndex) { level3.isExpanded = false; } else { level3.isExpanded = true; } }); console.log('展开指标:', item.name); } }, // 复选框变化事件 onCheckboxChange(e, children, parentIndex,item1) { console.log(e.target , ';;;;;;;',item1); if (e.target.checked) { // 选中时,只设置展开状态,不自动设置单选框状态 children.forEach((level3, index) => { if (index !== parentIndex) { level3.isExpanded = false; } else { level3.isExpanded = true; } }); } else { // 取消选中时,清空该指标的所有数据 const currentItem = children[parentIndex]; currentItem.isExpanded = false; // 不清空单选框状态,保持用户之前的选择 currentItem.problemDescription = ''; // 清空问题描述 currentItem.problemAttachment = []; // 清空附件 currentItem.zgqx = ''; // 清空整改期限 currentItem.radio = '' // 取消选中时需要更新统计,因为指标回到了未处理状态 this.$nextTick(() => { this.getStatistics(); }); } // 选中时不立即更新统计,因为还没有做任何实际处理 // 统计会在用户选择单选框或填写详情后更新 }, // 单选框变化事件 onRadioChange(value) { console.log('单选框变化:', value); // 立即更新统计,不需要等待 nextTick this.getStatistics(); }, // 获取指标样式类 getIndicatorClass(item) { if (!item.checked) { return 'indicator-normal'; } else if (item.radio === '3') { return 'indicator-no-problem'; } else if (item.radio === '0') { return 'indicator-has-problem'; } return 'indicator-normal'; }, // 初始化统计框位置 initStatisticsBoxes() { this.$nextTick(() => { // 确保统计框不超出屏幕边界 const boxWidth = 60; // 统计框宽度 const boxHeight = 60; // 统计框高度 const margin = 20; // 增加边距,防止超出屏幕 const spacing = 25; // 两个框之间的间距 // 第一个框放在右上角,确保不超出屏幕 this.uncheckedBoxPosition = { x: Math.max(margin, window.innerWidth - boxWidth - margin), y: margin }; // 第二个框放在第一个框下方,增加间距避免重叠 this.problemBoxPosition = { x: Math.max(margin, window.innerWidth - boxWidth - margin), y: margin + boxHeight + spacing }; }); }, // 获取徽章显示内容 getBadge(item) { if (null == item.questionStr) return; if ( item.questionStr && item.questionStr.split(",").includes("1") && item.questionStr.split(",").includes("2") ) { return "存在扣分和未检查"; } else { if (item.questionStr && item.questionStr.split(",").includes("1")) { return "存在扣分项"; } else { return "存在未检查项"; } } }, // 获取一级指标下未处理的三级指标数量 getLevel1UncheckedCount(level1Item) { let count = 0; if (level1Item.children && level1Item.children.length > 0) { level1Item.children.forEach(level2 => { if (level2.children && level2.children.length > 0) { level2.children.forEach(level3 => { // 与主统计逻辑保持一致: // 未处理:checked = false 或者 (checked = true 但 radio 没有值) if (!level3.checked || (level3.checked && (level3.radio === undefined || level3.radio === ''))) { count++; } }); } }); } return count; }, // 获取一级指标下有问题的三级指标数量 getLevel1ProblemCount(level1Item) { let count = 0; if (level1Item.children && level1Item.children.length > 0) { level1Item.children.forEach(level2 => { if (level2.children && level2.children.length > 0) { level2.children.forEach(level3 => { if (level3.checked && level3.radio === '0') { count++; } }); } }); } return count; }, // 获取当前展开的三级指标的索引 getActiveThirdLevel(children) { for (let i = 0; i < children.length; i++) { if (children[i].isExpanded) { return i; } } return -1; // 如果没有展开的,返回-1 } }, watch: { fristdata: { handler() { this.getStatistics(); }, deep: true } }, computed: { // 计算所有三级指标 allThirdLevelIndicators() { const indicators = []; this.fristdata.forEach(level1 => { if (level1.children && level1.children.length > 0) { level1.children.forEach(level2 => { if (level2.children && level2.children.length > 0) { level2.children.forEach(level3 => { indicators.push(level3); }); } }); } }); return indicators; } } }; </script> <style lang="less" scoped> .dailyAddBox { height: 100%; background: #f1f3f3; width: 100%; position: relative; font-size: 16px; .text { font-size: 14px; color: #999; } p { margin: 0; padding: 0; } /deep/ .bottonBox { height: 12vh; background: #fff; width: 100%; overflow: hidden; text-align: center; position: absolute; box-shadow: 0 -2px 10px #ccc; .botText { font-size: 14px; display: block; margin-top: 5px; } .van-button--large { margin: 0 -10px; border-radius: 10px; width: 87%; font-size: 15px; background: #3b77b3; margin-top: 15px; } } /deep/ .contentBox { margin: 10px; padding: 10px; background: #fff; height: calc(100% - 170px); overflow-y: scroll; .selectBox{ display: flex; align-items: center; margin-top: 18px; margin-bottom: 5px; .title{ margin: 0; margin-right: 10px; } } .radioBox { display: flex; margin-bottom: 20px; margin-top: 20px; padding-left: 0.75rem; font-size: .36rem; .van-radio-group { display: flex; .van-radio { margin-right: 20px; .van-radio__label { color: #666; font-weight: 550; } } } } .hasProblem { padding-left: .75rem !important; } .ant-divider-horizontal { margin: 0; } .title { margin: 0; color: #000; margin-top: 18px; margin-bottom: 5px; } .text { margin-bottom: 5px; } } } .messageBox { margin-bottom: 20px; .titleName { font-weight: 550; margin-top: 18px; margin-bottom: 5px; } .titleCon { color: #999; font-size: 14px; border-bottom: 1px solid #ccc; padding-bottom: 5px; } } /deep/ .firstCollapse>.ant-collapse-item>.ant-collapse-header { color: #fff; background: #3b77b3; .van-badge { display: flex !important; align-items: center !important; width: 100% !important; .van-badge__content { margin-right: 8px; } } } /deep/ .secondCollapse>.ant-collapse-item>.ant-collapse-header { color: #000; background: #eee; } /deep/ .thirdCollapse>.ant-collapse-item>.ant-collapse-header { color: #000; background: #fff; } /deep/ .ant-collapse-content>.ant-collapse-content-box { padding: 0 !important; } /deep/ .ant-table .ant-table-row-indent+.ant-table-row-expand-icon { display: none; } .threeDatas { padding: 10px; padding-left: 15px; } /* 一级指标数量提示样式 */ .level1-counts { display: flex; flex-direction: column; align-items: flex-end; margin-left: auto; font-size: 11px; line-height: 1.1; min-width: 60px; .count-item { padding: 1px 4px; border-radius: 3px; margin: 1px 0; white-space: nowrap; text-align: center; font-weight: 500; } .unchecked-count { color: #fff; background-color: rgba(255, 255, 255, 0.25); border: 1px solid rgba(255, 255, 255, 0.3); } .problem-count { color: #fff; background-color: rgba(255, 255, 0, 0.4); border: 1px solid rgba(255, 255, 0, 0.5); } } /* 统计框样式 */ .stat-box { position: fixed; width: 60px; height: 60px; background-color: rgba(255, 255, 255, 0.7); border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15); display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: grab; user-select: none; transition: transform 0.2s ease-in-out; z-index: 1000; &:active { cursor: grabbing; transform: scale(1.05); } .stat-content { text-align: center; } .stat-number { font-size: 18px; font-weight: bold; margin-bottom: 3px; } .stat-label { font-size: 10px; color: #666; } } .unchecked-box { background-color: rgba(249, 249, 249, 0.7); color: #999; &:hover { background-color: rgba(240, 247, 255, 0.85); } .stat-number { color: #999; } } .problem-box { background-color: rgba(255, 240, 240, 0.7); color: #ff6b6b; &:hover { background-color: rgba(255, 251, 230, 0.85); } .stat-number { color: #ff4d4f; } } /* 指标颜色样式 */ .indicator-normal { color: #333; } .indicator-no-problem { color: #52c41a; font-weight: 500; } .indicator-has-problem { color: #ff4d4f; font-weight: 500; } </style>点击提交报错dailyManagerAdd.vue:402 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'success') at eval (dailyManagerAdd.vue:402:1)
08-26
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>积分商城网厅常见问题分类配置</title> <script src="../js/jquery-1.min.js"></script> <script src="../js/bootstrap.min.js"></script> <link href="../css/bootstrap.min.css" rel="stylesheet"> <script src="../js/lay.js"></script> <!-- 添加 TinyMCE 引用 --> <script src="../js/tinymce/tinymce.min.js"></script> <style> /* 添加编辑器样式调整 */ .tox-tinymce { border-radius: 4px !important; border: 1px solid #dcdfe6 !important; } /* 隐藏原始文本区域 */ #content { display: none; } </style> </head> <style> body { font-family: "Microsoft YaHei", Arial, sans-serif; max-width: 1400px; margin: 30px auto; padding: 20px; background-color: #f8f9fa; } .hidden-id { display: none; } .form-container { background-color: white; padding: 40px; border-radius: 10px; box-shadow: 0 2px 12px rgba(0,0,0,0.08); } .form-section { border: 1px solid #ebeef5; padding: 20px; margin-bottom: 25px; border-radius: 8px; background: #fafafa; } .form-section h3 { color: #409eff; margin: 0 0 20px 0; padding-bottom: 10px; border-bottom: 1px solid #ebeef5; font-size: 16px; } .table { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .table th, .table td { padding: 12px 15px; border: 1px solid #ddd; text-align: left; } .table th { background-color: #f5f5f5; font-weight: 600; } .table tr:nth-child(even) { background-color: #f9f9f9; } .table tr:hover { background-color: #f1f1f1; } .btn { padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 14px; border: none; transition: all 0.3s; display: inline-flex; align-items: center; justify-content: center; } .btn-primary { background-color: #409eff; color: white; } .btn-primary:hover { background-color: #66b1ff; } .btn-success { background-color: #67c23a; color: white; } .btn-success:hover { background-color: #85ce61; } .btn-danger { background-color: #f56c6c; color: white; } .btn-danger:hover { background-color: #f78989; } .btn-sm { padding: 4px 8px; font-size: 12px; } .modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0,0,0,0.4); } .modal-content { background-color: #fefefe; margin: 10% auto; padding: 20px; border: 1px solid #888; width: 50%; border-radius: 8px; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; cursor: pointer; } .close:hover { color: black; } .form-group { margin-bottom: 15px; } .form-group label { display: block; margin-bottom: 5px; font-weight: 600; color: #606266; } .form-control { width: 100%; padding: 8px 12px; border: 1px solid #dcdfe6; border-radius: 4px; box-sizing: border-box; } textarea.form-control { height: auto; min-height: 100px; } .required label::before { content: "*"; color: #f56c6c; margin-right: 4px; } .child-node { padding-left: 30px; background-color: #f9f9f9; vertical-align: middle; } .submit-btn { display: block; width: 200px; margin: 20px auto 0; padding: 10px; background: #409eff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: all 0.3s; } .submit-btn:hover { background: #66b1ff; } .action-buttons { text-align: left; /* 内容左对齐 */ } .action-buttons button { display: inline-block; margin-right: 5px; /* 按钮之间的间距 */ vertical-align: middle; /* 垂直居中 */ } .table th:nth-child(4), .table td:nth-child(4) { width: 15%; /* 与表头设置一致 */ min-width: 180px; /* 设置最小宽度防止内容挤压 */ white-space: nowrap; /* 防止内容换行 */ } </style> <body> <div class="form-container"> <div class="form-section"> <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;"> <h3 style="margin: 0 auto;">常见问题分类配置</h3> <button id="addTopCategory" class="btn btn-primary">添加一级分类</button> </div> <table class="table" id="categoryTable"> <thead> <tr> <th width="15%">分类名称</th> <th width="50%">分类内容</th> <th width="5%">排序</th> <th width="30%">操作</th> </tr> </thead> <tbody id="categoryList"> <!-- 动态加载数据 --> </tbody> </table> </div> </div> <!-- 添加/编辑分类的模态框 --> <div id="categoryModal" class="modal"> <div class="modal-content"> <span class="close">×</span> <h3 id="modalTitle">添加分类</h3> <form id="categoryForm" novalidate> <input type="hidden" id="id" name="id"> <input type="hidden" id="pid" name="pid" value="0"> <div class="form-group required"> <label for="name">分类名称:</label> <input type="text" class="form-control" id="name" name="name" placeholder="请输入分类名称,同级别不允许重复" required> </div> <div class="form-group required" id="contentGroup"> <label for="content">分类内容:</label> <textarea class="form-control" id="content" name="content" placeholder="请输入分类描述" required></textarea> </div> <div class="form-group required"> <label for="orderNum">排序顺序: <span style="color: #888; font-size: 12px; margin-left: 5px;">值越小越靠前,请输入正整数</span> </label> <input type="number" class="form-control" id="orderNum" name="orderNum" placeholder="请输入分类排序,同级别不允许重复" required min="1" step="1"> </div> <button type="submit" class="submit-btn">保存</button> </form> </div> </div> <script> // 动态设置 required 的函数 function setContentRequired(required) { if (required) { $('#content').attr('required', 'required'); $("#contentGroup").removeClass("hidden"); tinymce.get('content').show(); $('#content').css({visibility: 'visible', position: 'static'}); // 移除隐藏样式 } else { $('#content').removeAttr('required'); $("#contentGroup").addClass("hidden"); tinymce.get('content').hide(); $('#content').css({visibility: 'hidden', position: 'absolute', left: '-9999px'}); // 恢复隐藏样式 } } $(document).ready(function() { // 初始化 TinyMCE tinymce.init({ license_key: 'gpl', selector: '#content', language: 'zh_CN', language_url: '../js/tinymce/langs/zh_CN.js', height: 300, plugins: 'link lists image table code help wordcount', toolbar: 'undo redo | formatselect | bold italic | alignleft aligncenter alignright | bullist numlist outdent indent | link image | code help', skin: 'oxide', content_css: 'default', menubar: false, branding: false, relative_urls: false, remove_script_host: false, // 自定义图片上传处理 images_upload_handler: function(blobInfo, progress) { return new Promise((resolve, reject) => { // 创建FormData对象 const formData = new FormData(); formData.append('file', blobInfo.blob(), blobInfo.filename()); // 显示上传进度 progress(0); if (blobInfo.blob().size > 2 * 1024 * 1024) { // 2MB限制 $.MsgBox.Alert("温馨提示","文件大小不能超过2MB"); return; } const allowedTypes = ['image/jpeg', 'image/png', 'image/gif']; if (!allowedTypes.includes(blobInfo.blob().type)) { $.MsgBox.Alert("温馨提示","只支持JPEG、PNG、GIF格式的图片"); return; } // 使用jQuery AJAX进行上传 $.ajax({ url: '../point/uploadFileNew?limit=010', type: 'POST', data: formData, contentType: false, processData: false, xhr: function() { const xhr = new XMLHttpRequest(); xhr.upload.addEventListener('progress', function(e) { if (e.lengthComputable) { progress((e.loaded / e.total) * 100); } }); return xhr; }, success: function(response) { if (response.state === '200') { progress(100); resolve(response.filePath); // 返回图片URL } else { $.MsgBox.Alert("温馨提示", response.msg || "未知错误"); } }, error: function(xhr) { $.MsgBox.Alert("温馨提示", "网络错误:" + xhr.statusText); } }); }); }, setup: function(editor) { editor.on('init', function() { console.log('TinyMCE 初始化成功'); }); editor.on('error', function(e) { console.error('TinyMCE 错误:', e); }); editor.on('change', function() { // 内容变化时的逻辑(如实时保存) }); }, valid_elements: 'p,br,strong/b,em/i,u,ol,ul,li,h1,h2,h3,h4,a[href|target=_blank],img[src|alt|title|width|height|class]', valid_children: '-p[strong/b|em/i|u|a|img],-li[p|ol|ul]', extended_valid_elements: '', invalid_elements: 'script,iframe,object,embed,form,input,textarea,button,select,option,style', content_security_policy: "default-src 'self'; img-src 'self' data:; media-src 'none'; script-src 'none';", forced_root_block: 'p', force_br_newlines: false, convert_newlines_to_brs: false, remove_linebreaks: false, // 允许的CSS类 valid_classes: { '*': 'text-left,text-center,text-right,text-justify' }, // 粘贴过滤设置 paste_as_text: false, paste_postprocess: function(editor, node) { // 额外的粘贴后处理 } }); // 加载分类数据 loadCategoryData(); // 打开添加一级分类模态框 $("#addTopCategory").click(function() { $("#modalTitle").text("添加一级分类"); $("#categoryForm")[0].reset(); $("#id").val(""); $("#pid").val("0"); $("#content").removeAttr("required"); $("#contentGroup").addClass("hidden"); tinymce.get('content').hide(); $("#categoryModal").show(); setContentRequired(false); }); // 添加 orderNum 输入限制 $("#orderNum").on('input', function() { let value = parseInt($(this).val()) || 0; if (value < 1) { $(this).val(1); } }); // 关闭模态框 $(".close").click(function() { $("#categoryModal").hide(); tinymce.get('content').setContent(''); }); // 点击模态框外部关闭 $(window).click(function(event) { if (event.target == $("#categoryModal")[0]) { $("#categoryModal").hide(); tinymce.get('content').setContent(''); } }); // 提交表单 $("#categoryForm").submit(function(e) { e.preventDefault(); // 验证分类名称不能为空 var name = $("#name").val().trim(); if (name === '') { $.MsgBox.Alert("提示", "分类名称不能为空"); $("#name").focus(); // 聚焦到输入框 return; } // 验证分类内容(如果是必填的) if ($("#content").is(':required') && tinymce.get('content').getContent().trim() === '') { $.MsgBox.Alert("提示", "分类内容不能为空"); tinymce.get('content').focus(); // 聚焦到编辑器 return; } // 同步编辑器内容到 textarea var content = tinymce.get('content').getContent(); $('#content').val(content); saveCategory(); }); }); // 加载分类数据 function loadCategoryData() { $.ajax({ url: "../problemClassification/list", type: "GET", dataType: "json", success: function(data) { renderCategoryTable(data); }, error: function() { $.MsgBox.Alert("提示", "加载分类数据失败"); } }); } // 渲染分类表格 function renderCategoryTable(data) { var html = ""; $.each(data, function(index, item) { html += '<tr data-id="' + item.id + '">'; html += ' <td class="hidden-id">' + item.id + '</td>'; // 隐藏的ID列 html += ' <td>' + item.name + '</td>'; html += ' <td>' + (item.content || '') + '</td>'; // 新增内容列 html += ' <td>' + (item.orderNum || 0) + '</td>'; html += ' <td class="action-buttons">'; html += ' <button class="btn btn-primary btn-sm edit-btn" data-id="' + item.id + '">编辑</button>'; html += ' <button class="btn btn-success btn-sm add-child-btn" data-id="' + item.id + '">添加子类</button>'; html += ' <button class="btn btn-danger btn-sm delete-btn" data-id="' + item.id + '">删除</button>'; html += ' </td>'; html += '</tr>'; // 添加子分类 if (item.chilsNode && item.chilsNode.length > 0) { $.each(item.chilsNode, function(i, child) { html += '<tr class="child-node" data-id="' + child.id + '">'; html += ' <td class="hidden-id">' + child.id + '</td>'; // 隐藏的ID列 html += ' <td>└ ' + child.name + '</td>'; html += ' <td>' + (child.content || '') + '</td>'; // 新增内容列 html += ' <td>' + (child.orderNum || 0) + '</td>'; html += ' <td class="action-buttons">'; html += ' <button class="btn btn-primary btn-sm edit-btn" data-id="' + child.id + '">编辑</button>'; html += ' <button class="btn btn-danger btn-sm delete-btn" data-id="' + child.id + '">删除</button>'; html += ' </td>'; html += '</tr>'; }); } }); $("#categoryList").html(html); // 绑定按钮事件 $(".edit-btn").click(function() { var id = $(this).data("id"); editCategory(id); }); // 添加子分类按钮点击事件 $(".add-child-btn").click(function() { var pid = $(this).data("id"); $("#modalTitle").text("添加子分类"); $("#categoryForm")[0].reset(); $("#id").val(""); $("#pid").val(pid); $("#content").attr("required", true); $("#contentGroup").removeClass("hidden"); tinymce.get('content').show(); // 显示编辑器 $("#categoryModal").show(); setContentRequired(true); }); $(".delete-btn").click(function() { var id = $(this).data("id"); $.MsgBox.Confirm("提示", "确定要删除这个分类吗?", function() { deleteCategory(id); }); }); } // 编辑分类 function editCategory(id) { $("#modalTitle").html("编辑分类 <small>(加载中...)</small>"); // 先确保编辑器实例存在 if (!tinymce.get('content')) { console.error('TinyMCE 编辑器未初始化'); $.MsgBox.Alert("温馨提示", "编辑器初始化失败,请刷新页面重试"); return; } $.ajax({ url: "../problemClassification/get/" + id, type: "GET", dataType: "json", success: function(data) { $("#modalTitle").text("编辑分类"); $("#id").val(data.id); $("#name").val(data.name); $("#orderNum").val(data.orderNum); $("#pid").val(data.pid); // 确保编辑器可见性正确 if(data.pid == 0) { setContentRequired(false); } else { setContentRequired(true); } // 延迟设置内容以确保编辑器就绪 setTimeout(function() { try { tinymce.get('content').setContent(data.content || ''); $("#categoryModal").show(); } catch(e) { console.error('设置编辑器内容失败:', e); $.MsgBox.Alert("温馨提示", "编辑器内容设置失败,请重试"); } }, 100); }, error: function() { $.MsgBox.Alert("温馨提示", "获取分类信息失败"); } }); } // 保存分类 function saveCategory() { // 获取提交按钮,并禁用 + 修改文字 var $submitBtn = $("#categoryForm").find("button[type='submit']"); $submitBtn.prop("disabled", true).text("保存中..."); // 强制同步 TinyMCE 内容到 textarea tinymce.get('content').save(); // 自定义验证 if ($("#content").is(':required') && tinymce.get('content').getContent().trim() === '') { $.MsgBox.Alert("温馨提示", '分类内容不能为空'); $submitBtn.prop("disabled", false).text("保存"); // 恢复按钮状态 return false; } // 新增 orderNum 校验 const orderNum = parseInt($("#orderNum").val()); if (isNaN(orderNum) || orderNum < 1) { $.MsgBox.Alert("温馨提示", "排序顺序必须为正整数"); $submitBtn.prop("disabled", false).text("保存"); // 恢复按钮状态 return false; } var formData = $("#categoryForm").serialize(); var url = $("#id").val() ? "../problemClassification/update" : "../problemClassification/add"; $.ajax({ url: url, type: "POST", data: formData, success: function(response) { if (response === "添加成功" || response === "更新成功") { $("#categoryModal").hide(); loadCategoryData(); } else { $.MsgBox.Alert("温馨提示", response); } }, error: function() { $.MsgBox.Alert("温馨提示", "操作失败"); }, complete: function() { // 无论成功或失败,最终恢复按钮状态 $submitBtn.prop("disabled", false).text("保存"); } }); } // 删除分类 function deleteCategory(id) { $.ajax({ url: "../problemClassification/delete/" + id, type: "POST", success: function(response) { if(response === "删除成功") { loadCategoryData(); } else { $.MsgBox.Alert("温馨提示", response); } }, error: function() { $.MsgBox.Alert("温馨提示", "删除失败"); } }); } </script> </body> </html> 分析一下,图片上传之后会替换content中的img url以及编辑的时候回显和列表页回显吗
06-19
on functions.php, it says Your PHP code changes were not applied due to an error on line 82 of file wp-content/themes/woodmart-child/functions.php. Please fix and try saving again. Uncaught TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given in wp-content/themes/woodmart-child/functions.php:82 Stack trace: #0 wp-content/themes/woodmart-child/functions.php(82): method_exists() #1 wp-includes/class-wp-hook.php(324): {closure}() #2 wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #3 wp-includes/plugin.php(517): WP_Hook->do_action() #4 wp-settings.php(749): do_action() #5 wp-config.php(105): require_once(‘/home/u18285596…’) #6 wp-load.php(50): require_once(‘/home/u18285596…’) #7 wp-admin/admin.php(35): require_once(‘/home/u18285596…’) #8 wp-admin/theme-editor.php(10): require_once(‘/home/u18285596…’) #9 {main} thrown cart.php <?php /** * Custom Cart Page for WooCommerce with Selective Checkout */ if (!defined('ABSPATH')) { exit; } do_action('woocommerce_before_cart'); // Provide context for JS (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(); // 已修正:仅在令牌存在时恢复购物车 if (method_exists(WC()->session, 'get')) { $token = WC()->session->get('pc_partial_token'); if ($token && !WC()->cart->is_empty()) { $payload = get_transient(pc_transient_key($token)); if (!empty($payload['snapshot'])) { pc_restore_cart_from_items($payload['snapshot']); WC()->cart->calculate_totals(); } } } ?> <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> /* Layout styles (unchanged) */ .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; } /* Responsive (unchanged) */ @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%; } } /* Loader overlay above quantity input */ .product-quantity .quantity { position: relative; /* anchor for overlay */ } .quantity-saving-overlay { position: absolute; top: 0; left: 0; right: 0; height: 100%; display: none; /* hidden by default */ align-items: flex-start; /* appear at top */ justify-content: center;/* centered horizontally */ padding-top: 2px; background: rgba(255,255,255,0.0); /* transparent so it doesn't dim UI */ z-index: 10; pointer-events: none; /* don't block typing/clicks when visible */ } .quantity-saving-loader { width: 20px; height: 20px; border: 3px solid #f3f3f3; border-top: 3px solid #3498db; border-radius: 50%; animation: spin 0.8s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </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) { location.reload(); // display rehydrated items } }); } // AFTER $('#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) { keys.forEach(function(k){ var $row = $('tr.cart_item[data-cart_item_key="'+k+'"]'); $row.fadeOut(250, function(){ $(this).remove(); snapshotCartFromDOM(); updateSelectedSummary(); }); }); // 从本地选择集合中剔除已删除项 var saved = readSelection().filter(function(k0){ return keys.indexOf(k0) === -1; }); writeSelection(saved); } else { alert((res && res.data && (res.data.message || res.data)) || '删除失败,请重试'); } }).fail(function(){ alert('删除失败,请重试'); }); }); // Clear cart // AFTER $('#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 // AFTER $('#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 (fixed regex + loader above input) ---- // 已修正:正确的正则表达式 function parseCartKeyFromInputName(n) { var m = (n || '').match(/^cart\[([a-zA-Z0-9_]+)\]\[qty\]$/); return m ? m[1] : null; } function ensureLoader($quantityContainer){ var $overlay = $quantityContainer.find('.quantity-saving-overlay'); if (!$overlay.length) { $overlay = $('<div class="quantity-saving-overlay"><div class="quantity-saving-loader"></div></div>'); $quantityContainer.append($overlay); } return $overlay; } var qtyTimers = {}; $(document).on('input change', 'input.qty', function(){ var $input = $(this); var key = parseCartKeyFromInputName($input.attr('name')); if (!key) return; var $row = $input.closest('tr.cart_item'); var $quantityContainer = $input.closest('.quantity'); var val = parseInt($input.val(), 10); if (isNaN(val) || val < 0) val = 0; // debounce if (qtyTimers[key]) clearTimeout(qtyTimers[key]); var $overlay = ensureLoader($quantityContainer); $overlay.hide(); qtyTimers[key] = setTimeout(function(){ $overlay.show(); $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); } 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); } if (val === 0 || res.data.removed) { $row.fadeOut(300, function(){ $(this).remove(); snapshotCartFromDOM(); updateSelectedSummary(); }); } else { snapshotCartFromDOM(); updateSelectedSummary(); } } else { var msg = (res && res.data && (res.data.message || res.data)) || '数量更新失败'; alert(msg); } }).fail(function(){ alert('数量更新失败,请重试'); }).always(function(){ $overlay.hide(); $input.prop('disabled', false); }); }, 500); // 0.5s 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('创建订单中...'); 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; // /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 clicking "x" remove; also 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(); }); // NEW: Real-time cart count updater function updateHeaderCartCount() { $.ajax({ url: PC.ajax_url, method: 'POST', data: { action: 'get_cart_count', security: PC.security }, success: function(response) { if (response.success) { $('.cart-count').text(response.count); } } }); } // Update on cart changes $(document).on('cart_updated', function() { updateHeaderCartCount(); }); // Initial update updateHeaderCartCount(); }); </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 * ------------------------------------------------- */ /** * 新增:购物车数量AJAX端点 */ add_action('wp_ajax_get_cart_count', 'pc_get_cart_count'); add_action('wp_ajax_nopriv_get_cart_count', 'pc_get_cart_count'); function pc_get_cart_count() { check_ajax_referer('woocommerce-cart', 'security'); $count = WC()->cart->get_cart_contents_count(); wp_send_json_success(array('count' => $count)); } 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(); } // Fix cart restoration logic add_action('wp_loaded', function() { if (!method_exists(WC()->session, 'get')) return; $token = WC()->session->get('pc_partial_token'); if ($token && WC()->cart->is_empty()) { $payload = get_transient(pc_transient_key($token)); if (!empty($payload['snapshot'])) { pc_restore_cart_from_items($payload['snapshot']); WC()->cart->calculate_totals(); } } }, 20); 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 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, 'removed' => false, )); } } /* ------------------------------------------------- * 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) { wp_send_json_error(array('message' => __('优惠券应用失败', 'woocommerce')), 400); } wp_send_json_success(true); } /* ------------------------------------------------- * AJAX: Start partial checkout to regular checkout page * ------------------------------------------------- */ 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 checkout for token and rebuild after purchase * ------------------------------------------------- */ // 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 pc_restore_cart_from_items($payload['selected']); }, 1); // Tag order with token add_action('woocommerce_checkout_create_order', function($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); $order->update_meta_data('_pc_cart_snapshot', $token); } }, 10, 1); // Fixed: Only remove purchased items from cart 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'])) { if (method_exists(WC()->session, 'set')) { WC()->session->set('pc_partial_token', null); } delete_transient(pc_transient_key($token)); return; } // 1. Restore FULL snapshot (all items) pc_restore_cart_from_items($payload['snapshot']); WC()->cart->calculate_totals(); // 2. Remove only the purchased (selected) items foreach ($payload['selected'] as $selected_item) { $cart_item_key = pc_find_cart_item($selected_item['product_id'], $selected_item['variation_id']); if ($cart_item_key) { WC()->cart->remove_cart_item($cart_item_key); } } // 3. Update cart totals WC()->cart->calculate_totals(); // Cleanup token if (method_exists(WC()->session, 'set')) { WC()->session->set('pc_partial_token', null); } delete_transient(pc_transient_key($token)); }, 20); // Helper: Find cart item by product and variation ID function pc_find_cart_item($product_id, $variation_id = 0) { if (!WC()->cart) wc_load_cart(); foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { $cart_pid = isset($cart_item['product_id']) ? (int)$cart_item['product_id'] : 0; $cart_vid = isset($cart_item['variation_id']) ? (int)$cart_item['variation_id'] : 0; if ($cart_pid == $product_id && $cart_vid == $variation_id) { return $cart_item_key; } } return false; } // Visiting cart with active token: restore full snapshot (cancel/back) 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']); }, 1); /* ------------------------------------------------- * Keep cart count accurate during checkout process * ------------------------------------------------- */ add_filter('woocommerce_cart_contents_count', function($count) { // Check if partial token exists if (!method_exists(WC()->session, 'get')) return $count; $token = WC()->session->get('pc_partial_token'); if ($token) { $payload = get_transient(pc_transient_key($token)); // Always show full cart count even during checkout if (!empty($payload['snapshot'])) { $snapshot_count = 0; foreach ($payload['snapshot'] as $item) { $snapshot_count += (int)$item['quantity']; } return $snapshot_count; } } return $count; }); // Ensure cart item totals are accurate in header add_action('woocommerce_before_cart', function() { pc_maintain_cart_consistency(); }); add_action('woocommerce_before_checkout_form', function() { pc_maintain_cart_consistency(); }); function pc_maintain_cart_consistency() { 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; // Immediately restore full cart for consistent UI pc_restore_cart_from_items($payload['snapshot']); WC()->cart->calculate_totals(); }
09-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值