OrderController
in package
OrderController class.
Helper class which creates and syncs orders with the cart.
Table of Contents
- ORDER_LOCK_OPTION_PREFIX = 'wc_store_api_order_lock_'
- Prefix of the option name used as a self-expiring mutex around an order. The order ID is appended.
- ORDER_LOCK_TIMEOUT = 10
- How long (in seconds) lock_order() waits by default for a lock held by another request.
- ORDER_LOCK_TTL = 60
- How long (in seconds) a held order lock stays valid before it is considered stale and can be taken over.
- LOCATION_FIELDS = ['country', 'state', 'postcode', 'city']
- Address fields that can change the price. Shipping zones match on country, state and postcode, and tax rates also match on city. Other fields, like name or street, do not change the price.
- $additional_fields_controller : CheckoutFields
- Checkout fields controller.
- $order_locks : array<string|int, mixed>
- Orders locked by the current request, as order ID => stored release time.
- $unlock_orders_on_shutdown : Closure|null
- Shutdown callback that releases locks held by this request.
- __construct() : mixed
- Constructor.
- create_order_from_cart() : WC_Order
- Create order and set props based on global settings.
- default_order_status() : string
- Changes default order status to draft for orders created via this API.
- get_failed_order_stock_error() : mixed
- Get errors for order stock on failed orders.
- get_order_lock_error_message() : string
- Get the shopper-facing message used when another request holds an order lock.
- locations_differ() : bool
- Checks whether two locations match a different shipping zone or tax rate.
- lock_order() : bool
- Give the current request exclusive use of an order until the request ends, or confirm that it still has it.
- sync_customer_data_with_order() : mixed
- Copies order data to customer object (not the session), so values persist for future checkouts.
- update_existing_order_addresses() : array<string|int, mixed>|null
- Sets new addresses on an existing order and validates them, without saving the order.
- update_order_from_cart() : mixed
- Update an order using data from the current cart.
- validate_existing_order_before_payment() : mixed
- Final validation for existing orders, ran before payment is taken.
- validate_existing_order_before_update() : void
- Validate an existing order's address data before the order is updated from the request.
- validate_order_before_payment() : mixed
- Final validation ran before payment is taken.
- validate_order_key() : mixed
- Validate a given order key against an existing order.
- validate_selected_shipping_methods() : mixed
- Check there is a shipping method if it requires shipping.
- get_coupon() : WC_Coupon
- Convert a coupon code to a coupon object.
- perform_custom_order_validation() : mixed
- Perform custom order validation via WooCommerce hooks.
- update_addresses_from_cart() : mixed
- Update address data from cart and/or customer session data.
- update_line_items_from_cart() : mixed
- Create order line items.
- validate_address_fields() : mixed
- Check all required address fields are set and return errors if not.
- validate_addresses() : mixed
- Validates customer address data based on the locale to ensure required fields are set.
- validate_allowed_country() : bool
- Check all required address fields are set and return errors if not.
- validate_coupon_email_restriction() : mixed
- Check email restrictions of a coupon against the order.
- validate_coupon_global_usage_limit() : void
- Check the coupon's global usage limit against the order.
- validate_coupon_usage_limit() : mixed
- Check usage restrictions of a coupon against the order.
- validate_coupons() : mixed
- Validate coupons applied to the order and remove those that are not valid.
- validate_email() : mixed
- Validates the customer email. This is a required field.
- get_email_from_user_id() : string
- Get user email from user id.
- get_shipping_location() : array<string|int, mixed>
- Gets the location an order ships to.
- get_usage_per_aliases() : int
- Get the usage count for a coupon based on a list of aliases (ids, emails).
- normalize_location_field() : string
- Normalizes a location field the way shipping zones and tax rates match it.
- restore_state_names() : void
- Keeps a state the order stores as a name, e.g. "New York", when the request sends its code, e.g. "NY".
- validate_location_change() : void
- Rejects a location change that would change the order's shipping cost or tax.
- validate_shipping_was_not_skipped_for_missing_address() : void
- Check shipping was not withheld because "Hide shipping costs until an address is entered" judged the address incomplete.
Constants
ORDER_LOCK_OPTION_PREFIX
Prefix of the option name used as a self-expiring mutex around an order. The order ID is appended.
public
mixed
ORDER_LOCK_OPTION_PREFIX
= 'wc_store_api_order_lock_'
The row holds the lock's release time (a microtime float); its mere existence is the lock. It lives in the options table for the reasons given on BatchProcessingController::ENQUEUED_PROCESSORS_LOCK_OPTION.
Tags
ORDER_LOCK_TIMEOUT
How long (in seconds) lock_order() waits by default for a lock held by another request.
public
mixed
ORDER_LOCK_TIMEOUT
= 10
Tags
ORDER_LOCK_TTL
How long (in seconds) a held order lock stays valid before it is considered stale and can be taken over.
public
mixed
ORDER_LOCK_TTL
= 60
Locks are released when the request ends, so this only bounds how long a lock left behind by a request that died without running its shutdown hooks keeps the order out of reach. A request that may have outlived its lock confirms it with lock_order() before relying on it.
Tags
LOCATION_FIELDS
Address fields that can change the price. Shipping zones match on country, state and postcode, and tax rates also match on city. Other fields, like name or street, do not change the price.
private
array<string|int, string>
LOCATION_FIELDS
= ['country', 'state', 'postcode', 'city']
Tags
Properties
$additional_fields_controller
Checkout fields controller.
private
CheckoutFields
$additional_fields_controller
$order_locks
Orders locked by the current request, as order ID => stored release time.
private
static array<string|int, mixed>
$order_locks
= array()
$unlock_orders_on_shutdown
Shutdown callback that releases locks held by this request.
private
static Closure|null
$unlock_orders_on_shutdown
= null
Methods
__construct()
Constructor.
public
__construct() : mixed
Return values
mixed —create_order_from_cart()
Create order and set props based on global settings.
public
create_order_from_cart() : WC_Order
Tags
Return values
WC_Order — A new order object.default_order_status()
Changes default order status to draft for orders created via this API.
public
default_order_status() : string
Return values
string —get_failed_order_stock_error()
Get errors for order stock on failed orders.
public
get_failed_order_stock_error(int $order_id) : mixed
Parameters
- $order_id : int
-
Order ID.
Tags
Return values
mixed —get_order_lock_error_message()
Get the shopper-facing message used when another request holds an order lock.
public
static get_order_lock_error_message() : string
Tags
Return values
string —locations_differ()
Checks whether two locations match a different shipping zone or tax rate.
public
static locations_differ(array<string|int, mixed> $old_location, array<string|int, mixed> $new_location) : bool
Shipping zones and tax rates ignore case and postcode spacing, so a change in only those does not count. Shared with DraftOrderTrait so both re-pricing guards agree on what counts as a destination change.
Parameters
- $old_location : array<string|int, mixed>
-
Location before the change.
- $new_location : array<string|int, mixed>
-
Location after the change.
Tags
Return values
bool —lock_order()
Give the current request exclusive use of an order until the request ends, or confirm that it still has it.
public
static lock_order(int $order_id[, int $timeout = self::ORDER_LOCK_TIMEOUT ]) : bool
Checkout flows use this lock to serialize reads and mutations of a shared order. Cart routes may also use it for order-specific side effects, such as releasing reserved stock. Callers acquire the lock before relying on order state so that another request cannot change the order after validation and before placement or payment.
The claim is an atomic INSERT of an options row (which fails if the row already exists, making it a mutex); if the row exists but its stored release time has already passed, a conditional UPDATE takes the stale lock over. Modeled on BatchProcessingController::acquire_enqueued_processors_lock(). A request that already holds the lock atomically renews the row only while its ownership value still matches, so the call is safe to repeat, for instance from every part of a batch request.
Parameters
- $order_id : int
-
Order ID. There is nothing to lock when this is 0.
- $timeout : int = self::ORDER_LOCK_TIMEOUT
-
How long (in seconds) to wait for a lock held by another request.
Tags
Return values
bool — True if the lock is held by this request. False if another request holds it, or took it over from this request after the lock went stale.sync_customer_data_with_order()
Copies order data to customer object (not the session), so values persist for future checkouts.
public
sync_customer_data_with_order(WC_Order $order) : mixed
Parameters
- $order : WC_Order
-
Order object.
Return values
mixed —update_existing_order_addresses()
Sets new addresses on an existing order and validates them, without saving the order.
public
update_existing_order_addresses(WC_Order $order, array<string|int, mixed> $billing, array<string|int, mixed>|null $shipping) : array<string|int, mixed>|null
Rejects a location change that would change the shipping cost or tax. The request recalculates tax but not shipping, and the total should stay what the order was priced at.
Parameters
- $order : WC_Order
-
Existing order.
- $billing : array<string|int, mixed>
-
New billing address.
- $shipping : array<string|int, mixed>|null
-
New shipping address, or null to keep the order's.
Tags
Return values
array<string|int, mixed>|null — Shipping address set on the order, or null if the order kept its own.update_order_from_cart()
Update an order using data from the current cart.
public
update_order_from_cart(WC_Order $order[, bool $update_totals = true ]) : mixed
Parameters
- $order : WC_Order
-
The order object to update.
- $update_totals : bool = true
-
Whether to update totals or not.
Return values
mixed —validate_existing_order_before_payment()
Final validation for existing orders, ran before payment is taken.
public
validate_existing_order_before_payment(WC_Order $order) : mixed
By this point we have an order populated with customer data and items.
Since the cart is not involved, we don't validate shipping methods and assume the order already contains the correct shipping items.
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
mixed —validate_existing_order_before_update()
Validate an existing order's address data before the order is updated from the request.
public
validate_existing_order_before_update(WC_Order $order) : void
Runs before any request data is persisted so a rejected address cannot mutate the order.
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
void —validate_order_before_payment()
Final validation ran before payment is taken.
public
validate_order_before_payment(WC_Order $order) : mixed
By this point we have an order populated with customer data and items.
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
mixed —validate_order_key()
Validate a given order key against an existing order.
public
validate_order_key(int $order_id, string $order_key) : mixed
Parameters
- $order_id : int
-
Order ID.
- $order_key : string
-
Order key.
Tags
Return values
mixed —validate_selected_shipping_methods()
Check there is a shipping method if it requires shipping.
public
validate_selected_shipping_methods(bool $needs_shipping[, array<string|int, mixed> $chosen_shipping_methods = array() ]) : mixed
Parameters
- $needs_shipping : bool
-
Current order needs shipping.
- $chosen_shipping_methods : array<string|int, mixed> = array()
-
Array of shipping methods.
Tags
Return values
mixed —get_coupon()
Convert a coupon code to a coupon object.
protected
get_coupon(string $coupon_code) : WC_Coupon
Parameters
- $coupon_code : string
-
Coupon code.
Return values
WC_Coupon — Coupon object.perform_custom_order_validation()
Perform custom order validation via WooCommerce hooks.
protected
perform_custom_order_validation(WC_Order $order) : mixed
Allows plugins to perform custom validation before payment.
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
mixed —update_addresses_from_cart()
Update address data from cart and/or customer session data.
protected
update_addresses_from_cart(WC_Order $order) : mixed
Parameters
- $order : WC_Order
-
The order object to update.
Return values
mixed —update_line_items_from_cart()
Create order line items.
protected
update_line_items_from_cart(WC_Order $order) : mixed
Parameters
- $order : WC_Order
-
The order object to update.
Return values
mixed —validate_address_fields()
Check all required address fields are set and return errors if not.
protected
validate_address_fields(WC_Order $order, string $address_type, WP_Error $errors) : mixed
Parameters
- $order : WC_Order
-
Order object.
- $address_type : string
-
billing or shipping address, used in error messages.
- $errors : WP_Error
-
Error object.
Return values
mixed —validate_addresses()
Validates customer address data based on the locale to ensure required fields are set.
protected
validate_addresses(WC_Order $order, bool $needs_shipping) : mixed
Parameters
- $order : WC_Order
-
Order object.
- $needs_shipping : bool
-
Whether the order needs shipping.
Tags
Return values
mixed —validate_allowed_country()
Check all required address fields are set and return errors if not.
protected
validate_allowed_country(string $country, array<string|int, mixed> $allowed_countries) : bool
Parameters
- $country : string
-
Country code.
- $allowed_countries : array<string|int, mixed>
-
List of valid country codes.
Return values
bool — True if valid.validate_coupon_email_restriction()
Check email restrictions of a coupon against the order.
protected
validate_coupon_email_restriction(WC_Coupon $coupon, WC_Order $order) : mixed
Parameters
Tags
Return values
mixed —validate_coupon_global_usage_limit()
Check the coupon's global usage limit against the order.
protected
validate_coupon_global_usage_limit(WC_Coupon $coupon, WC_Order $order) : void
Skipped once the order has recorded its own usage, so it is not counted against itself.
Parameters
Tags
Return values
void —validate_coupon_usage_limit()
Check usage restrictions of a coupon against the order.
protected
validate_coupon_usage_limit(WC_Coupon $coupon, WC_Order $order) : mixed
Parameters
Tags
Return values
mixed —validate_coupons()
Validate coupons applied to the order and remove those that are not valid.
protected
validate_coupons(WC_Order $order[, bool $use_order_data = false ]) : mixed
Parameters
- $order : WC_Order
-
Order object.
- $use_order_data : bool = false
-
Whether to use order data or cart data.
Tags
Return values
mixed —validate_email()
Validates the customer email. This is a required field.
protected
validate_email(WC_Order $order) : mixed
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
mixed —get_email_from_user_id()
Get user email from user id.
private
get_email_from_user_id(int $user_id) : string
Parameters
- $user_id : int
-
User ID.
Return values
string — Email or empty string.get_shipping_location()
Gets the location an order ships to.
private
static get_shipping_location(WC_Order $order) : array<string|int, mixed>
An order with no shipping address, e.g. one created in the admin, ships to its billing address. WC_Abstract_Order::get_tax_location() uses the same fallback.
Parameters
- $order : WC_Order
-
Order to read.
Return values
array<string|int, mixed> —get_usage_per_aliases()
Get the usage count for a coupon based on a list of aliases (ids, emails).
private
get_usage_per_aliases(WC_Coupon $coupon, array<string|int, mixed> $aliases) : int
Parameters
- $coupon : WC_Coupon
-
Coupon object applied to the cart.
- $aliases : array<string|int, mixed>
-
List of aliases to check.
Return values
int —normalize_location_field()
Normalizes a location field the way shipping zones and tax rates match it.
private
static normalize_location_field(string $field, array<string|int, mixed> $location) : string
Parameters
- $field : string
-
Field name.
- $location : array<string|int, mixed>
-
Location the field belongs to.
Return values
string —restore_state_names()
Keeps a state the order stores as a name, e.g. "New York", when the request sends its code, e.g. "NY".
private
restore_state_names(WC_Order $order, array<string|int, string> $old_states) : void
This is because some edge cases would have an order with a state name (New York) instead of NY. Switching from New York to NY might trigger new totals so we avoid that by keeping the full name.
Parameters
- $order : WC_Order
-
Order with the new addresses set.
- $old_states : array<string|int, string>
-
State per address type before the new addresses were set.
Return values
void —validate_location_change()
Rejects a location change that would change the order's shipping cost or tax.
private
validate_location_change(WC_Order $order, array<string|int, mixed> $old_shipping_location, array<string|int, mixed> $old_tax_location, bool $needs_shipping) : void
Parameters
- $order : WC_Order
-
Order with the new addresses set.
- $old_shipping_location : array<string|int, mixed>
-
Location the order shipped to before the change.
- $old_tax_location : array<string|int, mixed>
-
Location the order was taxed on before the change.
- $needs_shipping : bool
-
Whether the order needs shipping.
Tags
Return values
void —validate_shipping_was_not_skipped_for_missing_address()
Check shipping was not withheld because "Hide shipping costs until an address is entered" judged the address incomplete.
private
validate_shipping_was_not_skipped_for_missing_address(bool $needs_shipping) : void
Without this, no packages are calculated, so validate_selected_shipping_methods() has nothing to reject and the order is placed with no shipping line. Mirrors the classic checkout's WC_Checkout::is_shipping_withheld_for_missing_address(), including its exemption for stores that switch shipping calculation off through the filter.
Parameters
- $needs_shipping : bool
-
Whether the order needs shipping.
