WooCommerce Code Reference

OrderController
in package

OrderController class.

Helper class which creates and syncs orders with the cart.

Table of Contents

$additional_fields_controller  : CheckoutFields
Checkout fields controller.
__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.
sync_customer_data_with_order()  : mixed
Copies order data to customer object (not the session), so values persist for future checkouts.
update_order_from_cart()  : mixed
Update an order using data from the current cart.
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.
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_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_usage_per_aliases()  : int
Get the usage count for a coupon based on a list of aliases (ids, emails).

Properties

Methods

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
throws
RouteException

Exception if invalid data is detected.

Return values
mixed

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_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_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
throws
RouteException

Exception if invalid data is detected.

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
throws
RouteException

Exception if invalid data is detected.

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
throws
RouteException

Exception if invalid data is detected.

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_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
boolTrue 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
$coupon : WC_Coupon

Coupon object applied to the cart.

$order : WC_Order

Order object.

Tags
throws
Exception

Exception if invalid data is detected.

Return values
mixed

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
$coupon : WC_Coupon

Coupon object applied to the cart.

$order : WC_Order

Order object.

Tags
throws
Exception

Exception if invalid data is detected.

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
stringEmail or empty string.

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