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
파일 보기 프로젝트 열기: woocommerce/woocommerce 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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.

get_cart_contents_weight() 공개 메소드

Get weight of items in the cart.
부터: 2.5.0

get_cart_discount_tax_total() 공개 메소드

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

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

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()

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