Automattic\WooCommerce\Admin\API

ProductsLowInStock::get_countprotectedWC 1.0

Get the count of low in stock products.

Method of the class: ProductsLowInStock{}

No Hooks.

Returns

Int.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_count( $sidewide_stock_threshold_only, $status, $low_stock_threshold );
$sidewide_stock_threshold_only(true|false) (required)
Boolean to check if the store is using sitewide stock threshold only.
$status(string) (required)
Post status.
$low_stock_threshold(int) (required)
Low stock threshold.

ProductsLowInStock::get_count() code WC 10.3.6

protected function get_count( $sidewide_stock_threshold_only, $status, $low_stock_threshold ) {
	global $wpdb;
	if ( $sidewide_stock_threshold_only ) {
		$count_query_string  = $this->get_count_query( $sidewide_stock_threshold_only );
		$count_query_results = $wpdb->get_results(
			// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
			$wpdb->prepare( $count_query_string, $status, $low_stock_threshold ),
		);

		return (int) $count_query_results[0]->total;
	}

	// Split the query into two queries, one for products with a custom stock threshold and one for products without a custom stock threshold.
	// Splitting the queries also speeds up the query.
	$count_query_with_custom_stock_threshold_string    = $this->get_products_with_custom_stock_threshold_count_query_str();
	$count_query_without_custom_stock_threshold_string = $this->get_products_without_custom_stock_threshold_count_query_str();
	$count_query_with_custom_stock_threshold_results   = $wpdb->get_results(
		// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
		$wpdb->prepare( $count_query_with_custom_stock_threshold_string, $status ),
	);
	$count_query_without_custom_stock_threshold_results = $wpdb->get_results(
		// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
		$wpdb->prepare( $count_query_without_custom_stock_threshold_string, $status, $low_stock_threshold ),
	);

	return (int) $count_query_with_custom_stock_threshold_results[0]->total + (int) $count_query_without_custom_stock_threshold_results[0]->total;
}