Skip to content

Commit

Permalink
Merge pull request #186 from Mindsize/get-query-for-stock
Browse files Browse the repository at this point in the history
Implement `get_query_for_stock` from WC core
  • Loading branch information
claudiosanches authored Feb 19, 2021
2 parents bcb6b90 + 83257d9 commit bc785d8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions includes/data-stores/class-wc-product-data-store-custom-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2273,4 +2273,22 @@ public function query( $query_vars ) {

return $products;
}

/**
* Returns query statement for getting current `stock_quantity` of a product.
*
* @internal MAX function below is used to make sure result is a scalar.
* @param int $product_id Product ID.
* @return string|void Query statement.
*/
public function get_query_for_stock( $product_id ) {
global $wpdb;
return $wpdb->prepare(
"
SELECT COALESCE ( MAX( stock_quantity ), 0 ) FROM {$wpdb->prefix}wc_products
WHERE product_id = %d
",
$product_id
);
}
}

0 comments on commit bc785d8

Please sign in to comment.