PHP Класс 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.
Автор: WooThemes
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Открытые методы

Метод Описание
__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.

Приватные методы

Метод Описание
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.

Описание методов

__construct() публичный Метод

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

__get() публичный Метод

Auto-load in-accessible properties on demand.
public __get ( mixed $key ) : mixed
$key mixed
Результат mixed

add_discount() публичный Метод

Applies a coupon code passed to the method.
public add_discount ( string $coupon_code ) : boolean
$coupon_code string - The code to apply
Результат boolean True if the coupon is applied, false if it does not exist or cannot be applied

add_fee() публичный Метод

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() публичный Метод

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
Результат string | boolean $cart_item_key

apply_cart_discounts_after_tax() публичный Метод

Function to apply cart discounts after tax.
Устаревший: Coupons can not be applied after tax
public apply_cart_discounts_after_tax ( $values, $price )

apply_product_discounts_after_tax() публичный Метод

Function to apply product discounts after tax.
Устаревший: Coupons can not be applied after tax
public apply_product_discounts_after_tax ( $values, $price )

calculate_fees() публичный Метод

Calculate fees.
public calculate_fees ( )

calculate_shipping() публичный Метод

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

calculate_totals() публичный Метод

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

check_cart_coupons() публичный Метод

Check cart coupons for errors.
public check_cart_coupons ( )

check_cart_item_stock() публичный Метод

Looks through the cart to check each item is in stock. If not, add an error.
public check_cart_item_stock ( ) : boolean | WP_Error
Результат boolean | WP_Error

check_cart_item_validity() публичный Метод

Looks through cart items and checks the posts are not trashed or deleted.
public check_cart_item_validity ( ) : boolean | WP_Error
Результат boolean | WP_Error

check_cart_items() публичный Метод

Check all cart items for errors.
public check_cart_items ( )

check_customer_coupons() публичный Метод

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() публичный Метод

Coupons enabled function. Filterable.
Устаревший: 2.5.0 in favor to wc_coupons_enabled()
public coupons_enabled ( ) : boolean
Результат boolean

empty_cart() публичный Метод

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() публичный Метод

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
Результат string cart item key

generate_cart_id() публичный Метод

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
Результат string cart item key

get_applied_coupons() публичный Метод

Gets the array of applied coupon codes.
public get_applied_coupons ( ) : array
Результат array of applied coupons

get_cart() публичный Метод

Returns the contents of the cart in an array.
public get_cart ( ) : array
Результат array contents of the cart

get_cart_contents_count() публичный Метод

Get number of items in the cart.
public get_cart_contents_count ( ) : integer
Результат integer

get_cart_contents_weight() публичный Метод

Get weight of items in the cart.
С версии: 2.5.0
public get_cart_contents_weight ( ) : integer
Результат integer

get_cart_discount_tax_total() публичный Метод

Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
public get_cart_discount_tax_total ( ) : float
Результат float

get_cart_discount_total() публичный Метод

Get the total of all cart discounts.
public get_cart_discount_total ( ) : float
Результат float

get_cart_for_session() публичный Метод

Returns the contents of the cart in an array without the 'data' element.
public get_cart_for_session ( ) : array
Результат array contents of the cart

get_cart_from_session() публичный Метод

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

get_cart_item() публичный Метод

Returns a specific item in the cart.
public get_cart_item ( string $item_key ) : array
$item_key string Cart item key.
Результат array Item data

get_cart_item_quantities() публичный Метод

Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.
public get_cart_item_quantities ( ) : array
Результат array

get_cart_item_tax_classes() публичный Метод

Get all tax classes for items in the cart.
public get_cart_item_tax_classes ( ) : array
Результат array

get_cart_shipping_total() публичный Метод

Gets the shipping total (after calculation).
public get_cart_shipping_total ( ) : string
Результат string price or string for the shipping total

get_cart_subtotal() публичный Метод

Gets the sub total (after calculation).
public get_cart_subtotal ( boolean $compound = false ) : string
$compound boolean whether to include compound taxes
Результат string formatted price

get_cart_tax() публичный Метод

Gets the cart tax (after calculation).
public get_cart_tax ( ) : string
Результат string formatted price

get_cart_total() публичный Метод

Gets the cart contents total (after calculation).
public get_cart_total ( ) : string
Результат string formatted price

get_cart_url() публичный Метод

Gets the url to the cart page.
Устаревший: 2.5.0 in favor to wc_get_cart_url()
public get_cart_url ( ) : string
Результат string url to page

get_checkout_url() публичный Метод

Gets the url to the checkout page.
Устаревший: 2.5.0 in favor to wc_get_checkout_url()
public get_checkout_url ( ) : string
Результат string url to page

get_coupon_discount_amount() публичный Метод

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
Результат float discount amount

get_coupon_discount_tax_amount() публичный Метод

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
Результат float discount amount

get_coupons() публичный Метод

Get array of applied coupon objects and codes.
public get_coupons ( $deprecated = null ) : array
Результат array of applied coupons

get_cross_sells() публичный Метод

Gets cross sells based on the items in the cart.
public get_cross_sells ( ) : array
Результат array cross_sells (item ids)

get_discounted_price() публичный Метод

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)
Результат float price

get_discounts_after_tax() публичный Метод

Gets the order discount amount - these are applied after tax.
Устаревший: Coupons can not be applied after tax

get_discounts_before_tax() публичный Метод

Gets the total (product) discount amount - these are applied before tax.
Устаревший: Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
public get_discounts_before_tax ( ) : mixed
Результат mixed formatted price or false if there are none

get_displayed_subtotal() публичный Метод

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).
С версии: 2.6.0
public get_displayed_subtotal ( ) : string
Результат string

get_fees() публичный Метод

Get fees.
public get_fees ( ) : array
Результат array

get_item_data() публичный Метод

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)
Результат string

get_order_discount_total() публичный Метод

Get the total of all order discounts (after tax discounts).
Устаревший: Order discounts (after tax) removed in 2.3
public get_order_discount_total ( ) : integer
Результат integer

get_product_price() публичный Метод

Get the product row price per item.
public get_product_price ( WC_Product $product ) : string
$product WC_Product
Результат string formatted price

get_product_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.
public get_product_subtotal ( WC_Product $product, integer $quantity ) : string
$product WC_Product
$quantity integer
Результат string formatted price

get_remove_url() публичный Метод

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
Результат string url to page

get_shipping_packages() публичный Метод

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.
С версии: 1.5.4
public get_shipping_packages ( ) : array
Результат array of cart items

get_shipping_tax_amount() публичный Метод

Get a tax amount.
public get_shipping_tax_amount ( string $tax_rate_id ) : float
$tax_rate_id string
Результат float amount

get_tax_amount() публичный Метод

Get a tax amount.
public get_tax_amount ( string $tax_rate_id ) : float
$tax_rate_id string
Результат float amount

get_tax_totals() публичный Метод

Get taxes, merged by code, formatted ready for output.
public get_tax_totals ( ) : array
Результат array

get_taxes() публичный Метод

Returns the cart and shipping taxes, merged.
public get_taxes ( ) : array
Результат array merged taxes

get_taxes_total() публичный Метод

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
Результат float price

get_total() публичный Метод

Gets the order total (after calculation).
public get_total ( ) : string
Результат string formatted price

get_total_discount() публичный Метод

Gets the total discount amount - both kinds.
public get_total_discount ( ) : mixed
Результат mixed formatted price or false if there are none

get_total_ex_tax() публичный Метод

Gets the total excluding taxes.
public get_total_ex_tax ( ) : string
Результат string formatted price

get_undo_url() публичный Метод

Gets the url to re-add an item into the cart.
public get_undo_url ( string $cart_item_key ) : string
$cart_item_key string
Результат string url to page

has_discount() публичный Метод

Returns whether or not a discount has been applied.
public has_discount ( string $coupon_code = '' ) : boolean
$coupon_code string
Результат boolean

init() публичный Метод

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

is_empty() публичный Метод

Checks if the cart is empty.
public is_empty ( ) : boolean
Результат boolean

maybe_set_cart_cookies() публичный Метод

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

needs_payment() публичный Метод

Looks at the totals to see if payment is actually required.
public needs_payment ( ) : boolean
Результат boolean

needs_shipping() публичный Метод

Looks through the cart to see if shipping is actually required.
public needs_shipping ( ) : boolean
Результат boolean whether or not the cart needs shipping

needs_shipping_address() публичный Метод

Should the shipping address form be shown.
public needs_shipping_address ( ) : boolean
Результат boolean

persistent_cart_destroy() публичный Метод

Delete the persistent cart permanently.

persistent_cart_update() публичный Метод

Save the persistent cart when the cart is updated.

remove_cart_item() публичный Метод

Remove a cart item.
С версии: 2.3.0
public remove_cart_item ( string $cart_item_key ) : boolean
$cart_item_key string
Результат boolean

remove_coupon() публичный Метод

Remove a single coupon by code.
public remove_coupon ( string $coupon_code ) : boolean
$coupon_code string Code of the coupon to remove
Результат boolean

remove_coupons() публичный Метод

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() публичный Метод

Remove taxes.
public remove_taxes ( )

restore_cart_item() публичный Метод

Restore a cart item.
public restore_cart_item ( string $cart_item_key ) : boolean
$cart_item_key string
Результат boolean

set_quantity() публичный Метод

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
Результат boolean

set_session() публичный Метод

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

ship_to_billing_address_only() публичный Метод

Sees if we need a shipping address.
Устаревший: 2.5.0 in favor to wc_ship_to_billing_address_only()
public ship_to_billing_address_only ( ) : boolean
Результат boolean

show_shipping() публичный Метод

Sees if the customer has entered enough data to calc the shipping yet.
public show_shipping ( ) : boolean
Результат boolean

Описание свойств

$applied_coupons публичное свойство

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

$cart_contents публичное свойство

Contains an array of cart items.
public $cart_contents

$cart_contents_total публичное свойство

The total cost of the cart items.
public $cart_contents_total

$cart_session_data публичное свойство

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

$coupon_applied_count публичное свойство

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

$coupon_discount_amounts публичное свойство

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

$coupon_discount_tax_amounts публичное свойство

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

$coupons публичное свойство

Array of coupons
public $coupons

$discount_cart публичное свойство

Discount amount before tax
public $discount_cart

$discount_cart_tax публичное свойство

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

$fee_total публичное свойство

Total for additional fees.
public $fee_total

$fees публичное свойство

An array of fees.
public array $fees
Результат array

$removed_cart_contents публичное свойство

Contains an array of removed cart items.
public $removed_cart_contents

$shipping_tax_total публичное свойство

Shipping tax.
public $shipping_tax_total

$shipping_taxes публичное свойство

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

$shipping_total публичное свойство

Shipping cost.
public $shipping_total

$subtotal публичное свойство

Cart subtotal.
public $subtotal

$subtotal_ex_tax публичное свойство

Cart subtotal without tax.
public $subtotal_ex_tax

$tax_total публичное свойство

Total cart tax.
public $tax_total

$taxes публичное свойство

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

$total публичное свойство

Cart grand total.
public $total