php 减少库存仅适用于Woocommerce中的特定订单状态和付款方式

6tdlim6h  于 2023-01-01  发布在  PHP
关注(0)|答案(2)|浏览(146)

我正在为客户开发一个自定义Woocommerce功能。他们使用BACS支付网关来处理手动支付。
但是,网关目前减少库存的时间对于我们的需求来说太早了,即当订单处于"暂停"状态时。我只想在订单标记为"正在处理"或"完成"时减少库存(避免重复减少)。
我已经设法防止股票减少自己,而"持有"与以下片段:

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;
}

简而言之,我希望库存根据以下库存状态进行更改:

  • 暂停-不执行任何操作
  • 已完成或正在处理-减少库存(仅一次)
  • 取消-增加库存(仅在最初减少时)

任何帮助都是非常感谢!

vmdwslir

vmdwslir1#

使用**woocommerce_order_status_changed**中的自定义函数,您将能够针对“处理中”和“已完成”订单状态更改减少订单项目库存。
我已经在您的功能中添加了一个条件,目标只有“BACS”订单上的支付网关。

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文件中。*

测试和工作

tjjdgumg

tjjdgumg2#

公认的答案对我不起作用。第一部分起作用,但第二部分不起作用,下面是我对它的修改:

// 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 );
    }
  }
}

在订单标记为processingcompleted之前,这不会减少BACS付款的库存。

相关问题