Automattic\WooCommerce\Admin\API

ProductsLowInStock::get_low_in_stock_products()protectedWC 1.0

Get low in stock products data.

Method of the class: ProductsLowInStock{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_low_in_stock_products( $page, $per_page, $status );
$page(int)
current page.
Default: 1
$per_page(int)
items per page.
Default: 1
$status(string)
post status.
Default: 'publish'

ProductsLowInStock::get_low_in_stock_products() code WC 8.7.0

protected function get_low_in_stock_products( $page = 1, $per_page = 1, $status = 'publish' ) {
	global $wpdb;

	$offset              = ( $page - 1 ) * $per_page;
	$low_stock_threshold = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );

	$sidewide_stock_threshold_only = $this->is_using_sitewide_stock_threshold_only();

	$query_string = $this->get_query( $sidewide_stock_threshold_only );

	$query_results = $wpdb->get_results(
		// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
		$wpdb->prepare( $query_string, $status, $low_stock_threshold, $offset, $per_page ),
		OBJECT_K
	);

	$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 ),
	);

	$total_results = $count_query_results[0]->total;

	return array(
		'results' => $query_results,
		'total'   => (int) $total_results,
		'pages'   => (int) ceil( $total_results / (int) $per_page ),
	);
}