Automattic\WooCommerce\Checkout\Helpers

ReserveStock::get_query_for_reserved_stock()privateWC 1.0

Returns query statement for getting reserved stock of a product.

Method of the class: ReserveStock{}

Hooks from the method

Return

String|null. Query statement.

Usage

// private - for code of main (parent) class only
$result = $this->get_query_for_reserved_stock( $product_id, $exclude_order_id );
$product_id(int) (required)
Product ID.
$exclude_order_id(int)
Optional order to exclude from the results.

ReserveStock::get_query_for_reserved_stock() code WC 9.0.1

private function get_query_for_reserved_stock( $product_id, $exclude_order_id = 0 ) {
	global $wpdb;

	$join         = "$wpdb->posts posts ON stock_table.`order_id` = posts.ID";
	$where_status = "posts.post_status IN ( 'wc-checkout-draft', 'wc-pending' )";
	if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
		$join         = "{$wpdb->prefix}wc_orders orders ON stock_table.`order_id` = orders.id";
		$where_status = "orders.status IN ( 'wc-checkout-draft', 'wc-pending' )";
	}

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$query = $wpdb->prepare(
		"
		SELECT COALESCE( SUM( stock_table.`stock_quantity` ), 0 ) FROM $wpdb->wc_reserved_stock stock_table
		LEFT JOIN $join
		WHERE $where_status
		AND stock_table.`expires` > NOW()
		AND stock_table.`product_id` = %d
		AND stock_table.`order_id` != %d
		",
		$product_id,
		$exclude_order_id
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

	/**
	 * Filter: woocommerce_query_for_reserved_stock
	 * Allows to filter the query for getting reserved stock of a product.
	 *
	 * @since 4.5.0
	 * @param string $query            The query for getting reserved stock of a product.
	 * @param int    $product_id       Product ID.
	 * @param int    $exclude_order_id Order to exclude from the results.
	 */
	return apply_filters( 'woocommerce_query_for_reserved_stock', $query, $product_id, $exclude_order_id );
}