WooCommerce:编辑订单时如何修改item_cost单价并自动计算订单总金额
WooCommerce 订单商品单价直接修改解决方案
需求说明
店铺已设置全店默认售价,针对分销客户的协商专属价,需要在创建/编辑订单时直接修改单品单价,系统自动核算总金额,无需走折扣抵扣逻辑。
界面参考:
方案1:使用WooCommerce原生功能(无额外开发成本)
WooCommerce后台本身自带单品价格修改能力,操作步骤如下:
- 进入目标订单编辑页面,找到需要修改价格的商品行,点击行右侧的铅笔编辑图标
- 展开商品详情配置项后,直接修改「单价」字段为协商好的分销专属价
- 点击商品行的保存按钮,系统会自动按「新单价 × 商品数量」计算该行商品小计,整单总金额会同步更新,无需手动调整总额或设置折扣
方案2:自定义开发实现item_cost td单元格直接编辑
如果需要实现直接点击单价单元格编辑的效果,可将以下代码添加到当前激活主题的functions.php文件,或通过代码片段类插件插入生效:
// 后台订单商品行单价单元格添加双击编辑能力 add_action('admin_footer', 'wc_custom_editable_order_item_price'); function wc_custom_editable_order_item_price() { global $pagenow; // 仅在订单编辑页加载脚本 if ($pagenow === 'post.php' && get_post_type() === 'shop_order') : ?> <script> jQuery(document).ready(function($) { // 绑定单价单元格双击事件 $('.woocommerce_order_items .item_cost').on('dblclick', function() { const $costCell = $(this); const currentPrice = $costCell.text().replace(/[^0-9.]/g, ''); // 替换为数字输入框 $costCell.html(`<input type="number" step="0.01" min="0" value="${currentPrice}" style="width: 90%;padding: 2px;">`); const $priceInput = $costCell.find('input'); $priceInput.focus(); // 失焦或回车时保存修改 $priceInput.on('blur keydown', function(e) { if (e.type === 'keydown' && e.key !== 'Enter') return; const newPrice = parseFloat($priceInput.val()); if (!isNaN(newPrice) && newPrice >= 0) { // 调用WooCommerce原生保存逻辑更新价格 const $itemRow = $costCell.closest('tr.item'); const itemQty = parseInt($itemRow.find('input.quantity').val()); $itemRow.find('.edit_item').trigger('click'); setTimeout(() => { $itemRow.find('input[name*="line_subtotal"]').val((newPrice * itemQty).toFixed(2)); $itemRow.find('input[name*="line_total"]').val((newPrice * itemQty).toFixed(2)); $itemRow.find('.save_action').trigger('click'); }, 100); } }); }); }); </script> <?php endif; }
代码生效后,双击订单商品行的单价单元格即可直接输入新价格,回车或点击空白处就会自动保存并重新核算整单金额。
方案3:分销专属价自动匹配(适合高频分销场景)
如果有大量固定分销客户有专属定价,可以安装客户分级定价类WooCommerce插件,提前给对应分销客户设置好专属商品价,后续创建该客户的订单时系统会自动带入协商好的单价,无需手动修改。
内容的提问来源于stack exchange,提问作者vsilvestre22
相关产品推荐
相关产品推荐


