In a recent Business Bloomer Club Slack thread, a member sought advice on monitoring price changes for WooCommerce products.
This can be challenging because price updates may occur either manually by an admin or automatically via plugins or custom code. The user wanted to trigger an action whenever a product price changes, either for logging, notifications, or other purposes.
Here’s a guide on how to detect these changes using WooCommerce hooks, including options for monitoring both manual and automated price updates.
1. Detecting Manual Price Changes with save_post_product
For manually triggered price changes in the WooCommerce admin, you can use the save_post_product
action. This hook runs whenever a product is saved or updated, allowing you to compare the current and previous prices.
Here’s a code snippet to check for price changes:
add_action( 'save_post_product', 'check_product_price_change', 10, 3 );
function check_product_price_change( $post_id, $post, $update ) {
// Get the product object
$product = wc_get_product( $post_id );
// Get the current and previous prices
$current_price = $product->get_regular_price();
$previous_price = get_post_meta( $post_id, '_regular_price', true );
// Compare prices
if ( $current_price !== $previous_price ) {
// Log the price change or perform any action
error_log( 'Product ID ' . $post_id . ' price changed from ' . $previous_price . ' to ' . $current_price );
}
}
This code:
- Checks if the current price differs from the previous value stored in the database.
- Logs the change, which you can replace with other actions like sending notifications or updating a database.
2. Monitoring All Price Changes with woocommerce_before_product_object_save
and woocommerce_after_product_object_save
For detecting price updates triggered by plugins or code (not only manual changes), you can use woocommerce_before_product_object_save
and woocommerce_after_product_object_save
. These hooks allow you to capture product information both before and after the product is saved, making it possible to detect changes from other sources.
add_action( 'woocommerce_before_product_object_save', 'detect_price_change_before_save' );
add_action( 'woocommerce_after_product_object_save', 'detect_price_change_after_save' );
function detect_price_change_before_save( $product ) {
// Store the current price as a transient before saving
set_transient( 'product_' . $product->get_id() . '_price', $product->get_regular_price(), 60 );
}
function detect_price_change_after_save( $product ) {
// Get the previous price from the transient
$previous_price = get_transient( 'product_' . $product->get_id() . '_price' );
// Get the current price
$current_price = $product->get_regular_price();
// Check if the price has changed
if ( $previous_price !== $current_price ) {
error_log( 'Price changed for product ID: ' . $product->get_id() . ' from ' . $previous_price . ' to ' . $current_price );
// Perform any additional actions here, like sending a notification
}
}
This approach:
- Saves the price before the product is updated, allowing for a direct comparison post-save.
- Captures changes regardless of whether they’re triggered manually or programmatically, making it more versatile.
3. Scheduled Price Checks with CRON
For periodic checks of price changes across multiple products, consider setting up a daily cron job. This approach can be useful for larger stores or when tracking changes programmatically.
- Store product prices in a custom database table or custom field.
- Set up a cron job using
wp_schedule_event
to check for price differences daily.
Conclusion
Tracking price changes in WooCommerce can be effectively achieved using save_post_product
for manual updates or woocommerce_before_product_object_save
and woocommerce_after_product_object_save
for broader detection. For more advanced needs, a daily cron job may be useful. These methods allow you to monitor price changes and take action, whether for alerts, logs, or targeted promotions.