diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php index be40b97270a..1cd17ed058e 100644 --- a/includes/admin/class-wc-admin-status.php +++ b/includes/admin/class-wc-admin-status.php @@ -339,6 +339,29 @@ class WC_Admin_Status { } } + /** + * Prints table info if a base table is not present. + */ + private static function output_tables_info() { + $missing_tables = WC_Install::verify_base_tables( false ); + if ( 0 === count( $missing_tables ) ) { + return; + } + ?> + +
+ + + + + + get_untested_plugins( WC()->version, 'min ?> - +
- + diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 71d5d0a5d20..cd69608b2ac 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -994,8 +994,8 @@ CREATE TABLE {$wpdb->prefix}wc_reserved_stock ( `order_id` bigint(20) NOT NULL, `product_id` bigint(20) NOT NULL, `stock_quantity` double NOT NULL DEFAULT 0, - `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, - `expires` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `timestamp` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `expires` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`order_id`, `product_id`) ) $collate; "; diff --git a/src/Checkout/Helpers/ReserveStock.php b/src/Checkout/Helpers/ReserveStock.php index b882cf83906..32dff1c5c43 100644 --- a/src/Checkout/Helpers/ReserveStock.php +++ b/src/Checkout/Helpers/ReserveStock.php @@ -13,6 +13,31 @@ defined( 'ABSPATH' ) || exit; * Stock Reservation class. */ final class ReserveStock { + + /** + * Is stock reservation enabled? + * + * @var boolean + */ + private $enabled = true; + + /** + * Constructor + */ + public function __construct() { + // Table needed for this feature are added in 4.3. + $this->enabled = get_option( 'woocommerce_schema_version', 0 ) >= 430; + } + + /** + * Is stock reservation enabled? + * + * @return boolean + */ + protected function is_enabled() { + return $this->enabled; + } + /** * Query for any existing holds on stock for this item. * @@ -24,6 +49,10 @@ final class ReserveStock { public function get_reserved_stock( \WC_Product $product, $exclude_order_id = 0 ) { global $wpdb; + if ( ! $this->is_enabled() ) { + return 0; + } + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared return (int) $wpdb->get_var( $this->get_query_for_reserved_stock( $product->get_stock_managed_by_id(), $exclude_order_id ) ); } @@ -39,7 +68,7 @@ final class ReserveStock { public function reserve_stock_for_order( \WC_Order $order, $minutes = 0 ) { $minutes = $minutes ? $minutes : (int) get_option( 'woocommerce_hold_stock_minutes', 60 ); - if ( ! $minutes ) { + if ( ! $minutes || ! $this->is_enabled() ) { return; } @@ -95,6 +124,10 @@ final class ReserveStock { public function release_stock_for_order( \WC_Order $order ) { global $wpdb; + if ( ! $this->is_enabled() ) { + return; + } + $wpdb->delete( $wpdb->wc_reserved_stock, array( @@ -124,9 +157,10 @@ final class ReserveStock { $result = $wpdb->query( $wpdb->prepare( " - REPLACE INTO {$wpdb->wc_reserved_stock} ( order_id, product_id, stock_quantity, expires ) - SELECT %d, %d, %d, ( NOW() + INTERVAL %d MINUTE ) from DUAL + INSERT INTO {$wpdb->wc_reserved_stock} ( `order_id`, `product_id`, `stock_quantity`, `timestamp`, `expires` ) + SELECT %d, %d, %d, NOW(), ( NOW() + INTERVAL %d MINUTE ) FROM DUAL WHERE ( $query_for_stock FOR UPDATE ) - ( $query_for_reserved_stock FOR UPDATE ) >= %d + ON DUPLICATE KEY UPDATE `expires` = VALUES( `expires` ) ", $order->get_id(), $product_id,

+

+ +

+