CartController
in package
Uses
DraftOrderTrait
Woo Cart Controller class.
Helper class to bridge the gap between the cart API and Woo core.
Table of Contents
- add_to_cart() : string
- Based on the core cart class but returns errors rather than rendering notices directly.
- apply_coupon() : mixed
- Based on the core cart class but returns errors rather than rendering notices directly.
- calculate_totals() : WC_Cart
- Recalculates the cart totals and returns the updated cart instance.
- empty_cart() : mixed
- Empty cart contents.
- get_cart_coupons() : array<string|int, mixed>
- Returns all applied coupons.
- get_cart_errors() : WP_Error
- Validate the cart and get a list of errors.
- get_cart_for_response() : WC_Cart
- Gets the latest cart instance, and ensures totals have been calculated before returning.
- get_cart_hashes() : array<string|int, mixed>
- Get hashes for items in the current cart. Useful for tracking changes.
- get_cart_instance() : WC_Cart
- Get main instance of cart class.
- get_cart_item() : array<string|int, mixed>
- Return a cart item from the woo core cart class.
- get_cart_items() : array<string|int, mixed>
- Returns all cart items.
- get_shipping_packages() : array<string|int, mixed>
- Get shipping packages from the cart with calculated shipping rates.
- has_coupon() : bool
- See if cart has applied coupon by code.
- load_cart() : mixed
- Makes the cart and sessions available to a route by loading them from core.
- normalize_cart() : mixed
- Normalizes the cart by fixing any quantity violations.
- select_shipping_rate() : mixed
- Selects a shipping rate.
- set_cart_item_quantity() : mixed
- Based on core `set_quantity` method, but validates if an item is sold individually first and enforces any limits in place.
- validate_add_to_cart() : mixed
- Validate all items in the cart and check for errors.
- validate_cart() : mixed
- Validate cart and check for errors.
- validate_cart_coupons() : mixed
- Validate all coupons in the cart and check for errors.
- validate_cart_item() : mixed
- Validates an existing cart item and returns any errors.
- validate_cart_items() : mixed
- Validate all items in the cart and check for errors.
- validate_cart_not_empty() : mixed
- When placing an order, validate that the cart is not empty.
- filter_request_data() : array<string|int, mixed>
- Filter data for add to cart requests.
- 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_product_for_cart() : WC_Product|Error
- Get a product object to be added to the cart.
- get_product_id() : int
- For a given product, get the product ID.
- get_product_name() : string
- Get product name, hiding it for products the current user cannot view (unpublished products, or variations of one).
- get_product_quantity_in_cart() : int
- Gets the qty of a product across line items.
- get_remaining_stock_for_product() : int
- Gets remaining stock for a product.
- get_variable_product_attributes() : array<string|int, mixed>
- Get product attributes from the variable product (which may be the parent if the product object is a variation).
- get_variation_id() : int
- For a given product, get the variation ID.
- get_variation_id_from_variation_data() : int
- Try to match request data to a variation ID and return the ID.
- 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).
- parse_variation_data() : array<string|int, mixed>
- If variations are set, validate and format the values ready to add to the cart.
- sanitize_variation_data() : array<string|int, mixed>
- Format and sanitize variation data posted to the API.
- set_draft_order_id() : mixed
- Updates draft order data in the customer session.
- throw_default_product_exception() : mixed
- Default exception thrown when an item cannot be added to the cart.
- validate_cart_coupon() : mixed
- Validates an existing cart coupon and returns any errors.
- add_product_names_to_message() : mixed
- Generates the error message for out of stock products and adds product names to it.
- 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.
- get_error_message_for_stock_exception_type() : string
- Takes a string describing the type of stock extension, whether there is a single product or multiple products causing this exception and returns an appropriate error message.
- 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.
- stock_exceptions_to_wp_errors() : WP_Error
- This method will take arrays of exceptions relating to stock, and will convert them to a WP_Error object.
- without_default_state() : array<string|int, mixed>
- Returns a location without the state the customer session fills in by default.
Methods
add_to_cart()
Based on the core cart class but returns errors rather than rendering notices directly.
public
add_to_cart(array<string|int, mixed> $request) : string
Parameters
- $request : array<string|int, mixed>
-
Add to cart request params.
Tags
Return values
string —apply_coupon()
Based on the core cart class but returns errors rather than rendering notices directly.
public
apply_coupon(string $coupon_code) : mixed
Parameters
- $coupon_code : string
-
Coupon code.
Tags
Return values
mixed —calculate_totals()
Recalculates the cart totals and returns the updated cart instance.
public
calculate_totals() : WC_Cart
Tags
Return values
WC_Cart —empty_cart()
Empty cart contents.
public
empty_cart() : mixed
Return values
mixed —get_cart_coupons()
Returns all applied coupons.
public
get_cart_coupons([callable $callback = null ]) : array<string|int, mixed>
Parameters
- $callback : callable = null
-
Optional callback to apply to the array filter.
Return values
array<string|int, mixed> —get_cart_errors()
Validate the cart and get a list of errors.
public
get_cart_errors() : WP_Error
Return values
WP_Error — A WP_Error instance containing the cart's errors.get_cart_for_response()
Gets the latest cart instance, and ensures totals have been calculated before returning.
public
get_cart_for_response() : WC_Cart
Return values
WC_Cart —get_cart_hashes()
Get hashes for items in the current cart. Useful for tracking changes.
public
get_cart_hashes() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_cart_instance()
Get main instance of cart class.
public
get_cart_instance() : WC_Cart
Tags
Return values
WC_Cart —get_cart_item()
Return a cart item from the woo core cart class.
public
get_cart_item(string $item_id) : array<string|int, mixed>
Parameters
- $item_id : string
-
Cart item id.
Return values
array<string|int, mixed> —get_cart_items()
Returns all cart items.
public
get_cart_items([callable $callback = null ]) : array<string|int, mixed>
Parameters
- $callback : callable = null
-
Optional callback to apply to the array filter.
Return values
array<string|int, mixed> —get_shipping_packages()
Get shipping packages from the cart with calculated shipping rates.
public
get_shipping_packages([bool $calculate_rates = true ]) : array<string|int, mixed>
Parameters
- $calculate_rates : bool = true
-
Should rates for the packages also be returned.
Tags
Return values
array<string|int, mixed> —has_coupon()
See if cart has applied coupon by code.
public
has_coupon(string $coupon_code) : bool
Parameters
- $coupon_code : string
-
Cart coupon code.
Return values
bool —load_cart()
Makes the cart and sessions available to a route by loading them from core.
public
load_cart() : mixed
Return values
mixed —normalize_cart()
Normalizes the cart by fixing any quantity violations.
public
normalize_cart() : mixed
Return values
mixed —select_shipping_rate()
Selects a shipping rate.
public
select_shipping_rate(int|string $package_id, string $rate_id) : mixed
The rate must be listed for the package. Other choices are ignored.
Parameters
- $package_id : int|string
-
ID of the package to choose a rate for.
- $rate_id : string
-
ID of the rate being chosen.
Return values
mixed —set_cart_item_quantity()
Based on core `set_quantity` method, but validates if an item is sold individually first and enforces any limits in place.
public
set_cart_item_quantity(string $item_id[, int|float $quantity = 1 ]) : mixed
Parameters
- $item_id : string
-
Cart item id.
- $quantity : int|float = 1
-
Cart quantity.
Tags
Return values
mixed —validate_add_to_cart()
Validate all items in the cart and check for errors.
public
validate_add_to_cart(WC_Product $product, array<string|int, mixed> $request) : mixed
Parameters
- $product : WC_Product
-
Product object associated with the cart item.
- $request : array<string|int, mixed>
-
Add to cart request params.
Tags
Return values
mixed —validate_cart()
Validate cart and check for errors.
public
validate_cart() : mixed
Tags
Return values
mixed —validate_cart_coupons()
Validate all coupons in the cart and check for errors.
public
validate_cart_coupons() : mixed
Tags
Return values
mixed —validate_cart_item()
Validates an existing cart item and returns any errors.
public
validate_cart_item(array<string|int, mixed> $cart_item) : mixed
Parameters
- $cart_item : array<string|int, mixed>
-
Cart item array.
Tags
Return values
mixed —validate_cart_items()
Validate all items in the cart and check for errors.
public
validate_cart_items() : mixed
Tags
Return values
mixed —validate_cart_not_empty()
When placing an order, validate that the cart is not empty.
public
validate_cart_not_empty() : mixed
Tags
Return values
mixed —filter_request_data()
Filter data for add to cart requests.
protected
filter_request_data(array<string|int, mixed> $request) : array<string|int, mixed>
Parameters
- $request : array<string|int, mixed>
-
Add to cart request params.
Return values
array<string|int, mixed> — Updated request array.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
Return values
void —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_for_cart()
Get a product object to be added to the cart.
protected
get_product_for_cart(array<string|int, mixed> $request) : WC_Product|Error
Parameters
- $request : array<string|int, mixed>
-
Add to cart request params.
Tags
Return values
WC_Product|Error — Returns a product object if purchasable.get_product_id()
For a given product, get the product ID.
protected
get_product_id(WC_Product $product) : int
Parameters
- $product : WC_Product
-
Product object associated with the cart item.
Return values
int —get_product_name()
Get product name, hiding it for products the current user cannot view (unpublished products, or variations of one).
protected
get_product_name(WC_Product $product) : string
Parameters
- $product : WC_Product
-
Product instance.
Return values
string —get_product_quantity_in_cart()
Gets the qty of a product across line items.
protected
get_product_quantity_in_cart(WC_Product $product) : int
Parameters
- $product : WC_Product
-
Product object.
Return values
int —get_remaining_stock_for_product()
Gets remaining stock for a product.
protected
get_remaining_stock_for_product(WC_Product $product) : int
Parameters
- $product : WC_Product
-
Product object.
Return values
int —get_variable_product_attributes()
Get product attributes from the variable product (which may be the parent if the product object is a variation).
protected
get_variable_product_attributes(WC_Product $product) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
Product being added to the cart.
Tags
Return values
array<string|int, mixed> —get_variation_id()
For a given product, get the variation ID.
protected
get_variation_id(WC_Product $product) : int
Parameters
- $product : WC_Product
-
Product object associated with the cart item.
Return values
int —get_variation_id_from_variation_data()
Try to match request data to a variation ID and return the ID.
protected
get_variation_id_from_variation_data(array<string|int, mixed> $request, WC_Product $product) : int
Parameters
- $request : array<string|int, mixed>
-
Add to cart request params.
- $product : WC_Product
-
Product being added to the cart.
Tags
Return values
int — Matching variation ID.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
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
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.parse_variation_data()
If variations are set, validate and format the values ready to add to the cart.
protected
parse_variation_data(array<string|int, mixed> $request) : array<string|int, mixed>
Parameters
- $request : array<string|int, mixed>
-
Add to cart request params.
Tags
Return values
array<string|int, mixed> — Updated request array.sanitize_variation_data()
Format and sanitize variation data posted to the API.
protected
sanitize_variation_data(array<string|int, mixed> $variation_data, array<string|int, mixed> $variable_product_attributes) : array<string|int, mixed>
Labels are converted to names (e.g. Size to pa_size), and values are cleaned.
Parameters
- $variation_data : array<string|int, mixed>
-
Key value pairs of attributes and values.
- $variable_product_attributes : array<string|int, mixed>
-
Product attributes we're expecting.
Tags
Return values
array<string|int, mixed> —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 —throw_default_product_exception()
Default exception thrown when an item cannot be added to the cart.
protected
throw_default_product_exception(WC_Product $product) : mixed
Parameters
- $product : WC_Product
-
Product object associated with the cart item.
Tags
Return values
mixed —validate_cart_coupon()
Validates an existing cart coupon and returns any errors.
protected
validate_cart_coupon(WC_Coupon $coupon) : mixed
Parameters
- $coupon : WC_Coupon
-
Coupon object applied to the cart.
Tags
Return values
mixed —add_product_names_to_message()
Generates the error message for out of stock products and adds product names to it.
private
add_product_names_to_message(string $singular, string $plural, array<string|int, mixed> $items) : mixed
Parameters
- $singular : string
-
The message to use when only one product is in the list.
- $plural : string
-
The message to use when more than one product is in the list.
- $items : array<string|int, mixed>
-
The list of cart items whose names should be inserted into the message.
Tags
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 —get_error_message_for_stock_exception_type()
Takes a string describing the type of stock extension, whether there is a single product or multiple products causing this exception and returns an appropriate error message.
private
get_error_message_for_stock_exception_type(string $exception_type, string $singular_or_plural) : string
Parameters
- $exception_type : string
-
The type of exception encountered.
- $singular_or_plural : string
-
Whether to get the error message for a single product or multiple.
Return values
string —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 —stock_exceptions_to_wp_errors()
This method will take arrays of exceptions relating to stock, and will convert them to a WP_Error object.
private
stock_exceptions_to_wp_errors(array<string|int, TooManyInCartException> $too_many_in_cart_products, array<string|int, NotPurchasableException> $not_purchasable_products, array<string|int, PartialOutOfStockException> $partial_out_of_stock_products, array<string|int, OutOfStockException> $out_of_stock_products) : WP_Error
Parameters
- $too_many_in_cart_products : array<string|int, TooManyInCartException>
-
Array of TooManyInCartExceptions.
- $not_purchasable_products : array<string|int, NotPurchasableException>
-
Array of NotPurchasableExceptions.
- $partial_out_of_stock_products : array<string|int, PartialOutOfStockException>
-
Array of PartialOutOfStockExceptions.
- $out_of_stock_products : array<string|int, OutOfStockException>
-
Array of OutOfStockExceptions.
Return values
WP_Error — The WP_Error object returned. Will have errors if any exceptions were in the args. It will be empty if they do not.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.
