PHP Class WC_Abstract_Order

The WooCommerce order class handles order data.
Author: WooThemes
Show file Open project: woocommerce/woocommerce Class Usage Examples

Protected Properties

Property Type Description
$cache_group string A group must be set to to enable caching.
$data array Notes: cart_tax = cart_tax is the new name for the legacy 'order_tax' which is the tax for items only, not shipping.
$data_store_name string Which data store to load.
$items array Order items will be stored here, sometimes before they persist in the DB.
$items_to_delete array Order items that need deleting are stored here.

Public Methods

Method Description
__construct ( integer | object | WC_Order $order ) Get the order if ID is passed, otherwise the order is new and empty.
add_item ( $item ) Adds an order item to this order. The order item will not persist until save.
add_payment_token ( WC_Payment_Token $token ) : boolean | integer Add a payment token to an order
add_product ( WC_Product $product, integer $qty = 1, array $args = [] ) : integer Add a product line item to the order. This is the only line item type with it's own method because it saves looking up order amounts (costs are added up for you).
calculate_shipping ( ) : float Calculate shipping total.
calculate_taxes ( $args = [] ) Calculate taxes for all line items and shipping, and store the totals and tax rows.
calculate_totals ( boolean $and_taxes = true ) : float Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total.
get_cart_tax ( string $context = 'view' ) : float Gets cart tax amount.
get_currency ( string $context = 'view' ) : string Gets order currency.
get_data ( ) : array Get all class data in array format.
get_date_created ( string $context = 'view' ) : integer Get date_created.
get_date_modified ( string $context = 'view' ) : integer Get date_modified.
get_discount_tax ( string $context = 'view' ) : string Get discount_tax.
get_discount_to_display ( $tax_display = '' ) : string Get the discount amount (formatted).
get_discount_total ( string $context = 'view' ) : string Get discount_total.
get_fees ( ) : array Return an array of fees within this order.
get_formatted_line_subtotal ( array $item, string $tax_display = '' ) : string Gets line subtotal - formatted for display.
get_formatted_order_total ( ) : string Gets order total - formatted for display.
get_item ( integer $item_id ) : WC_Order_Item Get an order item object, based on it's type.
get_item_count ( string $item_type = '' ) : string Gets the count of order items of a certain type.
get_item_subtotal ( object $item, boolean $inc_tax = false, boolean $round = true ) : float Get item subtotal - this is the cost before discount.
get_item_tax ( mixed $item, boolean $round = true ) : float Get item tax - useful for gateways.
get_item_total ( object $item, boolean $inc_tax = false, boolean $round = true ) : float Calculate item cost - useful for gateways.
get_items ( string | array $types = 'line_item' ) : Array Return an array of items/products within this order.
get_items_tax_classes ( ) : array Get all tax classes for items in the order.
get_line_subtotal ( object $item, boolean $inc_tax = false, boolean $round = true ) : float Get line subtotal - this is the cost before discount.
get_line_tax ( mixed $item ) : float Get line tax - useful for gateways.
get_line_total ( object $item, boolean $inc_tax = false, boolean $round = true ) : float Calculate line total - useful for gateways.
get_order_item_totals ( mixed $tax_display = '' ) : array Get totals for display on pages and in emails.
get_parent_id ( string $context = 'view' ) : integer Get parent order ID.
get_payment_tokens ( ) : array Returns a list of all payment tokens associated with the current order
get_prices_include_tax ( string $context = 'view' ) : boolean Get prices_include_tax.
get_shipping_method ( ) : string Gets formatted shipping method title.
get_shipping_methods ( ) : array Return an array of shipping costs within this order.
get_shipping_tax ( string $context = 'view' ) : string Get shipping_tax.
get_shipping_to_display ( $tax_display = '' ) : string Gets shipping (formatted).
get_shipping_total ( string $context = 'view' ) : string Get shipping_total.
get_status ( string $context = 'view' ) : string Return the order statuses without wc- internal prefix.
get_subtotal ( ) : float Gets order subtotal.
get_subtotal_to_display ( boolean $compound = false, string $tax_display = '' ) : string Gets subtotal - subtotal is shown before discounts, but with localised taxes.
get_tax_totals ( ) : array Get taxes, merged by code, formatted ready for output.
get_taxes ( ) : array Return an array of taxes within this order.
get_total ( string $context = 'view' ) : float Gets order grand total. incl. taxes. Used in gateways.
get_total_discount ( boolean $ex_tax = true ) : float Gets the total discount amount.
get_total_tax ( string $context = 'view' ) : float Get total tax amount. Alias for get_order_tax().
get_type ( ) : string Get internal type.
get_used_coupons ( ) : array Get coupon codes only.
get_version ( string $context = 'view' ) : string Get order_version.
has_free_item ( ) : boolean Returns true if the order contains a free product.
has_shipping_method ( string $method_id ) : boolean Check whether this order has a specific shipping method or not.
has_status ( $status ) : boolean Checks the order status against a passed in status.
remove_item ( integer $item_id ) Remove item from the order.
remove_order_items ( string $type = null ) Remove all line items (products, coupons, shipping, taxes) from the order.
save ( ) : integer Save data to the database.
set_cart_tax ( string $value ) Set cart tax.
set_currency ( string $value ) Set order_currency.
set_date_created ( string $timestamp ) Set date_created.
set_date_modified ( string $timestamp ) Set date_modified.
set_discount_tax ( string $value ) Set discount_tax.
set_discount_total ( string $value ) Set discount_total.
set_parent_id ( integer $value ) Set parent order ID.
set_prices_include_tax ( boolean $value ) Set prices_include_tax.
set_shipping_tax ( string $value ) Set shipping_tax.
set_shipping_total ( string $value ) Set shipping_total.
set_status ( string $new_status ) : array Set order status.
set_total ( string $value, string $deprecated = '' ) Set total.
set_version ( string $value ) Set order_version.
update_taxes ( ) Update tax lines for the order based on the line item taxes themselves.

Protected Methods

Method Description
get_hook_prefix ( ) : string Prefix for action and filter hooks on data.
get_items_key ( $item ) : string Get key for where a certain item type is stored in _items.
save_items ( ) Save all order items which are part of this order.
set_total_tax ( string $value ) Sets order tax (sum of cart and shipping tax). Used internaly only.
type_to_group ( string $type ) : string Convert a type to a types group.

Method Details

__construct() public method

This class should NOT be instantiated, but the get_order function or new WC_Order_Factory. should be used. It is possible, but the aforementioned are preferred and are the only. methods that will be maintained going forward.
public __construct ( integer | object | WC_Order $order )
$order integer | object | WC_Order Order to read.

add_item() public method

Adds an order item to this order. The order item will not persist until save.
Since: 2.7.0
public add_item ( $item )

add_payment_token() public method

Add a payment token to an order
Since: 2.6
public add_payment_token ( WC_Payment_Token $token ) : boolean | integer
$token WC_Payment_Token Payment token object
return boolean | integer The new token ID or false if it failed.

add_product() public method

Add a product line item to the order. This is the only line item type with it's own method because it saves looking up order amounts (costs are added up for you).
public add_product ( WC_Product $product, integer $qty = 1, array $args = [] ) : integer
$product WC_Product
$qty integer
$args array
return integer order item ID

calculate_shipping() public method

Calculate shipping total.
Since: 2.2
public calculate_shipping ( ) : float
return float

calculate_taxes() public method

Will use the base country unless customer addresses are set.
public calculate_taxes ( $args = [] )
$args array Added in 2.7.0 to pass things like location.

calculate_totals() public method

Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total.
Since: 2.2
public calculate_totals ( boolean $and_taxes = true ) : float
$and_taxes boolean Calc taxes if true.
return float calculated grand total.

get_cart_tax() public method

Gets cart tax amount.
public get_cart_tax ( string $context = 'view' ) : float
$context string
return float

get_currency() public method

Gets order currency.
public get_currency ( string $context = 'view' ) : string
$context string
return string

get_data() public method

Get all class data in array format.
Since: 2.7.0
public get_data ( ) : array
return array

get_date_created() public method

Get date_created.
public get_date_created ( string $context = 'view' ) : integer
$context string
return integer

get_date_modified() public method

Get date_modified.
public get_date_modified ( string $context = 'view' ) : integer
$context string
return integer

get_discount_tax() public method

Get discount_tax.
public get_discount_tax ( string $context = 'view' ) : string
$context string
return string

get_discount_to_display() public method

Get the discount amount (formatted).
Since: 2.3.0
public get_discount_to_display ( $tax_display = '' ) : string
return string

get_discount_total() public method

Get discount_total.
public get_discount_total ( string $context = 'view' ) : string
$context string
return string

get_fees() public method

Return an array of fees within this order.
public get_fees ( ) : array
return array

get_formatted_line_subtotal() public method

Gets line subtotal - formatted for display.
public get_formatted_line_subtotal ( array $item, string $tax_display = '' ) : string
$item array
$tax_display string
return string

get_formatted_order_total() public method

Gets order total - formatted for display.

get_hook_prefix() protected method

Prefix for action and filter hooks on data.
Since: 2.7.0
protected get_hook_prefix ( ) : string
return string

get_item() public method

Get an order item object, based on it's type.
Since: 2.7.0
public get_item ( integer $item_id ) : WC_Order_Item
$item_id integer
return WC_Order_Item

get_item_count() public method

Gets the count of order items of a certain type.
public get_item_count ( string $item_type = '' ) : string
$item_type string
return string

get_item_subtotal() public method

Get item subtotal - this is the cost before discount.
public get_item_subtotal ( object $item, boolean $inc_tax = false, boolean $round = true ) : float
$item object
$inc_tax boolean (default: false).
$round boolean (default: true).
return float

get_item_tax() public method

Get item tax - useful for gateways.
public get_item_tax ( mixed $item, boolean $round = true ) : float
$item mixed
$round boolean (default: true).
return float

get_item_total() public method

Calculate item cost - useful for gateways.
public get_item_total ( object $item, boolean $inc_tax = false, boolean $round = true ) : float
$item object
$inc_tax boolean (default: false).
$round boolean (default: true).
return float

get_items() public method

Return an array of items/products within this order.
public get_items ( string | array $types = 'line_item' ) : Array
$types string | array Types of line items to get (array or string).
return Array of WC_Order_item

get_items_key() protected method

Get key for where a certain item type is stored in _items.
Since: 2.7.0
protected get_items_key ( $item ) : string
$item object Order item (product, shipping, fee, coupon, tax)
return string

get_items_tax_classes() public method

Get all tax classes for items in the order.
Since: 2.6.3
public get_items_tax_classes ( ) : array
return array

get_line_subtotal() public method

Get line subtotal - this is the cost before discount.
public get_line_subtotal ( object $item, boolean $inc_tax = false, boolean $round = true ) : float
$item object
$inc_tax boolean (default: false).
$round boolean (default: true).
return float

get_line_tax() public method

Get line tax - useful for gateways.
public get_line_tax ( mixed $item ) : float
$item mixed
return float

get_line_total() public method

Calculate line total - useful for gateways.
public get_line_total ( object $item, boolean $inc_tax = false, boolean $round = true ) : float
$item object
$inc_tax boolean (default: false).
$round boolean (default: true).
return float

get_order_item_totals() public method

Get totals for display on pages and in emails.
public get_order_item_totals ( mixed $tax_display = '' ) : array
$tax_display mixed
return array

get_parent_id() public method

Get parent order ID.
Since: 2.7.0
public get_parent_id ( string $context = 'view' ) : integer
$context string
return integer

get_payment_tokens() public method

Returns a list of all payment tokens associated with the current order
Since: 2.6
public get_payment_tokens ( ) : array
return array An array of payment token objects

get_prices_include_tax() public method

Get prices_include_tax.
public get_prices_include_tax ( string $context = 'view' ) : boolean
$context string
return boolean

get_shipping_method() public method

Gets formatted shipping method title.
public get_shipping_method ( ) : string
return string

get_shipping_methods() public method

Return an array of shipping costs within this order.
public get_shipping_methods ( ) : array
return array

get_shipping_tax() public method

Get shipping_tax.
public get_shipping_tax ( string $context = 'view' ) : string
$context string
return string

get_shipping_to_display() public method

Gets shipping (formatted).
public get_shipping_to_display ( $tax_display = '' ) : string
return string

get_shipping_total() public method

Get shipping_total.
public get_shipping_total ( string $context = 'view' ) : string
$context string
return string

get_status() public method

Return the order statuses without wc- internal prefix.
public get_status ( string $context = 'view' ) : string
$context string
return string

get_subtotal() public method

Gets order subtotal.
public get_subtotal ( ) : float
return float

get_subtotal_to_display() public method

Gets subtotal - subtotal is shown before discounts, but with localised taxes.
public get_subtotal_to_display ( boolean $compound = false, string $tax_display = '' ) : string
$compound boolean (default: false).
$tax_display string (default: the tax_display_cart value).
return string

get_tax_totals() public method

Get taxes, merged by code, formatted ready for output.
public get_tax_totals ( ) : array
return array

get_taxes() public method

Return an array of taxes within this order.
public get_taxes ( ) : array
return array

get_total() public method

Gets order grand total. incl. taxes. Used in gateways.
public get_total ( string $context = 'view' ) : float
$context string
return float

get_total_discount() public method

Gets the total discount amount.
public get_total_discount ( boolean $ex_tax = true ) : float
$ex_tax boolean Show discount excl any tax.
return float

get_total_tax() public method

Get total tax amount. Alias for get_order_tax().
public get_total_tax ( string $context = 'view' ) : float
$context string
return float

get_type() public method

Get internal type.
public get_type ( ) : string
return string

get_used_coupons() public method

Get coupon codes only.
public get_used_coupons ( ) : array
return array

get_version() public method

Get order_version.
public get_version ( string $context = 'view' ) : string
$context string
return string

has_free_item() public method

Returns true if the order contains a free product.
Since: 2.5.0
public has_free_item ( ) : boolean
return boolean

has_shipping_method() public method

Check whether this order has a specific shipping method or not.
public has_shipping_method ( string $method_id ) : boolean
$method_id string
return boolean

has_status() public method

Checks the order status against a passed in status.
public has_status ( $status ) : boolean
return boolean

remove_item() public method

Remove item from the order.
public remove_item ( integer $item_id )
$item_id integer

remove_order_items() public method

Remove all line items (products, coupons, shipping, taxes) from the order.
public remove_order_items ( string $type = null )
$type string Order item type. Default null.

save() public method

Save data to the database.
Since: 2.7.0
public save ( ) : integer
return integer order ID

save_items() protected method

Save all order items which are part of this order.
protected save_items ( )

set_cart_tax() public method

Set cart tax.
public set_cart_tax ( string $value )
$value string

set_currency() public method

Set order_currency.
public set_currency ( string $value )
$value string

set_date_created() public method

Set date_created.
public set_date_created ( string $timestamp )
$timestamp string Timestamp

set_date_modified() public method

Set date_modified.
public set_date_modified ( string $timestamp )
$timestamp string

set_discount_tax() public method

Set discount_tax.
public set_discount_tax ( string $value )
$value string

set_discount_total() public method

Set discount_total.
public set_discount_total ( string $value )
$value string

set_parent_id() public method

Set parent order ID.
Since: 2.7.0
public set_parent_id ( integer $value )
$value integer

set_prices_include_tax() public method

Set prices_include_tax.
public set_prices_include_tax ( boolean $value )
$value boolean

set_shipping_tax() public method

Set shipping_tax.
public set_shipping_tax ( string $value )
$value string

set_shipping_total() public method

Set shipping_total.
public set_shipping_total ( string $value )
$value string

set_status() public method

Set order status.
Since: 2.7.0
public set_status ( string $new_status ) : array
$new_status string Status to change the order to. No internal wc- prefix is required.
return array details of change

set_total() public method

Set total.
public set_total ( string $value, string $deprecated = '' )
$value string
$deprecated string Function used to set different totals based on this.

set_total_tax() protected method

Sets order tax (sum of cart and shipping tax). Used internaly only.
protected set_total_tax ( string $value )
$value string

set_version() public method

Set order_version.
public set_version ( string $value )
$value string

type_to_group() protected method

Convert a type to a types group.
protected type_to_group ( string $type ) : string
$type string
return string group

update_taxes() public method

Update tax lines for the order based on the line item taxes themselves.
public update_taxes ( )

Property Details

$cache_group protected property

A group must be set to to enable caching.
protected string $cache_group
return string

$data protected property

Notes: cart_tax = cart_tax is the new name for the legacy 'order_tax' which is the tax for items only, not shipping.
Since: 2.7.0
protected array $data
return array

$data_store_name protected property

Which data store to load.
protected string $data_store_name
return string

$items protected property

Order items will be stored here, sometimes before they persist in the DB.
Since: 2.7.0
protected array $items
return array

$items_to_delete protected property

Order items that need deleting are stored here.
Since: 2.7.0
protected array $items_to_delete
return array