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
- Return a number using the closest multiple of another number. Used to enforce step/multiple values.
- validate_cart_item_quantity() : WP_Error|true
- Check that a given quantity is valid according to any limits in place.
- filter_value() : mixed
- Get a quantity for a product or cart item by 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_product_quantity_limit() : int
- Get the limit for the total number of a product allowed in the cart.
- get_remaining_stock() : int|null
- Returns the remaining stock for a product if it has stock.
- 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>
Parameters
- $product : WC_Product
-
Product instance.
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 $number, int $multiple_of[, string $rounding_function = 'round' ]) : int
Parameters
- $number : int
-
Number to round.
- $multiple_of : int
-
The multiple.
- $rounding_function : string = 'round'
-
ceil, floor, or round.
Return values
int —validate_cart_item_quantity()
Check that a given quantity is valid according to any limits in place.
public
validate_cart_item_quantity(int $quantity, WC_Product|array<string|int, mixed> $cart_item) : WP_Error|true
Parameters
- $quantity : int
-
Quantity to validate.
- $cart_item : WC_Product|array<string|int, mixed>
-
Cart item.
Return values
WP_Error|true —filter_value()
Get a quantity for a product or cart item by running it through a filter hook.
protected
filter_value(int|null $value, string $value_type, WC_Product|array<string|int, mixed> $cart_item_or_product) : mixed
Parameters
- $value : int|null
-
Value to filter.
- $value_type : string
-
Type of value. Used for filter suffix.
- $cart_item_or_product : WC_Product|array<string|int, mixed>
-
Either a cart item or a product instance.
Return values
mixed —get_draft_order()
Uses the draft order ID to return an order object, if valid.
protected
get_draft_order() : WC_Order|null
Return values
WC_Order|null —get_draft_order_id()
Gets draft order data from the customer session.
protected
get_draft_order_id() : int
Return values
int —get_product_quantity_limit()
Get the limit for the total number of a product allowed in the cart.
protected
get_product_quantity_limit(WC_Product $product) : int
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
- $product : WC_Product
-
Product instance.
Return values
int —get_remaining_stock()
Returns the remaining stock for a product if it has stock.
protected
get_remaining_stock(WC_Product $product) : int|null
This also factors in draft orders.
Parameters
- $product : WC_Product
-
Product instance.
Return values
int|null —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
bool — Whether 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.