DraftOrderTrait
DraftOrderTrait
Shared functionality for getting and setting draft order IDs from session.
Table of Contents
- 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.
- 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).
- set_draft_order_id() : mixed
- Updates draft order data in the customer session.
- 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.
- 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.
- without_default_state() : array<string|int, mixed>
- Returns a location without the state the customer session fills in by default.
Methods
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 —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.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 —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 —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 —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.
