Automattic\WooCommerce\Checkout\Helpers

ReserveStock::get_stale_own_holds_exclusion_clause │ private │ WC 1.0

Builds a SQL clause excluding the current shopper's stale stock holds.

Method of the class: ReserveStock{}

Returns

string. SQL clause, or an empty string when no holds qualify.

Usage

// private - for code of main (parent) class only
$result = $this->get_stale_own_holds_exclusion_clause( $product_id, $exclude_order_id ): string;
$product_id(int) (required)
Product whose reserved stock is being counted.
$exclude_order_id(int) (required)
Order already excluded by the caller.

ReserveStock::get_stale_own_holds_exclusion_clause() code WC 11.3.0

private function get_stale_own_holds_exclusion_clause( $product_id, $exclude_order_id ): string {
	global $wpdb;

	/**
	 * Filters how many minutes must pass before a shopper's own unpaid stock hold
	 * stops blocking that same shopper.
	 *
	 * The exclusion is disabled by default. Enabling it can create multiple payable
	 * orders for the same stock and cause overselling if an older payment later
	 * completes. Return 0 to exclude the shopper's own holds immediately.
	 *
	 * @since 11.3.0
	 *
	 * @param int|false $threshold_minutes Threshold in minutes, or false to keep the exclusion disabled.
	 * @param int       $product_id        Product whose reserved stock is being counted.
	 * @param int       $exclude_order_id  Order already excluded by the caller.
	 */
	$threshold_minutes = apply_filters( 'woocommerce_own_stock_hold_exclusion_threshold_minutes', false, $product_id, $exclude_order_id );

	if ( false === $threshold_minutes || ! is_numeric( $threshold_minutes ) ) {
		return '';
	}

	$threshold_minutes = max( 0, (int) $threshold_minutes );

	$order_ids = array_values( array_diff( $this->get_current_shopper_stock_holding_order_ids(), array( absint( $exclude_order_id ) ) ) );

	if ( empty( $order_ids ) ) {
		return '';
	}

	$placeholders = implode( ',', array_fill( 0, count( $order_ids ), '%d' ) );

	// The `timestamp` column records when the hold was first placed, so it
	// measures the age of the shopper's attempt rather than of the last write.
	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The interpolated value contains only generated %d placeholders.
	return $wpdb->prepare(
		"
		AND NOT (
			stock_table.`order_id` IN ( $placeholders )
			AND stock_table.`timestamp` < ( NOW() - INTERVAL %d MINUTE )
		)
		",
		array_merge( $order_ids, array( $threshold_minutes ) )
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}