Skip to content

Commit bc785d8

Browse files
Merge pull request #186 from Mindsize/get-query-for-stock
Implement `get_query_for_stock` from WC core
2 parents bcb6b90 + 83257d9 commit bc785d8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,4 +2273,22 @@ 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+
WHERE product_id = %d
2290+
",
2291+
$product_id
2292+
);
2293+
}
22762294
}

0 commit comments

Comments
 (0)