WordPress at Your Fingertips
function is not described

Automattic\WooCommerce\Checkout\Helpers

ReserveStock::reserve_stock_for_product() private WC 1.0

Reserve stock for a product by inserting rows into the DB.

{} It's a method of the class: ReserveStock{}

No Hooks.

Return

null. Nothing.

Usage

// private - for code of main (parent) class only
$result = $this->reserve_stock_for_product( $product_id, $stock_quantity, $order, $minutes );
$product_id(int) (required)
Product ID which is having stock reserved.
$stock_quantity(int) (required)
Stock amount to reserve.
$order(\WC_Order) (required)
Order object which contains the product.
$minutes(int) (required)
How long to reserve stock in minutes.

Code of ReserveStock::reserve_stock_for_product() WC 5.9.0

private function reserve_stock_for_product( $product_id, $stock_quantity, $order, $minutes ) {
	global $wpdb;

	$product_data_store       = \WC_Data_Store::load( 'product' );
	$query_for_stock          = $product_data_store->get_query_for_stock( $product_id );
	$query_for_reserved_stock = $this->get_query_for_reserved_stock( $product_id, $order->get_id() );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
	$result = $wpdb->query(
		$wpdb->prepare(
			"
			INSERT INTO {$wpdb->wc_reserved_stock} ( `order_id`, `product_id`, `stock_quantity`, `timestamp`, `expires` )
			SELECT %d, %d, %d, NOW(), ( NOW() + INTERVAL %d MINUTE ) FROM DUAL
			WHERE ( $query_for_stock FOR UPDATE ) - ( $query_for_reserved_stock FOR UPDATE ) >= %d
			ON DUPLICATE KEY UPDATE `expires` = VALUES( `expires` ), `stock_quantity` = VALUES( `stock_quantity` )
			",
			$order->get_id(),
			$product_id,
			$stock_quantity,
			$minutes,
			$stock_quantity
		)
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared

	if ( ! $result ) {
		$product = wc_get_product( $product_id );
		throw new ReserveStockException(
			'woocommerce_product_not_enough_stock',
			sprintf(
				/* translators: %s: product name */
				__( 'Not enough units of %s are available in stock to fulfil this order.', 'woocommerce' ),
				$product ? $product->get_name() : '#' . $product_id
			),
			403
		);
	}
}