I’m using the following code to hook a product update in woocommerce:
add_action('woocommerce_update_product', 'on_update_product', 10, 2);
function on_update_product($product_id, $product){
// code here
}
Is there a way to check if certain fields have changed, compared to the previously stored version of the product?
Thanks!
2
Answers
The best way to do this that I know is with hashes.
To get data out of the product object check this resource:
https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
I hope this suits your situation
I would recommend hooking to another action. I use it to identify changes in orders, but it actually can use for any woocomercce related object types (orders, products, coupons, subscriptions etc.)
for your purpose you can use:
Having that said, you need to be careful, since this hook may be initiated from different triggers (some background processes etc). You may want to have some caution measurements:
$_POST['action'] == 'editpost'
to make sure the action is anactual “Update” click from the admin edit page.
(is_admin())
to limit it only to admin area(!defined('DOING_CRON'))
to make sure it won’t run on any cron execution(!defined('DOING_AJAX'))
to make sure it won’t run on ajax callsthis way you can limit it only to the exact action you wish to catch.