Reputation: 167
I'm trying to find a way to stop products going back in to stock when removing them from an order.
This is when we go in to the "Orders" screen, click on an individual order and remove a single product from that order. As standard it goes back in to stock.
I've been looking at the wc_delete_order_item
function but can't seem to figure it out.
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;
}
Upvotes: 1
Views: 604
Reputation: 29624
You're close, however before wc_delete_order_item
we can find in 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 );
The wc_maybe_adjust_line_item_product_stock
function from admin/wc-admin-functions.php
contains the following filter hook woocommerce_prevent_adjust_line_item_product_stock
So to answer your question, you could use (where the function can be further specified via the other parameters: only certain products, etc...)
/**
* 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 );
Or in short
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', '__return_true' );
Upvotes: 3