You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce扣减库存时如何阻止date_modified字段自动更新

问题根因

WooCommerce核心逻辑中,所有触发商品数据保存的操作(包括下单扣减库存)默认都会同步刷新date_modified字段为当前时间,这就是调用get_date_modified()拿到下单时间而非手动更新时间的原因。

解决方案

不需要修改你现有的按重量属性扣减库存的代码,直接通过WooCommerce原生钩子拦截库存变动场景下的修改时间更新即可,核心逻辑是:

  • 检测到是订单触发的库存扣减/回滚操作时,临时关闭修改时间自动更新
  • 库存操作完成后立刻恢复默认逻辑,保证手动编辑商品时修改时间能正常刷新

将以下代码加入主题functions.php文件或自定义代码片段插件中即可生效:

// 订单库存变动时阻止更新商品date_modified字段
add_action('woocommerce_before_product_object_save', 'block_modified_update_on_stock_change', 10, 2);
function block_modified_update_on_stock_change($product, $data_store) {
    if (did_action('woocommerce_reduce_order_stock') || did_action('woocommerce_restore_order_stock')) {
        add_filter('woocommerce_product_get_date_modified', 'retain_original_modified_date', 10, 2);
    }
}

function retain_original_modified_date($date, $product) {
    remove_filter('woocommerce_product_get_date_modified', 'retain_original_modified_date', 10);
    return $product->get_date_modified();
}

// 兼容直接调用库存更新接口的场景
add_filter('woocommerce_update_product_stock_query', 'temporary_disable_modified_refresh', 10, 4);
function temporary_disable_modified_refresh($sql, $product_id, $new_stock, $operation) {
    add_filter('woocommerce_product_should_update_date_modified', '__return_false', 10);
    return $sql;
}

// 库存更新完成后恢复默认逻辑
add_action('woocommerce_product_set_stock', 'restore_modified_refresh_logic');
add_action('woocommerce_variation_set_stock', 'restore_modified_refresh_logic');
function restore_modified_refresh_logic() {
    remove_filter('woocommerce_product_should_update_date_modified', '__return_false', 10);
}
注意事项
  • 该代码完全兼容已有的pa_weight属性扣库存逻辑,无需改动原有代码
  • 拦截范围仅限订单触发的库存扣减、取消订单触发的库存回滚场景,后台手动编辑商品、REST接口更新商品时,date_modified字段仍会正常更新
  • 采用临时挂载/移除过滤器的写法,不会造成全局逻辑污染,不影响其他插件、主题对商品修改时间的正常调用

你原有库存扣减逻辑参考如下:

// ---------------------------------------------------------------------------------------- //
// --------------------------  REDUCE WEIGHT BASED ON WEIGHT FIELD  ----------------------- //
// ---------------------------------------------------------------------------------------- //

// reduce stock based on 'pa_weight' attribute
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 ); 
function filter_order_item_quantity( $quantity, $order, $item )  
{
    $product   = $item->get_product();
    $term_name = $product->get_attribute('pa_weight');

    // 'pa_weight' - keep only the numbers
    $quantity = preg_replace('/[^0-9.]+/', '', $term_name);

    // new quantity
    if( is_numeric ( $quantity ) && $quantity != 0 )
        $quantity *= $quantity;

    return $quantity;
}


/**
 * Order change stock reduction fix.
 * 
 * @param $prevent
 * @param $item
 * @param $item_quantity
 * @return bool|mixed
 */
function custom_stock_reduction_order_change( $prevent, $item, $item_quantity ) {
    $stock_reduced   = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
    $stock_reduction = filter_order_item_quantity( $item->get_quantity(), null, $item );

    if ( $stock_reduction == $stock_reduced ) {
        $prevent = true;
    }

    return $prevent;
}
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', 'custom_stock_reduction_order_change', 10, 3 );

内容的提问来源于stack exchange,提问作者JayStone22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 17:45:40