From 81e5dbf20b07bac5f51bb85fda6829e5b5f4c50e Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Sun, 13 Dec 2015 20:08:07 +0100 Subject: [PATCH] V1.2: Fix race condition with counter for concurrent orders - Change the counter values to a non-autoloading option - Clear the option cache before reading the current counter value Before this change, one order would change the counter value, but a second order submitted while the first is still processed would get the old, cached value and thus generate duplicate order numbers. This changes make sure that the counter is never used from cached values. --- ordernumber_helper_woocommerce.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ordernumber_helper_woocommerce.php b/ordernumber_helper_woocommerce.php index d877bb7..479ab4f 100644 --- a/ordernumber_helper_woocommerce.php +++ b/ordernumber_helper_woocommerce.php @@ -100,6 +100,12 @@ class OrdernumberHelperWooCommerce extends OrdernumberHelper { } function getCounter($type, $format, $default=0) { + // the option is cached, so two orders at approximately the same time + // (one submitted while the other one is still processed) will get the + // same counter value from the cach, unless we explicitly erase the + // cache and force WP to look into the database for the current value + wp_cache_delete ('alloptions', 'options'); + wp_cache_delete (self::$ordernumber_counter_prefix.$type.'-'.$format, 'options'); return get_option (self::$ordernumber_counter_prefix.$type.'-'.$format, $default); } @@ -108,7 +114,7 @@ class OrdernumberHelperWooCommerce extends OrdernumberHelper { } function setCounter($type, $format, $value) { - return update_option(self::$ordernumber_counter_prefix.$type.'-'.$format, $value); + return update_option(self::$ordernumber_counter_prefix.$type.'-'.$format, $value, /*autoload=*/false); } function deleteCounter($type, $format) { -- GitLab