PHP Class WC_Cart

The WooCommerce cart class stores cart data and active coupons as well as handling customer sessions and some cart related urls. The cart class also has a price calculation function which calls upon other classes to calculate totals.
Author: WooThemes
Show file Open project: woocommerce/woocommerce Class Usage Examples

Public Properties

Property Type Description
$applied_coupons Contains an array of coupon codes applied to the cart.
$cart_contents Contains an array of cart items.
$cart_contents_total The total cost of the cart items.
$cart_session_data cart_session_data. Array of data the cart calculates and stores in the session with defaults
$coupon_applied_count Contains an array of coupon usage counts after they have been applied.
$coupon_discount_amounts Contains an array of coupon code discounts after they have been applied.
$coupon_discount_tax_amounts Contains an array of coupon code discount taxes. Used for tax incl pricing.
$coupons Array of coupons
$discount_cart Discount amount before tax
$discount_cart_tax Discounted tax amount. Used predominantly for displaying tax inclusive prices correctly
$fee_total Total for additional fees.
$fees array An array of fees.
$removed_cart_contents Contains an array of removed cart items.
$shipping_tax_total Shipping tax.
$shipping_taxes An array of taxes/tax rates for the shipping.
$shipping_total Shipping cost.
$subtotal Cart subtotal.
$subtotal_ex_tax Cart subtotal without tax.
$tax_total Total cart tax.
$taxes An array of taxes/tax rates for the cart.
$total Cart grand total.

Public Methods

Method Description
__construct ( ) Constructor for the cart class. Loads options and hooks in the init method.
__get ( mixed $key ) : mixed Auto-load in-accessible properties on demand.
add_discount ( string $coupon_code ) : boolean Applies a coupon code passed to the method.
add_fee ( string $name, float $amount, boolean $taxable = false, string $tax_class = '' ) Add additional fee to the cart.
add_to_cart ( integer $product_id, integer $quantity = 1, integer $variation_id, array $variation = [], array $cart_item_data = [] ) : string | boolean Add a product to the cart.
apply_cart_discounts_after_tax ( $values, $price ) Function to apply cart discounts after tax.
apply_product_discounts_after_tax ( $values, $price ) Function to apply product discounts after tax.
calculate_fees ( ) Calculate fees.
calculate_shipping ( ) Uses the shipping class to calculate shipping then gets the totals when its finished.
calculate_totals ( ) Calculate totals for the items in the cart.
check_cart_coupons ( ) Check cart coupons for errors.
check_cart_item_stock ( ) : boolean | WP_Error Looks through the cart to check each item is in stock. If not, add an error.
check_cart_item_validity ( ) : boolean | WP_Error Looks through cart items and checks the posts are not trashed or deleted.
check_cart_items ( ) Check all cart items for errors.
check_customer_coupons ( array $posted ) Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.
coupons_enabled ( ) : boolean Coupons enabled function. Filterable.
empty_cart ( boolean $clear_persistent_cart = true ) Empties the cart and optionally the persistent cart too.
find_product_in_cart ( $cart_id = false ) : string Check if product is in the cart and return cart item key.
generate_cart_id ( integer $product_id, integer $variation_id, array $variation = [], array $cart_item_data = [] ) : string Generate a unique ID for the cart item being added.
get_applied_coupons ( ) : array Gets the array of applied coupon codes.
get_cart ( ) : array Returns the contents of the cart in an array.
get_cart_contents_count ( ) : integer Get number of items in the cart.
get_cart_contents_weight ( ) : integer Get weight of items in the cart.
get_cart_discount_tax_total ( ) : float Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
get_cart_discount_total ( ) : float Get the total of all cart discounts.
get_cart_for_session ( ) : array Returns the contents of the cart in an array without the 'data' element.
get_cart_from_session ( ) Get the cart data from the PHP session and store it in class variables.
get_cart_item ( string $item_key ) : array Returns a specific item in the cart.
get_cart_item_quantities ( ) : array Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.
get_cart_item_tax_classes ( ) : array Get all tax classes for items in the cart.
get_cart_shipping_total ( ) : string Gets the shipping total (after calculation).
get_cart_subtotal ( boolean $compound = false ) : string Gets the sub total (after calculation).
get_cart_tax ( ) : string Gets the cart tax (after calculation).
get_cart_total ( ) : string Gets the cart contents total (after calculation).
get_cart_url ( ) : string Gets the url to the cart page.
get_checkout_url ( ) : string Gets the url to the checkout page.
get_coupon_discount_amount ( string $code, boolean $ex_tax = true ) : float Get the discount amount for a used coupon.
get_coupon_discount_tax_amount ( string $code ) : float Get the discount tax amount for a used coupon (for tax inclusive prices).
get_coupons ( $deprecated = null ) : array Get array of applied coupon objects and codes.
get_cross_sells ( ) : array Gets cross sells based on the items in the cart.
get_discounted_price ( mixed $values, mixed $price, boolean $add_totals = false ) : float Function to apply discounts to a product and get the discounted price (before tax is applied).
get_discounts_after_tax ( ) Gets the order discount amount - these are applied after tax.
get_discounts_before_tax ( ) : mixed Gets the total (product) discount amount - these are applied before tax.
get_displayed_subtotal ( ) : string Determines the value that the customer spent and the subtotal displayed, used for things like coupon validation.
get_fees ( ) : array Get fees.
get_item_data ( array $cart_item, boolean $flat = false ) : string Gets and formats a list of cart item data + variations for display on the frontend.
get_order_discount_total ( ) : integer Get the total of all order discounts (after tax discounts).
get_product_price ( WC_Product $product ) : string Get the product row price per item.
get_product_subtotal ( WC_Product $product, integer $quantity ) : string Get the product row subtotal.
get_remove_url ( string $cart_item_key ) : string Gets the url to remove an item from the cart.
get_shipping_packages ( ) : array Get packages to calculate shipping for.
get_shipping_tax_amount ( string $tax_rate_id ) : float Get a tax amount.
get_tax_amount ( string $tax_rate_id ) : float Get a tax amount.
get_tax_totals ( ) : array Get taxes, merged by code, formatted ready for output.
get_taxes ( ) : array Returns the cart and shipping taxes, merged.
get_taxes_total ( boolean $compound = true, boolean $display = true ) : float Get tax row amounts with or without compound taxes includes.
get_total ( ) : string Gets the order total (after calculation).
get_total_discount ( ) : mixed Gets the total discount amount - both kinds.
get_total_ex_tax ( ) : string Gets the total excluding taxes.
get_undo_url ( string $cart_item_key ) : string Gets the url to re-add an item into the cart.
has_discount ( string $coupon_code = '' ) : boolean Returns whether or not a discount has been applied.
init ( ) Loads the cart data from the PHP session during WordPress init and hooks in other methods.
is_empty ( ) : boolean Checks if the cart is empty.
maybe_set_cart_cookies ( ) Will set cart cookies if needed, once, during WP hook.
needs_payment ( ) : boolean Looks at the totals to see if payment is actually required.
needs_shipping ( ) : boolean Looks through the cart to see if shipping is actually required.
needs_shipping_address ( ) : boolean Should the shipping address form be shown.
persistent_cart_destroy ( ) Delete the persistent cart permanently.
persistent_cart_update ( ) Save the persistent cart when the cart is updated.
remove_cart_item ( string $cart_item_key ) : boolean Remove a cart item.
remove_coupon ( string $coupon_code ) : boolean Remove a single coupon by code.
remove_coupons ( $deprecated = null ) Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.
remove_taxes ( ) Remove taxes.
restore_cart_item ( string $cart_item_key ) : boolean Restore a cart item.
set_quantity ( string $cart_item_key, integer $quantity = 1, boolean $refresh_totals = true ) : boolean Set the quantity for an item in the cart.
set_session ( ) Sets the php session data for the cart and coupons.
ship_to_billing_address_only ( ) : boolean Sees if we need a shipping address.
show_shipping ( ) : boolean Sees if the customer has entered enough data to calc the shipping yet.

Private Methods

Method Description
increase_coupon_applied_count ( string $code, integer $count = 1 ) Store how many times each coupon is applied to cart/items.
increase_coupon_discount_amount ( string $code, double $amount, double $tax ) Store how much discount each coupon grants.
reset ( boolean $unset_session = false ) Reset cart totals to the defaults. Useful before running calculations.
set_cart_cookies ( boolean $set = true ) Set cart hash cookie and items in cart.
sort_by_subtotal ( array $a, array $b ) : integer Sort by subtotal.

Method Details

__construct() public method

Constructor for the cart class. Loads options and hooks in the init method.
public __construct ( )

__get() public method

Auto-load in-accessible properties on demand.
public __get ( mixed $key ) : mixed
$key mixed
return mixed

add_discount() public method

Applies a coupon code passed to the method.
public add_discount ( string $coupon_code ) : boolean
$coupon_code string - The code to apply
return boolean True if the coupon is applied, false if it does not exist or cannot be applied

add_fee() public method

Add additional fee to the cart.
public add_fee ( string $name, float $amount, boolean $taxable = false, string $tax_class = '' )
$name string Unique name for the fee. Multiple fees of the same name cannot be added.
$amount float Fee amount.
$taxable boolean (default: false) Is the fee taxable?
$tax_class string (default: '') The tax class for the fee if taxable. A blank string is standard tax class.

add_to_cart() public method

Add a product to the cart.
public add_to_cart ( integer $product_id, integer $quantity = 1, integer $variation_id, array $variation = [], array $cart_item_data = [] ) : string | boolean
$product_id integer contains the id of the product to add to the cart
$quantity integer contains the quantity of the item to add
$variation_id integer
$variation array attribute values
$cart_item_data array extra cart item data we want to pass into the item
return string | boolean $cart_item_key

apply_cart_discounts_after_tax() public method

Function to apply cart discounts after tax.
Deprecation: Coupons can not be applied after tax
public apply_cart_discounts_after_tax ( $values, $price )

apply_product_discounts_after_tax() public method

Function to apply product discounts after tax.
Deprecation: Coupons can not be applied after tax
public apply_product_discounts_after_tax ( $values, $price )

calculate_fees() public method

Calculate fees.
public calculate_fees ( )

calculate_shipping() public method

Uses the shipping class to calculate shipping then gets the totals when its finished.
public calculate_shipping ( )

calculate_totals() public method

Calculate totals for the items in the cart.
public calculate_totals ( )

check_cart_coupons() public method

Check cart coupons for errors.
public check_cart_coupons ( )

check_cart_item_stock() public method

Looks through the cart to check each item is in stock. If not, add an error.
public check_cart_item_stock ( ) : boolean | WP_Error
return boolean | WP_Error

check_cart_item_validity() public method

Looks through cart items and checks the posts are not trashed or deleted.
public check_cart_item_validity ( ) : boolean | WP_Error
return boolean | WP_Error

check_cart_items() public method

Check all cart items for errors.
public check_cart_items ( )

check_customer_coupons() public method

Checks two types of coupons: 1. Where a list of customer emails are set (limits coupon usage to those defined). 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email).
public check_customer_coupons ( array $posted )
$posted array

coupons_enabled() public method

Coupons enabled function. Filterable.
Deprecation: 2.5.0 in favor to wc_coupons_enabled()
public coupons_enabled ( ) : boolean
return boolean

empty_cart() public method

Empties the cart and optionally the persistent cart too.
public empty_cart ( boolean $clear_persistent_cart = true )
$clear_persistent_cart boolean (default: true)

find_product_in_cart() public method

Cart item key will be unique based on the item and its properties, such as variations.
public find_product_in_cart ( $cart_id = false ) : string
return string cart item key

generate_cart_id() public method

Generate a unique ID for the cart item being added.
public generate_cart_id ( integer $product_id, integer $variation_id, array $variation = [], array $cart_item_data = [] ) : string
$product_id integer - id of the product the key is being generated for
$variation_id integer of the product the key is being generated for
$variation array data for the cart item
$cart_item_data array other cart item data passed which affects this items uniqueness in the cart
return string cart item key

get_applied_coupons() public method

Gets the array of applied coupon codes.
public get_applied_coupons ( ) : array
return array of applied coupons

get_cart() public method

Returns the contents of the cart in an array.
public get_cart ( ) : array
return array contents of the cart

get_cart_contents_count() public method

Get number of items in the cart.

get_cart_contents_weight() public method

Get weight of items in the cart.
Since: 2.5.0

get_cart_discount_tax_total() public method

Get the total of all cart tax discounts (used for discounts on tax inclusive prices).

get_cart_discount_total() public method

Get the total of all cart discounts.
public get_cart_discount_total ( ) : float
return float

get_cart_for_session() public method

Returns the contents of the cart in an array without the 'data' element.
public get_cart_for_session ( ) : array
return array contents of the cart

get_cart_from_session() public method

Get the cart data from the PHP session and store it in class variables.

get_cart_item() public method

Returns a specific item in the cart.
public get_cart_item ( string $item_key ) : array
$item_key string Cart item key.
return array Item data

get_cart_item_quantities() public method

Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.
public get_cart_item_quantities ( ) : array
return array

get_cart_item_tax_classes() public method

Get all tax classes for items in the cart.
public get_cart_item_tax_classes ( ) : array
return array

get_cart_shipping_total() public method

Gets the shipping total (after calculation).
public get_cart_shipping_total ( ) : string
return string price or string for the shipping total

get_cart_subtotal() public method

Gets the sub total (after calculation).
public get_cart_subtotal ( boolean $compound = false ) : string
$compound boolean whether to include compound taxes
return string formatted price

get_cart_tax() public method

Gets the cart tax (after calculation).
public get_cart_tax ( ) : string
return string formatted price

get_cart_total() public method

Gets the cart contents total (after calculation).
public get_cart_total ( ) : string
return string formatted price

get_cart_url() public method

Gets the url to the cart page.
Deprecation: 2.5.0 in favor to wc_get_cart_url()
public get_cart_url ( ) : string
return string url to page

get_checkout_url() public method

Gets the url to the checkout page.
Deprecation: 2.5.0 in favor to wc_get_checkout_url()
public get_checkout_url ( ) : string
return string url to page

get_coupon_discount_amount() public method

Get the discount amount for a used coupon.
public get_coupon_discount_amount ( string $code, boolean $ex_tax = true ) : float
$code string coupon code
$ex_tax boolean inc or ex tax
return float discount amount

get_coupon_discount_tax_amount() public method

Get the discount tax amount for a used coupon (for tax inclusive prices).
public get_coupon_discount_tax_amount ( string $code ) : float
$code string coupon code
return float discount amount

get_coupons() public method

Get array of applied coupon objects and codes.
public get_coupons ( $deprecated = null ) : array
return array of applied coupons

get_cross_sells() public method

Gets cross sells based on the items in the cart.
public get_cross_sells ( ) : array
return array cross_sells (item ids)

get_discounted_price() public method

Function to apply discounts to a product and get the discounted price (before tax is applied).
public get_discounted_price ( mixed $values, mixed $price, boolean $add_totals = false ) : float
$values mixed
$price mixed
$add_totals boolean (default: false)
return float price

get_discounts_after_tax() public method

Gets the order discount amount - these are applied after tax.
Deprecation: Coupons can not be applied after tax

get_discounts_before_tax() public method

Gets the total (product) discount amount - these are applied before tax.
Deprecation: Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
public get_discounts_before_tax ( ) : mixed
return mixed formatted price or false if there are none

get_displayed_subtotal() public method

Since the coupon lines are displayed based on the TAX DISPLAY value of cart, this is used to determine the spend. If cart totals are shown including tax, use the subtotal. If cart totals are shown excluding tax, use the subtotal ex tax (tax is shown after coupons).
Since: 2.6.0
public get_displayed_subtotal ( ) : string
return string

get_fees() public method

Get fees.
public get_fees ( ) : array
return array

get_item_data() public method

Gets and formats a list of cart item data + variations for display on the frontend.
public get_item_data ( array $cart_item, boolean $flat = false ) : string
$cart_item array
$flat boolean (default: false)
return string

get_order_discount_total() public method

Get the total of all order discounts (after tax discounts).
Deprecation: Order discounts (after tax) removed in 2.3

get_product_price() public method

Get the product row price per item.
public get_product_price ( WC_Product $product ) : string
$product WC_Product
return string formatted price

get_product_subtotal() public method

Gets the tax etc to avoid rounding issues. When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate.
public get_product_subtotal ( WC_Product $product, integer $quantity ) : string
$product WC_Product
$quantity integer
return string formatted price

get_remove_url() public method

Gets the url to remove an item from the cart.
public get_remove_url ( string $cart_item_key ) : string
$cart_item_key string contains the id of the cart item
return string url to page

get_shipping_packages() public method

This lets us calculate costs for carts that are shipped to multiple locations. Shipping methods are responsible for looping through these packages. By default we pass the cart itself as a package - plugins can change this. through the filter and break it up.
Since: 1.5.4
public get_shipping_packages ( ) : array
return array of cart items

get_shipping_tax_amount() public method

Get a tax amount.
public get_shipping_tax_amount ( string $tax_rate_id ) : float
$tax_rate_id string
return float amount

get_tax_amount() public method

Get a tax amount.
public get_tax_amount ( string $tax_rate_id ) : float
$tax_rate_id string
return float amount

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

Returns the cart and shipping taxes, merged.
public get_taxes ( ) : array
return array merged taxes

get_taxes_total() public method

Get tax row amounts with or without compound taxes includes.
public get_taxes_total ( boolean $compound = true, boolean $display = true ) : float
$compound boolean True if getting compound taxes
$display boolean True if getting total to display
return float price

get_total() public method

Gets the order total (after calculation).
public get_total ( ) : string
return string formatted price

get_total_discount() public method

Gets the total discount amount - both kinds.
public get_total_discount ( ) : mixed
return mixed formatted price or false if there are none

get_total_ex_tax() public method

Gets the total excluding taxes.
public get_total_ex_tax ( ) : string
return string formatted price

get_undo_url() public method

Gets the url to re-add an item into the cart.
public get_undo_url ( string $cart_item_key ) : string
$cart_item_key string
return string url to page

has_discount() public method

Returns whether or not a discount has been applied.
public has_discount ( string $coupon_code = '' ) : boolean
$coupon_code string
return boolean

init() public method

Loads the cart data from the PHP session during WordPress init and hooks in other methods.
public init ( )

is_empty() public method

Checks if the cart is empty.
public is_empty ( ) : boolean
return boolean

maybe_set_cart_cookies() public method

Will set cart cookies if needed, once, during WP hook.

needs_payment() public method

Looks at the totals to see if payment is actually required.
public needs_payment ( ) : boolean
return boolean

needs_shipping() public method

Looks through the cart to see if shipping is actually required.
public needs_shipping ( ) : boolean
return boolean whether or not the cart needs shipping

needs_shipping_address() public method

Should the shipping address form be shown.
public needs_shipping_address ( ) : boolean
return boolean

persistent_cart_destroy() public method

Delete the persistent cart permanently.

persistent_cart_update() public method

Save the persistent cart when the cart is updated.

remove_cart_item() public method

Remove a cart item.
Since: 2.3.0
public remove_cart_item ( string $cart_item_key ) : boolean
$cart_item_key string
return boolean

remove_coupon() public method

Remove a single coupon by code.
public remove_coupon ( string $coupon_code ) : boolean
$coupon_code string Code of the coupon to remove
return boolean

remove_coupons() public method

Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.
public remove_coupons ( $deprecated = null )

remove_taxes() public method

Remove taxes.
public remove_taxes ( )

restore_cart_item() public method

Restore a cart item.
public restore_cart_item ( string $cart_item_key ) : boolean
$cart_item_key string
return boolean

set_quantity() public method

Set the quantity for an item in the cart.
public set_quantity ( string $cart_item_key, integer $quantity = 1, boolean $refresh_totals = true ) : boolean
$cart_item_key string contains the id of the cart item
$quantity integer contains the quantity of the item
$refresh_totals boolean whether or not to calculate totals after setting the new qty
return boolean

set_session() public method

Sets the php session data for the cart and coupons.
public set_session ( )

ship_to_billing_address_only() public method

Sees if we need a shipping address.
Deprecation: 2.5.0 in favor to wc_ship_to_billing_address_only()

show_shipping() public method

Sees if the customer has entered enough data to calc the shipping yet.
public show_shipping ( ) : boolean
return boolean

Property Details

$applied_coupons public property

Contains an array of coupon codes applied to the cart.
public $applied_coupons

$cart_contents public property

Contains an array of cart items.
public $cart_contents

$cart_contents_total public property

The total cost of the cart items.
public $cart_contents_total

$cart_session_data public property

cart_session_data. Array of data the cart calculates and stores in the session with defaults
public $cart_session_data

$coupon_applied_count public property

Contains an array of coupon usage counts after they have been applied.
public $coupon_applied_count

$coupon_discount_amounts public property

Contains an array of coupon code discounts after they have been applied.
public $coupon_discount_amounts

$coupon_discount_tax_amounts public property

Contains an array of coupon code discount taxes. Used for tax incl pricing.
public $coupon_discount_tax_amounts

$coupons public property

Array of coupons
public $coupons

$discount_cart public property

Discount amount before tax
public $discount_cart

$discount_cart_tax public property

Discounted tax amount. Used predominantly for displaying tax inclusive prices correctly
public $discount_cart_tax

$fee_total public property

Total for additional fees.
public $fee_total

$fees public property

An array of fees.
public array $fees
return array

$removed_cart_contents public property

Contains an array of removed cart items.
public $removed_cart_contents

$shipping_tax_total public property

Shipping tax.
public $shipping_tax_total

$shipping_taxes public property

An array of taxes/tax rates for the shipping.
public $shipping_taxes

$shipping_total public property

Shipping cost.
public $shipping_total

$subtotal public property

Cart subtotal.
public $subtotal

$subtotal_ex_tax public property

Cart subtotal without tax.
public $subtotal_ex_tax

$tax_total public property

Total cart tax.
public $tax_total

$taxes public property

An array of taxes/tax rates for the cart.
public $taxes

$total public property

Cart grand total.
public $total