php - 在 WooCommerce 中从订单中删除产品时防止调整库存
问题描述
我试图找到一种方法来阻止产品从订单中删除时重新进入库存。
这是当我们进入“订单”屏幕时,单击单个订单并从该订单中删除单个产品。作为标准,它会重新入库。
我一直在看这个wc_delete_order_item
功能,但似乎无法弄清楚。
function wc_delete_order_item( $item_id ) {
if ( ! $item_id = absint( $item_id ) ) {
return false;
}
$data_store = WC_Data_Store::load( 'order-item' );
do_action( 'woocommerce_before_delete_order_item', $item_id );
$data_store->delete_order_item( $item_id );
do_action( 'woocommerce_delete_order_item', $item_id );
return true;
}
解决方案
你已经很近了,但是在wc_delete_order_item
我们找到之前includes/class-wc-ajax.php
// Before deleting the item, adjust any stock values already reduced.
if ( $item->is_type( 'line_item' ) ) {
$changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 );
来自的wc_maybe_adjust_line_item_product_stock
函数admin/wc-admin-functions.php
包含以下过滤器挂钩woocommerce_prevent_adjust_line_item_product_stock
因此,要回答您的问题,您可以使用(可以通过其他参数进一步指定功能:仅某些产品等...)
/**
* Prevent adjust line item product stock.
*
* @since 3.7.1
* @param bool $prevent If should prevent (false).
* @param WC_Order_Item $item Item object.
* @param int $item_quantity Optional quantity to check against.
*/
function filter_woocommerce_prevent_adjust_line_item_product_stock ( $prevent, $item, $item_quantity ) {
$prevent = true;
return $prevent;
}
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', 'filter_woocommerce_prevent_adjust_line_item_product_stock', 10, 3 );
或者简而言之
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', '__return_true' );
推荐阅读
- javascript - 图像在网页上打印时间过长 - JavaScript 问题
- function - 减少功能
- reactjs - 错误:使用 axios 获取 API 时,对象作为 React 子项无效
- java - 转换列表
- > 到数组列表
- python - 想要将焦点从第一个文本框更改为下一个文本框?
- python - Ajax 调用表单字段以自动填充其他字段
- c++ - 如何创建一个反转 y 轴的按钮?
- r - R将字符/字符串评估为调用函数名
- python - Create OneToOne field user object in Django serialization
- swift - AVAudioPlayerDelegate swift 不调用该方法