Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Jetpack Sync: Prevent syncing unchanged orders on save within a short window.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class WooCommerce_HPOS_Orders extends Module {
*/
const WOOCOMMERCE_SUBSCRIPTIONS_PATH = 'woocommerce-subscriptions/woocommerce-subscriptions.php';

/**
* Cache key prefix for storing order checksums to avoid resyncing unchanged orders.
*/
const HPOS_CACHE_KEY = 'jetpack_sync_hpos_cache_key_';

/**
* Cache expiration for storing order content signatures.
*/
const CACHE_EXPIRATION = 5 * MINUTE_IN_SECONDS;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be longer?


/**
* Order table name. There are four order tables (order, addresses, operational_data and meta), but for sync purposes we only care about the main table since it has the order ID.
*
Expand Down Expand Up @@ -325,6 +335,34 @@ public function on_before_enqueue_order_save( $args ) {
return false;
}

$filtered = $this->filter_order_data( $order_object );

unset(
$filtered['date_modified']
);

// md5 of the date-stripped order payload to detect no-change saves (ignores timestamp-only changes)
$current_signature = md5( wp_json_encode( $filtered ) );
$cache_key = self::HPOS_CACHE_KEY . $order_id;

$group = 'jetpack_sync_hpos';

$previous_signature = wp_using_ext_object_cache()
? wp_cache_get( $cache_key, $group )
: get_transient( $cache_key );

// If no substantive changes, skip enqueue.
if ( is_string( $previous_signature ) && hash_equals( $previous_signature, $current_signature ) ) {
return false;
}

// Remember current signature briefly to avoid "Update" or "Recalculate" clicks syncing again with no changes.
if ( wp_using_ext_object_cache() ) {
wp_cache_set( $cache_key, $current_signature, $group, self::CACHE_EXPIRATION );
} else {
set_transient( $cache_key, $current_signature, self::CACHE_EXPIRATION );
}

$processed[ $order_id ] = true;

return array( 'id' => $order_id );
Expand Down
Loading