Skip to content

Commit 320c1e5

Browse files
committed
Implement get_query_for_stock from WC core
WooCommerce Core implemented a method to get the current stock quantity for a product. This patch brings that functionality into the feature plugin as well. Without this patch, customers are unable to checkout with any product that is managing inventory levels. See core method https://github.com/woocommerce/woocommerce/blob/09d98a6fe059c4a0c5e88a549213a08b56314e85/includes/data-stores/class-wc-product-data-store-cpt.php#L2112 Testing: *Before applying this patch* - Create a product that has an inventory stock level - Add items to the cart - Proceed through checkout - Verify checkout fails with a not enough stock notice *After applying this patch* - Add the product with an inventory stock level to cart - Proceed through checkout - Verify checkout is successful - Verify stock count was decremented appropriately
1 parent bcb6b90 commit 320c1e5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

includes/data-stores/class-wc-product-data-store-custom-table.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,4 +2273,23 @@ public function query( $query_vars ) {
22732273

22742274
return $products;
22752275
}
2276+
2277+
/**
2278+
* Returns query statement for getting current `stock_quantity` of a product.
2279+
*
2280+
* @internal MAX function below is used to make sure result is a scalar.
2281+
* @param int $product_id Product ID.
2282+
* @return string|void Query statement.
2283+
*/
2284+
public function get_query_for_stock( $product_id ) {
2285+
global $wpdb;
2286+
return $wpdb->prepare(
2287+
"
2288+
SELECT COALESCE ( MAX( stock_quantity ), 0 ) FROM {$wpdb->prefix}wc_products
2289+
2290+
WHERE product_id = %d
2291+
",
2292+
$product_id
2293+
);
2294+
}
22762295
}

0 commit comments

Comments
 (0)