Class WC_Cart

WooCommerce 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.

Package: WooCommerce\Classes
Category: Class
Author: WooThemes
Version: 2.1.0
Class: WC_Cart
Located at includes/class-wc-cart.php

Methods summary

public
# __construct( )

Constructor for the cart class. Loads options and hooks in the init method.

Constructor for the cart class. Loads options and hooks in the init method.

public
# init( )

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

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

public
# get_cart_from_session( )

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

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

public
# set_session( )

Sets the php session data for the cart and coupons.

Sets the php session data for the cart and coupons.

public
# empty_cart( boolean $clear_persistent_cart = true )

Empties the cart and optionally the persistent cart too.

Empties the cart and optionally the persistent cart too.

Parameters

$clear_persistent_cart
boolean
$clear_persistent_cart (default: true)
public
# persistent_cart_update( )

Save the persistent cart when the cart is updated.

Save the persistent cart when the cart is updated.

public
# persistent_cart_destroy( )

Delete the persistent cart permanently.

Delete the persistent cart permanently.

public boolean
# coupons_enabled( )

Coupons enabled function. Filterable.

Coupons enabled function. Filterable.

Returns

boolean
public integer
# get_cart_contents_count( )

Get number of items in the cart.

Get number of items in the cart.

Returns

integer
public
# check_cart_items( )

Check all cart items for errors.

Check all cart items for errors.

public
# check_cart_coupons( )

Check cart coupons for errors.

Check cart coupons for errors.

public array
# get_cart_item_quantities( )

Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.

Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.

Returns

array
public boolean|WP_Error
# check_cart_item_validity( )

Looks through cart items and checks the posts are not trashed or deleted.

Looks through cart items and checks the posts are not trashed or deleted.

Returns

boolean|WP_Error
public boolean|WP_Error
# check_cart_item_stock( )

Looks through the cart to check each item is in stock. If not, add an error.

Looks through the cart to check each item is in stock. If not, add an error.

Returns

boolean|WP_Error
public string
# get_item_data( array $cart_item, boolean $flat = false )

Gets and formats a list of cart item data + variations for display on the frontend.

Gets and formats a list of cart item data + variations for display on the frontend.

Parameters

$cart_item
array
$cart_item
$flat
boolean
$flat (default: false)

Returns

string
public array
# get_cross_sells( )

Gets cross sells based on the items in the cart.

Gets cross sells based on the items in the cart.

Returns

array
cross_sells (item ids)
public string
# get_cart_url( )

Gets the url to the cart page.

Gets the url to the cart page.

Returns

string
url to page
public string
# get_checkout_url( )

Gets the url to the checkout page.

Gets the url to the checkout page.

Returns

string
url to page
public string
# get_remove_url( string $cart_item_key )

Gets the url to remove an item from the cart.

Gets the url to remove an item from the cart.

Parameters

$cart_item_key
string
cart_item_key contains the id of the cart item

Returns

string
url to page
public array
# get_cart( )

Returns the contents of the cart in an array.

Returns the contents of the cart in an array.

Returns

array
contents of the cart
public array
# get_taxes( )

Returns the cart and shipping taxes, merged.

Returns the cart and shipping taxes, merged.

Returns

array
merged taxes
public array
# get_tax_totals( )

Get taxes, merged by code, formatted ready for output.

Get taxes, merged by code, formatted ready for output.

Returns

array
public string
# find_product_in_cart( mixed $cart_id = false )

Check if product is in the cart and return cart item key.

Check if product is in the cart and return cart item key.

Cart item key will be unique based on the item and its properties, such as variations.

Parameters

$cart_id
mixed
id of product to find in the cart

Returns

string
cart item key
public string
# generate_cart_id( integer $product_id, integer $variation_id = 0, array $variation = array(), array $cart_item_data = array() )

Generate a unique ID for the cart item being added.

Generate a unique ID for the cart item being added.

Parameters

$product_id
integer
$product_id - id of the product the key is being generated for
$variation_id
integer
$variation_id of the product the key is being generated for
$variation
array
$variation data for the cart item
$cart_item_data
array
$cart_item_data other cart item data passed which affects this items uniqueness in the cart

Returns

string
cart item key
public boolean
# add_to_cart( string $product_id, string $quantity = 1, integer $variation_id = '', array $variation = '', array $cart_item_data = array() )

Add a product to the cart.

Add a product to the cart.

Parameters

$product_id
string
$product_id contains the id of the product to add to the cart
$quantity
string
$quantity contains the quantity of the item to add
$variation_id
integer
$variation_id
$variation
array
$variation attribute values
$cart_item_data
array
$cart_item_data extra cart item data we want to pass into the item

Returns

boolean
public
# set_quantity( string $cart_item_key, string $quantity = 1, boolean $refresh_totals = true )

Set the quantity for an item in the cart.

Set the quantity for an item in the cart.

Parameters

$cart_item_key
string
cart_item_key contains the id of the cart item
$quantity
string
quantity contains the quantity of the item
$refresh_totals
boolean
$refresh_totals whether or not to calculate totals after setting the new qty
public
# calculate_totals( )

Calculate totals for the items in the cart.

Calculate totals for the items in the cart.

public
# remove_taxes( )

remove_taxes function.

remove_taxes function.

public boolean
# needs_payment( )

looks at the totals to see if payment is actually required.

looks at the totals to see if payment is actually required.

Returns

boolean
public
# calculate_shipping( )

Uses the shipping class to calculate shipping then gets the totals when its finished.

Uses the shipping class to calculate shipping then gets the totals when its finished.

public array
# get_shipping_packages( )

Get packages to calculate shipping for.

Get packages to calculate shipping for.

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.

Returns

array
of cart items

Since

1.5.4
public boolean
# needs_shipping( )

Looks through the cart to see if shipping is actually required.

Looks through the cart to see if shipping is actually required.

Returns

boolean
whether or not the cart needs shipping
public boolean
# needs_shipping_address( )

Should the shipping address form be shown

Should the shipping address form be shown

Returns

boolean
public boolean
# show_shipping( )

Sees if the customer has entered enough data to calc the shipping yet.

Sees if the customer has entered enough data to calc the shipping yet.

Returns

boolean
public boolean
# ship_to_billing_address_only( )

Sees if we need a shipping address.

Sees if we need a shipping address.

Returns

boolean
public string
# get_cart_shipping_total( )

Gets the shipping total (after calculation).

Gets the shipping total (after calculation).

Returns

string
price or string for the shipping total
public
# check_customer_coupons( array $posted )

Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.

Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.

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)

Parameters

$posted
array
$posted
public boolean
# has_discount( mixed $coupon_code )

Returns whether or not a discount has been applied.

Returns whether or not a discount has been applied.

Returns

boolean
public boolean
# add_discount( string $coupon_code )

Applies a coupon code passed to the method.

Applies a coupon code passed to the method.

Parameters

$coupon_code
string
$coupon_code - The code to apply

Returns

boolean
True if the coupon is applied, false if it does not exist or cannot be applied
public array
# get_coupons( string $type = null )

Get array of applied coupon objects and codes.

Get array of applied coupon objects and codes.

Parameters

$type
string
Type of coupons to get. Can be 'cart' or 'order' which are before and after tax respectively.

Returns

array
of applied coupons
public array
# get_applied_coupons( )

Gets the array of applied coupon codes.

Gets the array of applied coupon codes.

Returns

array
of applied coupons
public
# remove_coupons( mixed $type = null )

Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.

Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.

Params

string type - cart for before tax, order for after tax
public boolean
# remove_coupon( string $coupon_code )

Remove a single coupon by code

Remove a single coupon by code

Parameters

$coupon_code
string
$coupon_code Code of the coupon to remove

Returns

boolean
public float
# get_discounted_price( mixed $values, mixed $price, boolean $add_totals = false )

Function to apply discounts to a product and get the discounted price (before tax is applied).

Function to apply discounts to a product and get the discounted price (before tax is applied).

Parameters

$values
mixed
$values
$price
mixed
$price
$add_totals
boolean
$add_totals (default: false)

Returns

float
price
public
# apply_cart_discounts_after_tax( )

Function to apply cart discounts after tax.

Function to apply cart discounts after tax.

public
# apply_product_discounts_after_tax( mixed $values, mixed $price )

Function to apply product discounts after tax.

Function to apply product discounts after tax.

Parameters

$values
mixed
$values
$price
mixed
$price
public
# add_fee( mixed $name, mixed $amount, boolean $taxable = false, string $tax_class = '' )

add_fee function.

add_fee function.

Parameters

$name
mixed
$name
$amount
mixed
$amount
$taxable
boolean
$taxable (default: false)
$tax_class
string
$tax_class (default: '')
public array
# get_fees( )

get_fees function.

get_fees function.

Returns

array
public
# calculate_fees( )

Calculate fees

Calculate fees

public float
# get_order_discount_total( )

Get the total of all order discounts (after tax discounts).

Get the total of all order discounts (after tax discounts).

Returns

float
public float
# get_cart_discount_total( )

Get the total of all cart discounts (before tax discounts).

Get the total of all cart discounts (before tax discounts).

Returns

float
public string
# get_total( )

Gets the order total (after calculation).

Gets the order total (after calculation).

Returns

string
formatted price
public string
# get_total_ex_tax( )

Gets the total excluding taxes.

Gets the total excluding taxes.

Returns

string
formatted price
public string
# get_cart_total( )

Gets the cart contents total (after calculation).

Gets the cart contents total (after calculation).

Returns

string
formatted price
public string
# get_cart_subtotal( mixed $compound = false )

Gets the sub total (after calculation).

Gets the sub total (after calculation).

Returns

string
formatted price

Params

bool whether to include compound taxes
public string
# get_product_price( WC_Product $_product )

Get the product row price per item.

Get the product row price per item.

Parameters

$_product
WC_Product
$_product

Returns

string
formatted price
public string
# get_product_subtotal( WC_Product $_product, integer $quantity )

Get the product row subtotal.

Get the product row subtotal.

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

Parameters

$_product
WC_Product
$_product
$quantity
integer
quantity

Returns

string
formatted price
public string
# get_cart_tax( )

Gets the cart tax (after calculation).

Gets the cart tax (after calculation).

Returns

string
formatted price
public float
# get_taxes_total( boolean $compound = true, boolean $display = true )

Get tax row amounts with or without compound taxes includes.

Get tax row amounts with or without compound taxes includes.

Parameters

$compound
boolean
$compound True if getting compound taxes
$display
boolean
$display True if getting total to display

Returns

float
price
public mixed
# get_discounts_before_tax( )

Gets the total (product) discount amount - these are applied before tax.

Gets the total (product) discount amount - these are applied before tax.

Returns

mixed
formatted price or false if there are none
public mixed
# get_discounts_after_tax( )

Gets the order discount amount - these are applied after tax.

Gets the order discount amount - these are applied after tax.

Returns

mixed
formatted price or false if there are none
public mixed
# get_total_discount( )

Gets the total discount amount - both kinds.

Gets the total discount amount - both kinds.

Returns

mixed
formatted price or false if there are none

Magic methods summary

Properties summary

public array $cart_contents
#

Contains an array of cart items.

Contains an array of cart items.

public array $applied_coupons
#

Contains an array of coupon codes applied to the cart.

Contains an array of coupon codes applied to the cart.

public array $coupon_discount_amounts
#

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

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

public array $coupon_applied_count
#

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

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

public float $cart_contents_total
#

The total cost of the cart items.

The total cost of the cart items.

public float $cart_contents_weight
#

The total weight of the cart items.

The total weight of the cart items.

public float $cart_contents_count
#

The total count of the cart items.

The total count of the cart items.

public float $cart_contents_tax
#

The total tax for the cart items.

The total tax for the cart items.

public float $total
#

Cart grand total.

Cart grand total.

public float $subtotal
#

Cart subtotal.

Cart subtotal.

public float $subtotal_ex_tax
#

Cart subtotal without tax.

Cart subtotal without tax.

public float $tax_total
#

Total cart tax.

Total cart tax.

public array $taxes
#

An array of taxes/tax rates for the cart.

An array of taxes/tax rates for the cart.

public array $shipping_taxes
#

An array of taxes/tax rates for the shipping.

An array of taxes/tax rates for the shipping.

public float $discount_cart
#

Discounts before tax.

Discounts before tax.

public float $discount_total
#

Discounts after tax.

Discounts after tax.

public float $fee_total
#

Total for additional fees.

Total for additional fees.

public float $shipping_total
#

Shipping cost.

Shipping cost.

public float $shipping_tax_total
#

Shipping tax.

Shipping tax.

public WC_Tax $tax
#
public array $cart_session_data
#

cart_session_data

cart_session_data

public array $fees
#

An array of fees.

An array of fees.

WooCommerce API documentation generated by ApiGen 2.8.0