WooCommerce Code Reference

QuantityLimits
in package
Uses DraftOrderTrait

QuantityLimits class.

Returns limits for products and cart items when using the StoreAPI and supporting classes.

Table of Contents

get_add_to_cart_limits()  : array<string|int, mixed>
Get limits for product add to cart forms.
get_cart_item_quantity_limits()  : array<string|int, mixed>
Get quantity limits (min, max, step/multiple) for a product or cart item.
limit_to_multiple()  : int|float
Return a number using the closest multiple of another number. Used to enforce step/multiple values.
normalize_cart_item_quantity()  : int|float
Fix a quantity violation by adjusting it to the nearest valid quantity.
validate_cart_item_quantity()  : WP_Error|true
Check that a given quantity is valid according to any limits in place.
adjust_product_quantity_limit()  : int|float
Get the limit for the total number of a product allowed in the cart.
filter_boolean_value()  : bool
Get a boolean value while running it through a filter hook.
filter_numeric_value()  : int|float
Get a numeric value while running it through a filter hook.
forget_draft_order_if_not_saved()  : void
Removes the order from the session when it is the draft order and its last save failed.
get_draft_order()  : WC_Order|null
Uses the draft order ID to return an order object, if valid.
get_draft_order_id()  : int
Gets draft order data from the customer session.
get_remaining_stock()  : int|float|null
Returns the remaining stock for a product if it has stock.
is_multiple_of()  : bool
Checks if a number is a multiple of another number.
is_valid_draft_order()  : bool
Whether the argument can serve as the session's draft order: a checkout-draft, or a pending/failed order the shopper can still pay as placed (its cart hash matches, the cart keeps its counted coupons, a priced order that needs shipping still ships to the customer's destination, and its tax location has not moved).
order_ships_to_customer_destination()  : bool
Whether the order's shipping lines are still rated for the session customer's destination, which the cart hash does not cover. Compared through the pay-for-order guard's helper, reading a state the session filled in by default as empty; the taxable location is checked by order_taxed_at_customer_location().
order_taxed_at_customer_location()  : bool
Whether the order would still be taxed for the same location once it takes the session customer's addresses, which the cart hash does not cover either. The sync re-prices tax from the order's own addresses, so the old tax is never charged, but the new address skips the checks checkout makes (allowed countries, state codes).
set_draft_order_id()  : mixed
Updates draft order data in the customer session.
cart_keeps_counted_coupons()  : bool
Whether a sync from the cart would keep the coupons the order counted. The cart hash does not cover coupons, and a sync that swapped or dropped a counted coupon would leave it counted with no order carrying it. An order that counted nothing, e.g. a failed one, may follow the cart. When the cart holds a coupon an extension supplies without a stored record, such as the one WooCommerce Subscriptions puts in place of a parent order's coupons, the order may follow the cart as long as the cart adds no stored coupon the order lacks. Compares formatted codes, not `_coupons_hash`, which only the Store API sync writes. The flag is read through the data store, because the order object may be one held since before its usage was counted.
is_stored_coupon_code()  : bool
Whether a coupon with this code is stored: one wc_get_coupon_id_by_code() finds, or a coupon post with the code in any status. wc_get_coupon_id_by_code() only finds published coupons, so a coupon trashed after the order was placed would otherwise pass for an extension's code.
locations_match()  : bool
Whether two locations match, reading a state the customer session filled in by default as empty.
order_awaits_first_address()  : bool
Whether the order has no address yet and was not created through the Store API, e.g. one a merchant made for the shopper to fill in. It is taxed at the shop's base address and has no shipping zone yet, so, as in OrderController::validate_location_change(), its first address is allowed.
without_default_state()  : array<string|int, mixed>
Returns a location without the state the customer session fills in by default.

Methods

get_add_to_cart_limits()

Get limits for product add to cart forms.

public get_add_to_cart_limits(WC_Product $product[, array<string|int, mixed>|null $cart_item = null ]) : array<string|int, mixed>
Parameters
$product : WC_Product

Product instance.

$cart_item : array<string|int, mixed>|null = null

Optional cart item associated with the product.

Return values
array<string|int, mixed> —

get_cart_item_quantity_limits()

Get quantity limits (min, max, step/multiple) for a product or cart item.

public get_cart_item_quantity_limits(array<string|int, mixed> $cart_item) : array<string|int, mixed>
Parameters
$cart_item : array<string|int, mixed>

A cart item array.

Return values
array<string|int, mixed> —

limit_to_multiple()

Return a number using the closest multiple of another number. Used to enforce step/multiple values.

public limit_to_multiple(int|float $number, int|float $multiple_of[, string $rounding_function = 'round' ]) : int|float
Parameters
$number : int|float

Number to round.

$multiple_of : int|float

The multiple.

$rounding_function : string = 'round'

ceil, floor, or round.

Return values
int|float —

normalize_cart_item_quantity()

Fix a quantity violation by adjusting it to the nearest valid quantity.

public normalize_cart_item_quantity(int|float $quantity, array<string|int, mixed> $cart_item) : int|float
Parameters
$quantity : int|float

Quantity.

$cart_item : array<string|int, mixed>

Cart item.

Return values
int|float —

validate_cart_item_quantity()

Check that a given quantity is valid according to any limits in place.

public validate_cart_item_quantity(int|float $quantity, array<string|int, mixed> $cart_item) : WP_Error|true
Parameters
$quantity : int|float

Quantity to validate.

$cart_item : array<string|int, mixed>

Cart item.

Return values
WP_Error|true —

adjust_product_quantity_limit()

Get the limit for the total number of a product allowed in the cart.

protected adjust_product_quantity_limit(int|float $purchase_limit, WC_Product $product[, array<string|int, mixed>|null $cart_item = null ]) : int|float

This is based on product properties, including remaining stock, and defaults to a maximum of 9999 of any product in the cart at once.

Parameters
$purchase_limit : int|float

The purchase limit from the product. Usually maps to get_max_purchase_quantity.

$product : WC_Product

Product instance.

$cart_item : array<string|int, mixed>|null = null

Optional cart item associated with the product.

Return values
int|float —

filter_boolean_value()

Get a boolean value while running it through a filter hook.

protected filter_boolean_value(bool $value, string $value_type, WC_Product $product[, array<string|int, mixed>|null $cart_item = null ]) : bool
Parameters
$value : bool

Value to filter.

$value_type : string

Type of value. Used for filter suffix.

$product : WC_Product

Product instance.

$cart_item : array<string|int, mixed>|null = null

Optional cart item associated with the product.

Return values
bool —

filter_numeric_value()

Get a numeric value while running it through a filter hook.

protected filter_numeric_value(int|float $value, string $value_type, WC_Product $product[, array<string|int, mixed>|null $cart_item = null ]) : int|float
Parameters
$value : int|float

Value to filter.

$value_type : string

Type of value. Used for filter suffix.

$product : WC_Product

Product instance.

$cart_item : array<string|int, mixed>|null = null

Optional cart item associated with the product.

Return values
int|float —

forget_draft_order_if_not_saved()

Removes the order from the session when it is the draft order and its last save failed.

protected forget_draft_order_if_not_saved(WC_Order $order) : void

A failed save can leave the order partly written, and resuming it would reuse that state.

Parameters
$order : WC_Order

Order object.

Tags
since
11.2.2
Return values
void —

get_remaining_stock()

Returns the remaining stock for a product if it has stock.

protected get_remaining_stock(WC_Product $product) : int|float|null

This also factors in draft orders.

Parameters
$product : WC_Product

Product instance.

Return values
int|float|null —

is_multiple_of()

Checks if a number is a multiple of another number.

protected is_multiple_of(int|float $number, int|float $multiple_of) : bool
Parameters
$number : int|float

The number to check.

$multiple_of : int|float

The multiple.

Return values
bool —

is_valid_draft_order()

Whether the argument can serve as the session's draft order: a checkout-draft, or a pending/failed order the shopper can still pay as placed (its cart hash matches, the cart keeps its counted coupons, a priced order that needs shipping still ships to the customer's destination, and its tax location has not moved).

protected is_valid_draft_order(WC_Order $order_object) : bool
Parameters
$order_object : WC_Order

Order object to check.

Return values
bool — Whether the order is valid as a draft order.

order_ships_to_customer_destination()

Whether the order's shipping lines are still rated for the session customer's destination, which the cart hash does not cover. Compared through the pay-for-order guard's helper, reading a state the session filled in by default as empty; the taxable location is checked by order_taxed_at_customer_location().

protected order_ships_to_customer_destination(WC_Order $order_object) : bool
Parameters
$order_object : WC_Order

Order object to check.

Tags
since
11.3.1
Return values
bool — True when the destinations match or the cart needs no shipping; false without a customer, or when only one of the order and the customer has a shipping country.

order_taxed_at_customer_location()

Whether the order would still be taxed for the same location once it takes the session customer's addresses, which the cart hash does not cover either. The sync re-prices tax from the order's own addresses, so the old tax is never charged, but the new address skips the checks checkout makes (allowed countries, state codes).

protected order_taxed_at_customer_location(WC_Order $order_object) : bool

Refusing reuse sends the change through checkout instead. Same before-and-after comparison as the pay-for-order guard, OrderController::validate_location_change(), except that a state the session filled in by default is read as empty; its exemption for an order awaiting its first address is applied by is_valid_draft_order().

Parameters
$order_object : WC_Order

Order object to check.

Tags
since
11.3.1
Return values
bool — True when the tax location is unchanged or the store collects no tax; false without a customer, or when either filtered tax location is not an array.

set_draft_order_id()

Updates draft order data in the customer session.

protected set_draft_order_id(int $order_id) : mixed
Parameters
$order_id : int

Draft order ID.

Return values
mixed —

cart_keeps_counted_coupons()

Whether a sync from the cart would keep the coupons the order counted. The cart hash does not cover coupons, and a sync that swapped or dropped a counted coupon would leave it counted with no order carrying it. An order that counted nothing, e.g. a failed one, may follow the cart. When the cart holds a coupon an extension supplies without a stored record, such as the one WooCommerce Subscriptions puts in place of a parent order's coupons, the order may follow the cart as long as the cart adds no stored coupon the order lacks. Compares formatted codes, not `_coupons_hash`, which only the Store API sync writes. The flag is read through the data store, because the order object may be one held since before its usage was counted.

private cart_keeps_counted_coupons(WC_Order $order_object) : bool
Parameters
$order_object : WC_Order

Order object to check.

Return values
bool —

is_stored_coupon_code()

Whether a coupon with this code is stored: one wc_get_coupon_id_by_code() finds, or a coupon post with the code in any status. wc_get_coupon_id_by_code() only finds published coupons, so a coupon trashed after the order was placed would otherwise pass for an extension's code.

private is_stored_coupon_code(string $code) : bool
Parameters
$code : string

Coupon code as applied to the cart.

Return values
bool —

locations_match()

Whether two locations match, reading a state the customer session filled in by default as empty.

private locations_match(array<string|int, mixed> $order_location, array<string|int, mixed> $customer_location) : bool
Parameters
$order_location : array<string|int, mixed>

Location the order was priced for.

$customer_location : array<string|int, mixed>

Location the session customer would give it.

Return values
bool —

order_awaits_first_address()

Whether the order has no address yet and was not created through the Store API, e.g. one a merchant made for the shopper to fill in. It is taxed at the shop's base address and has no shipping zone yet, so, as in OrderController::validate_location_change(), its first address is allowed.

private order_awaits_first_address(WC_Order $order_object) : bool
Parameters
$order_object : WC_Order

Order object to check.

Return values
bool —

without_default_state()

Returns a location without the state the customer session fills in by default.

private without_default_state(array<string|int, mixed> $location) : array<string|int, mixed>

The session gives a customer with no billing state the default customer location's state, whatever their country (WC_Customer_Data_Store_Session::set_defaults()), and saves it. A shopper who left an optional state empty has it on later requests while the order they placed has none. On another country, where it is not one of that country's states, such a state is read as empty so the same address still compares as equal.

Parameters
$location : array<string|int, mixed>

Location with country and state keys.

Return values
array<string|int, mixed> —