Im allowing backorders in my store, but i don’t want the stock quantity to be negative.
I tried to fix this using this :
add_action('woocommerce_variation_set_stock', 'avoid_negative_stock', 10, 1);
function avoid_negative_stock( $product ) {
if ( did_action( 'woocommerce_variation_set_stock' ) >= 2 )
return;
if($product->get_stock_quantity() < 0){
$product->set_stock_quantity(0);
$product->save();
}
}
But it doesn’t work :/
Can anyone help me up with that ?
Regards
2
Answers
You can use the
woocommerce_update_product_stock_query
hook.In this answer you can find a solution.
I’ll give you an example:
This is multiplied by all orders of the same product which for any reason are canceled and/or refunded. The product stock quantity will no longer be reliable.
Just be aware of it.
That said, you can disable negative stock quantity like this:
The code has been tested and works. Add it to your active theme’s functions.php.
You can use
update_product_stock
andread_stock_quantity
Try below code.