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.
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 passed argument is a draft order or an order that is pending/failed and the cart hasn't changed.
set_draft_order_id()  : mixed
Updates draft order data in the customer session.

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

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 passed argument is a draft order or an order that is pending/failed and the cart hasn't changed.

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

Order object to check.

Return values
boolWhether the order is valid as a draft order.

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