From bd0d4d6ac762d166ca171bd5a99d7f8815ba36b0 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:24:41 -0800 Subject: [PATCH 1/3] ReserveStock: Add method to detect if HPOS tables are in use This method is copied from WC_Customer_Data_Store. --- .../woocommerce/src/Checkout/Helpers/ReserveStock.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php b/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php index b164c895eb0..de49ddab74f 100644 --- a/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php +++ b/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php @@ -5,6 +5,8 @@ namespace Automattic\WooCommerce\Checkout\Helpers; +use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; + defined( 'ABSPATH' ) || exit; /** @@ -36,6 +38,15 @@ final class ReserveStock { return $this->enabled; } + /** + * Check if the usage of the custom orders table is enabled. + * + * @return bool + */ + protected function is_cot_in_use(): bool { + return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled(); + } + /** * Query for any existing holds on stock for this item. * From 4b179e1d182c4e41cb002165b6ed6e020b923dde Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:55:39 -0800 Subject: [PATCH 2/3] ReserveStock: Modify reserved stock query when HPOS is in use Ensures reserved stock is correctly counted when HPOS tables are used and not kept in sync with the posts table. --- .../changelog/fix-36375-hpos-reserve-stock | 4 ++++ .../src/Checkout/Helpers/ReserveStock.php | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-36375-hpos-reserve-stock diff --git a/plugins/woocommerce/changelog/fix-36375-hpos-reserve-stock b/plugins/woocommerce/changelog/fix-36375-hpos-reserve-stock new file mode 100644 index 00000000000..2c956bcfd0a --- /dev/null +++ b/plugins/woocommerce/changelog/fix-36375-hpos-reserve-stock @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Add HPOS support to the reserved stock query diff --git a/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php b/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php index de49ddab74f..df9f5b83f68 100644 --- a/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php +++ b/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php @@ -213,11 +213,20 @@ final class ReserveStock { */ private function get_query_for_reserved_stock( $product_id, $exclude_order_id = 0 ) { global $wpdb; + + $join = "$wpdb->posts posts ON stock_table.`order_id` = posts.ID"; + $where_status = "posts.post_status IN ( 'wc-checkout-draft', 'wc-pending' )"; + if ( $this->is_cot_in_use() ) { + $join = "{$wpdb->prefix}wc_orders orders ON stock_table.`order_id` = orders.id"; + $where_status = "orders.status IN ( 'wc-checkout-draft', 'wc-pending' )"; + } + + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $query = $wpdb->prepare( " SELECT COALESCE( SUM( stock_table.`stock_quantity` ), 0 ) FROM $wpdb->wc_reserved_stock stock_table - LEFT JOIN $wpdb->posts posts ON stock_table.`order_id` = posts.ID - WHERE posts.post_status IN ( 'wc-checkout-draft', 'wc-pending' ) + LEFT JOIN $join + WHERE $where_status AND stock_table.`expires` > NOW() AND stock_table.`product_id` = %d AND stock_table.`order_id` != %d @@ -225,6 +234,7 @@ final class ReserveStock { $product_id, $exclude_order_id ); + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared /** * Filter: woocommerce_query_for_reserved_stock From 5d59e534dab35d24ac01205fb633c07bcf07aadc Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Tue, 24 Jan 2023 16:02:46 -0800 Subject: [PATCH 3/3] ReserveStock: Switch to OrderUtil for detecting HPOS usage --- .../src/Checkout/Helpers/ReserveStock.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php b/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php index df9f5b83f68..247e80a5255 100644 --- a/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php +++ b/plugins/woocommerce/src/Checkout/Helpers/ReserveStock.php @@ -5,7 +5,7 @@ namespace Automattic\WooCommerce\Checkout\Helpers; -use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; +use Automattic\WooCommerce\Utilities\OrderUtil; defined( 'ABSPATH' ) || exit; @@ -38,15 +38,6 @@ final class ReserveStock { return $this->enabled; } - /** - * Check if the usage of the custom orders table is enabled. - * - * @return bool - */ - protected function is_cot_in_use(): bool { - return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled(); - } - /** * Query for any existing holds on stock for this item. * @@ -216,7 +207,7 @@ final class ReserveStock { $join = "$wpdb->posts posts ON stock_table.`order_id` = posts.ID"; $where_status = "posts.post_status IN ( 'wc-checkout-draft', 'wc-pending' )"; - if ( $this->is_cot_in_use() ) { + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { $join = "{$wpdb->prefix}wc_orders orders ON stock_table.`order_id` = orders.id"; $where_status = "orders.status IN ( 'wc-checkout-draft', 'wc-pending' )"; }