WooCommerce Code Reference

UpdateUtils
in package
Uses CogsAwareTrait

UpdateUtils class.

Table of Contents

$order_schema  : OrderSchema
The order schema.
update_order_from_request()  : void
Update an order from the request.
get_product_id_from_line_item()  : int
Gets the product ID from the SKU or posted ID.
item_is_null_or_zero()  : bool
Helper method to check if the resource ID associated with the provided item is null.
maybe_set_item_meta_data()  : mixed
Maybe set item meta if posted.
maybe_set_item_prop()  : mixed
Maybe set an item prop if the value was posted.
maybe_set_item_props()  : mixed
Maybe set item props if the values were posted.
prepare_coupon_data()  : WC_Order_Item_Coupon
Create or update an order coupon.
prepare_fee_data()  : WC_Order_Item_Fee
Create or update an order fee.
prepare_line_item_data()  : WC_Order_Item_Product
Create or update a line item, overridden to add COGS data as needed.
prepare_shipping_data()  : WC_Order_Item_Shipping
Create or update an order shipping method.
remove_item_from_order()  : void
Wrapper method to remove order items.
update_address()  : mixed
Update address.
update_line_item()  : int
Wrapper method to create/update order items.
update_line_items()  : mixed
Update line items from an array of line item data for an order. Non-posted line items are removed.
update_meta_data()  : mixed
Update meta data.

Properties

Methods

update_order_from_request()

Update an order from the request.

public update_order_from_request(WC_Order $order, WP_REST_Request $request[, bool $creating = false ]) : void
Parameters
$order : WC_Order

Order object.

$request : WP_REST_Request

Request object.

$creating : bool = false

True when creating object, false when updating.

Tags
throws
WC_REST_Exception

When fails to set any item, \WC_Data_Exception When fails to set any item.

Return values
void

get_product_id_from_line_item()

Gets the product ID from the SKU or posted ID.

protected get_product_id_from_line_item(array<string|int, mixed> $request_data[, string $action = 'create' ]) : int
Parameters
$request_data : array<string|int, mixed>

Request data.

$action : string = 'create'

'create' to add line item or 'update' to update it.

Tags
throws
WC_REST_Exception

When SKU or ID is not valid.

Return values
int

item_is_null_or_zero()

Helper method to check if the resource ID associated with the provided item is null.

protected item_is_null_or_zero(array<string|int, mixed> $item) : bool

Items can be deleted by setting the resource ID to null.

Parameters
$item : array<string|int, mixed>

Item provided in the request body.

Return values
boolTrue if the item resource ID is null, false otherwise.

maybe_set_item_meta_data()

Maybe set item meta if posted.

protected maybe_set_item_meta_data(WC_Order_Item $item, array<string|int, mixed> $request_data) : mixed
Parameters
$item : WC_Order_Item

Order item data.

$request_data : array<string|int, mixed>

Request data.

Return values
mixed

maybe_set_item_prop()

Maybe set an item prop if the value was posted.

protected maybe_set_item_prop(WC_Order_Item $item, string $prop, array<string|int, mixed> $request_data) : mixed
Parameters
$item : WC_Order_Item

Order item.

$prop : string

Order property.

$request_data : array<string|int, mixed>

Request data.

Return values
mixed

maybe_set_item_props()

Maybe set item props if the values were posted.

protected maybe_set_item_props(WC_Order_Item $item, array<string|int, string> $props, array<string|int, mixed> $request_data) : mixed
Parameters
$item : WC_Order_Item

Order item data.

$props : array<string|int, string>

Properties.

$request_data : array<string|int, mixed>

Request data.

Return values
mixed

prepare_coupon_data()

Create or update an order coupon.

protected prepare_coupon_data(array<string|int, mixed> $request_data[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Coupon
Parameters
$request_data : array<string|int, mixed>

Item data.

$action : string = 'create'

'create' to add coupon or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Coupon

prepare_fee_data()

Create or update an order fee.

protected prepare_fee_data(array<string|int, mixed> $request_data[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Fee
Parameters
$request_data : array<string|int, mixed>

Item data.

$action : string = 'create'

'create' to add fee or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Fee

prepare_line_item_data()

Create or update a line item, overridden to add COGS data as needed.

protected prepare_line_item_data(array<string|int, mixed> $request_data[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Product
Parameters
$request_data : array<string|int, mixed>

Line item data.

$action : string = 'create'

'create' to add line item or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Product

prepare_shipping_data()

Create or update an order shipping method.

protected prepare_shipping_data(array<string|int, mixed> $request_data[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Shipping
Parameters
$request_data : array<string|int, mixed>

$shipping Item data.

$action : string = 'create'

'create' to add shipping or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Shipping

remove_item_from_order()

Wrapper method to remove order items.

protected remove_item_from_order(WC_Order $order, string $line_items_type, int $item_id) : void

When updating, the item ID provided is checked to ensure it is associated with the order.

Parameters
$order : WC_Order

The order to remove the item from.

$line_items_type : string

The item type.

$item_id : int

The ID of the item to remove.

Tags
throws
WC_REST_Exception

If item ID is not associated with order.

Return values
void

update_address()

Update address.

protected update_address(WC_Order $order, string $type, array<string|int, mixed> $request_data) : mixed
Parameters
$order : WC_Order

Order data.

$type : string

Type of address; 'billing' or 'shipping'.

$request_data : array<string|int, mixed>

Posted data.

Return values
mixed

update_line_item()

Wrapper method to create/update order items.

protected update_line_item(WC_Order $order, string $line_items_type, array<string|int, mixed> $line_item_data) : int

When updating, the item ID provided is checked to ensure it is associated with the order.

Parameters
$order : WC_Order

order object.

$line_items_type : string

The item type.

$line_item_data : array<string|int, mixed>

item provided in the request body.

Tags
throws
WC_REST_Exception

If item ID is not associated with order.

Return values
intThe ID of the updated or created item.

update_line_items()

Update line items from an array of line item data for an order. Non-posted line items are removed.

protected update_line_items(WC_Order $order, array<string|int, mixed> $line_items[, string $line_items_type = 'line_item' ]) : mixed
Parameters
$order : WC_Order

The order to update the line items for.

$line_items : array<string|int, mixed>

The line items to update.

$line_items_type : string = 'line_item'

The type of line items to update.

Tags
throws
WC_REST_Exception

If line items type is invalid.

Return values
mixed