Skip to content
Snippets Groups Projects
Commit 81e5dbf2 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

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.
parent 7966244e
Branches
Tags V1.2
No related merge requests found
...@@ -100,6 +100,12 @@ class OrdernumberHelperWooCommerce extends OrdernumberHelper { ...@@ -100,6 +100,12 @@ class OrdernumberHelperWooCommerce extends OrdernumberHelper {
} }
function getCounter($type, $format, $default=0) { 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); return get_option (self::$ordernumber_counter_prefix.$type.'-'.$format, $default);
} }
...@@ -108,7 +114,7 @@ class OrdernumberHelperWooCommerce extends OrdernumberHelper { ...@@ -108,7 +114,7 @@ class OrdernumberHelperWooCommerce extends OrdernumberHelper {
} }
function setCounter($type, $format, $value) { 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) { function deleteCounter($type, $format) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment