ReserveStock
in package
Stock Reservation class.
Table of Contents
- LEGACY_COUNTED_ORDER_STATUSES = array('wc-' . AutomatticWooCommerceEnumsOrderStatus::CHECKOUT_DRAFT, AutomatticWooCommerceEnumsOrderInternalStatus::PENDING)
- Internal statuses whose holds have always counted as held stock.
- MAX_OWN_ORDERS = 10
- Upper bound on own orders considered when excluding a shopper's stale holds.
- OWN_ORDERS_SESSION_KEY = 'stock_holding_orders'
- Session key listing orders this shopper has taken a stock hold for.
- UNPAID_ORDER_STATUSES = array(...self::LEGACY_COUNTED_ORDER_STATUSES, AutomatticWooCommerceEnumsOrderInternalStatus::FAILED)
- Internal statuses an order can still be resumed and paid from.
- $enabled : bool
- Is stock reservation enabled?
- $potential_order_ids_by_customer : array<int, array<string|int, int>>
- Request scoped memo of a signed in customer's potential stock-holding order ids.
- __construct() : mixed
- Constructor
- get_reserved_stock() : int|float
- Query for any existing holds on stock for this item.
- release_stock_for_order() : mixed
- Release a temporary hold on stock for an order.
- reserve_stock_for_order() : mixed
- Put a temporary hold on stock for an order if enough is available.
- is_enabled() : bool
- Is stock reservation enabled?
- get_counted_order_statuses() : array<string|int, string>
- Returns the internal order statuses whose stock holds count as held stock.
- get_current_shopper_stock_holding_order_ids() : array<string|int, int>
- Returns stock-holding order ids associated with the current shopper.
- get_potential_stock_holding_order_ids_for_customer() : array<string|int, int>
- Returns order ids of a signed-in customer that could be holding stock, newest first.
- get_query_for_reserved_stock() : string
- Returns query statement for getting reserved stock of a product.
- get_session_stock_holding_order_ids() : array<string|int, int>
- Returns the stock-holding order ids recorded against the current session.
- get_stale_own_holds_exclusion_clause() : string
- Builds a SQL clause excluding the current shopper's stale stock holds.
- get_status_clause() : string
- Builds the status condition of the reserved stock query.
- quote_status_list() : string
- Quotes and escapes a list of statuses for use inside an SQL IN list.
- remember_order_for_shopper() : void
- Records a stock-holding order against the current customer session.
- reserve_stock_for_product() : mixed
- Reserve stock for a product by inserting rows into the DB.
Constants
LEGACY_COUNTED_ORDER_STATUSES
Internal statuses whose holds have always counted as held stock.
private
mixed
LEGACY_COUNTED_ORDER_STATUSES
= array('wc-' . AutomatticWooCommerceEnumsOrderStatus::CHECKOUT_DRAFT, AutomatticWooCommerceEnumsOrderInternalStatus::PENDING)
The reserved stock query keeps them as a literal pair. See get_status_clause().
MAX_OWN_ORDERS
Upper bound on own orders considered when excluding a shopper's stale holds.
private
mixed
MAX_OWN_ORDERS
= 10
Keeps the generated IN clause bounded. A shopper who exceeds it loses the oldest ids from the list, and those holds block again until they expire.
OWN_ORDERS_SESSION_KEY
Session key listing orders this shopper has taken a stock hold for.
private
mixed
OWN_ORDERS_SESSION_KEY
= 'stock_holding_orders'
Holds array( 'customer' => string, 'order_ids' => int[] ), where customer
is the session customer id that recorded them.
UNPAID_ORDER_STATUSES
Internal statuses an order can still be resumed and paid from.
private
mixed
UNPAID_ORDER_STATUSES
= array(...self::LEGACY_COUNTED_ORDER_STATUSES, AutomatticWooCommerceEnumsOrderInternalStatus::FAILED)
Only holds on these orders are ever excluded as the shopper's own. A paid order's hold keeps counting until wc_reduce_stock_levels() releases it.
Properties
$enabled
Is stock reservation enabled?
private
bool
$enabled
= true
$potential_order_ids_by_customer
Request scoped memo of a signed in customer's potential stock-holding order ids.
private
static array<int, array<string|int, int>>
$potential_order_ids_by_customer
= array()
A new ReserveStock is constructed for each wc_get_held_stock_quantity() call, so the memo cannot live on the instance.
Methods
__construct()
Constructor
public
__construct() : mixed
Return values
mixed —get_reserved_stock()
Query for any existing holds on stock for this item.
public
get_reserved_stock(WC_Product $product, int $exclude_order_id) : int|float
Parameters
- $product : WC_Product
-
Product to get reserved stock for.
- $exclude_order_id : int
-
Optional order to exclude from the results.
Return values
int|float — Amount of stock already reserved.release_stock_for_order()
Release a temporary hold on stock for an order.
public
release_stock_for_order(WC_Order $order) : mixed
Parameters
- $order : WC_Order
-
Order object.
Return values
mixed —reserve_stock_for_order()
Put a temporary hold on stock for an order if enough is available.
public
reserve_stock_for_order(WC_Order $order[, int $minutes = 60 ]) : mixed
Parameters
- $order : WC_Order
-
Order object.
- $minutes : int = 60
-
How long to reserve stock in minutes. Defaults to 60.
Tags
Return values
mixed —is_enabled()
Is stock reservation enabled?
protected
is_enabled() : bool
Return values
bool —get_counted_order_statuses()
Returns the internal order statuses whose stock holds count as held stock.
private
get_counted_order_statuses() : array<string|int, string>
Payment writes the new status before stock is reduced, so holds on paid statuses stay counted until wc_reduce_stock_levels() releases them. Failed is counted because checkout resumes a failed order and reserves again without changing its status.
Return values
array<string|int, string> — Internal (prefixed) order statuses.get_current_shopper_stock_holding_order_ids()
Returns stock-holding order ids associated with the current shopper.
private
get_current_shopper_stock_holding_order_ids() : array<string|int, int>
Includes orders recorded by the current session and unpaid orders belonging to the signed-in customer. Ownership is never inferred from anything a shopper can type, such as the billing email.
Return values
array<string|int, int> —get_potential_stock_holding_order_ids_for_customer()
Returns order ids of a signed-in customer that could be holding stock, newest first.
private
get_potential_stock_holding_order_ids_for_customer(int $customer_id) : array<string|int, int>
Covers a shopper whose session pointers were lost (new device, cleared cookies, lapsed session). Only unpaid statuses are listed to keep the lookup small; get_stale_own_holds_exclusion_clause() enforces the same rule in SQL.
Parameters
- $customer_id : int
-
Customer ID.
Return values
array<string|int, int> —get_query_for_reserved_stock()
Returns query statement for getting reserved stock of a product.
private
get_query_for_reserved_stock(int $product_id, int $exclude_order_id) : string
Parameters
- $product_id : int
-
Product ID.
- $exclude_order_id : int
-
Order to exclude from the results.
Return values
string — Query statement.get_session_stock_holding_order_ids()
Returns the stock-holding order ids recorded against the current session.
private
get_session_stock_holding_order_ids(WC_Session $session) : array<string|int, int>
The list is only trusted if it was written under this session's customer id.
Parameters
- $session : WC_Session
-
Session of the shopper making this request.
Return values
array<string|int, int> —get_stale_own_holds_exclusion_clause()
Builds a SQL clause excluding the current shopper's stale stock holds.
private
get_stale_own_holds_exclusion_clause(int $product_id, int $exclude_order_id, string $status_column) : string
Parameters
- $product_id : int
-
Product whose reserved stock is being counted.
- $exclude_order_id : int
-
Order already excluded by the caller.
- $status_column : string
-
Status column of the order table joined by the caller.
Return values
string — SQL clause, or an empty string when no holds qualify.get_status_clause()
Builds the status condition of the reserved stock query.
private
get_status_clause(string $status_column, array<string|int, string> $statuses) : string
The two statuses counted before 11.3.1 are kept as the literal
IN ( 'wc-checkout-draft', 'wc-pending' ), with any further statuses in a
second IN list, because woocommerce_query_for_reserved_stock callbacks are
known to string-replace that literal to add their own statuses.
Parameters
- $status_column : string
-
Status column of the joined order table.
- $statuses : array<string|int, string>
-
Internal order statuses to count.
Return values
string — SQL condition.quote_status_list()
Quotes and escapes a list of statuses for use inside an SQL IN list.
private
quote_status_list(array<string|int, string> $statuses) : string
Parameters
- $statuses : array<string|int, string>
-
Statuses.
Return values
string — Comma-separated quoted statuses.remember_order_for_shopper()
Records a stock-holding order against the current customer session.
private
remember_order_for_shopper(WC_Order $order) : void
The session's customer id is stamped alongside the ids so the list can be discarded if it later turns up in somebody else's session. See get_session_stock_holding_order_ids().
Parameters
- $order : WC_Order
-
Order that now holds stock.
Return values
void —reserve_stock_for_product()
Reserve stock for a product by inserting rows into the DB.
private
reserve_stock_for_product(int $product_id, int $stock_quantity, WC_Order $order, int $minutes) : mixed
Parameters
- $product_id : int
-
Product ID which is having stock reserved.
- $stock_quantity : int
-
Stock amount to reserve.
- $order : WC_Order
-
Order object which contains the product.
- $minutes : int
-
How long to reserve stock in minutes.
