仅在WooCommerce中的特定订单状态和付款方式中减少库存



我正在为客户提供一些自定义的WooCommerce功能。他们使用BAC付款网关来处理手动付款。

但是,目前,网关可减少库存,以满足我们的需求,即订单"搁置"时。我只想在订单标记为"处理"或"完整"时减少库存(避免重复减少(。

我已努力防止股票在以下片段"搁置"时减少自身:

function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' ) ) {
$reduce_stock = false;
}
return $reduce_stock;
}
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );

我不太确定如何进行。虽然上述代码有效,但添加以下条件并不能:

else if ( $order->has_status( 'processing' ) || $order->has_status( 'completed' ) ) {
$reduce_stock = true;
}
简而
  • 搁置 - 什么都不做
  • 完成或处理 - 减少库存(仅一次(
  • 取消 - 增加库存(仅在最初减少时(

任何帮助都非常感谢!

使用挂钩 woocommerce_order_status_changed 中的自定义函数您将能够定位'处理'和'已完成的'订单状态更改降低订单项目库存。

我在您的功能中添加了一种条件,仅针对订单上的" BAC"支付网关。

add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
    if ( $order->has_status( 'on-hold' ) && $order->get_payment_method() == 'bacs' ) {
        $reduce_stock = false;
    }
    return $reduce_stock;
}
add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
    // Only for 'processing' and 'completed' order statuses change
    if ( $new_status == 'processing' || $new_status == 'completed' ){
    $stock_reduced = get_post_meta( $order_id, '_order_stock_reduced', true );
        if( empty($stock_reduced) && $order->get_payment_method() == 'bacs' ){
            wc_reduce_stock_levels($order_id);
        }
    }
}

代码在活动子主题(或活动主题(的function.php文件中。

测试并起作用

接受的答案对我不起作用。第一部分确实是但不是第二部分,这就是我修改它的方式:

// This is the same as the accepted answer
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
    if ( $order->has_status( 'on-hold' ) && $order->get_payment_method() == 'bacs' ) {
        $reduce_stock = false;
    }
    return $reduce_stock;
}
// This is what I changed
add_action( 'woocommerce_order_status_processing', 'reduce_stock_on_bacs_order_status_change', 10, 1 );
add_action( 'woocommerce_order_status_completed', 'reduce_stock_on_bacs_order_status_change', 10, 1 );
function reduce_stock_on_bacs_order_status_change( $order_id ) {
  // Get the order object
  $order = wc_get_order( $order_id );
  
  // Check if the order was paid using BACS
  if ( 'bacs' == $order->get_payment_method() ) {
    // Check if the stock reduction has already been done for this order
    $stock_reduction_done = get_post_meta( $order_id, '_stock_reduction_done', true );
    if ( ! $stock_reduction_done ) {
      // Iterate over the order items
      foreach( $order->get_items() as $item_id => $item ) {
        // Reduce stock for each item
        $product = $item->get_product();
        $qty = $item->get_quantity();
        $product->reduce_stock( $qty );
      }
      
      // Mark the stock reduction as done for this order
      update_post_meta( $order_id, '_stock_reduction_done', true );
    }
  }
}

这不会减少BACS付款的股票,直到订单标记为processingcompleted

最新更新