I need previous and current stock quantity. For previous I found woocommerce_product_before_set_stock
hook, that must be right, but it saves current quantity;
add_action('woocommerce_product_before_set_stock', function ($product) {
update_post_meta($product->get_id(), '_old_stock_quantity', $product->get_stock_quantity());
});
For current i can use just $product->get_stock_quantity()
. So i need only previous. Help./
2
Answers
You can use
get_post_meta
. to get old stock quantity by using the_stock
key. Try the below code.You can use the
woocommerce_update_product_stock_query
hook found inside theupdate_product_stock
andset_product_stock
methods of theWC_Product_Data_Store_CPT
class.Then you can use the
$product
object to get the old stock quantity of the product (since the product is not yet updated), via$product->get_stock_quantity()
.To save the previous product stock quantity as a custom post meta you can use the following function:
The code has been tested and works. Add it to your active theme’s functions.php.
FOR INFORMATION
If the operation was decrease and the quantity was 2, the value of the
$sql
variable would be: