2011-08-09 15:16:18 +00:00
< ? php
/**
2011-08-10 17:11:11 +00:00
* WooCommerce cart
2012-08-06 12:24:59 +00:00
*
2011-08-10 17:11:11 +00:00
* The WooCommerce cart class stores cart data and active coupons as well as handling customer sessions and some cart related urls .
2012-01-30 19:24:52 +00:00
* The cart class also has a price calculation function which calls upon other classes to calculate totals .
2011-08-09 15:16:18 +00:00
*
2012-08-14 19:42:38 +00:00
* @ class WC_Cart
2013-09-12 13:41:02 +00:00
* @ version 2.1 . 0
2012-08-14 22:43:48 +00:00
* @ package WooCommerce / Classes
2013-02-20 17:14:46 +00:00
* @ category Class
2012-08-14 19:42:38 +00:00
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Cart {
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var array Contains an array of cart items. */
2013-10-18 17:10:55 +00:00
public $cart_contents = array ();
2012-08-14 19:42:38 +00:00
2012-12-11 17:02:08 +00:00
/** @var array Contains an array of coupon codes applied to the cart. */
2013-10-18 17:10:55 +00:00
public $applied_coupons = array ();
2012-12-11 17:02:08 +00:00
/** @var array Contains an array of coupon code discounts after they have been applied. */
2013-10-18 17:10:55 +00:00
public $coupon_discount_amounts = array ();
/** @var array Contains an array of coupon usage counts after they have been applied. */
public $coupon_applied_count = array ();
2012-12-11 17:02:08 +00:00
2012-08-15 17:08:42 +00:00
/** @var float The total cost of the cart items. */
2012-12-14 21:26:07 +00:00
public $cart_contents_total ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float The total weight of the cart items. */
2012-12-14 21:26:07 +00:00
public $cart_contents_weight ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float The total count of the cart items. */
2012-12-14 21:26:07 +00:00
public $cart_contents_count ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float The total tax for the cart items. */
2012-12-14 21:26:07 +00:00
public $cart_contents_tax ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Cart grand total. */
2012-12-14 21:26:07 +00:00
public $total ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Cart subtotal. */
2012-12-14 21:26:07 +00:00
public $subtotal ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Cart subtotal without tax. */
2012-12-14 21:26:07 +00:00
public $subtotal_ex_tax ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Total cart tax. */
2012-12-14 21:26:07 +00:00
public $tax_total ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var array An array of taxes/tax rates for the cart. */
2012-12-14 21:26:07 +00:00
public $taxes ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var array An array of taxes/tax rates for the shipping. */
2012-12-14 21:26:07 +00:00
public $shipping_taxes ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Discounts before tax. */
2012-12-14 21:26:07 +00:00
public $discount_cart ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Discounts after tax. */
2012-12-14 21:26:07 +00:00
public $discount_total ;
2012-11-27 16:22:47 +00:00
2013-03-03 17:07:31 +00:00
/** @var float Total for additional fees. */
2012-12-14 21:26:07 +00:00
public $fee_total ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Shipping cost. */
2012-12-14 21:26:07 +00:00
public $shipping_total ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Shipping tax. */
2012-12-14 21:26:07 +00:00
public $shipping_tax_total ;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var WC_Tax */
2012-12-14 21:26:07 +00:00
public $tax ;
2012-11-27 16:22:47 +00:00
2014-02-17 10:16:02 +00:00
/** @var array cart_session_data */
public $cart_session_data = array ();
2012-11-12 16:08:05 +00:00
/** @var array An array of fees. */
2013-10-25 18:28:09 +00:00
public $fees = array ();
2012-08-06 12:24:59 +00:00
2011-11-06 13:45:18 +00:00
/**
2012-08-15 17:08:42 +00:00
* Constructor for the cart class . Loads options and hooks in the init method .
2012-03-20 13:22:35 +00:00
*
2012-08-14 19:42:38 +00:00
* @ access public
* @ return void
2011-11-06 13:45:18 +00:00
*/
2012-12-15 12:01:42 +00:00
public function __construct () {
2013-10-25 18:28:09 +00:00
$this -> tax = new WC_Tax ();
$this -> prices_include_tax = get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ;
2014-09-14 19:48:13 +00:00
$this -> round_at_subtotal = get_option ( 'woocommerce_tax_round_at_subtotal' ) == 'yes' ;
2013-10-25 18:28:09 +00:00
$this -> tax_display_cart = get_option ( 'woocommerce_tax_display_cart' );
$this -> dp = absint ( get_option ( 'woocommerce_price_num_decimals' ) );
$this -> display_totals_ex_tax = $this -> tax_display_cart == 'excl' ;
$this -> display_cart_ex_tax = $this -> tax_display_cart == 'excl' ;
2014-07-19 04:08:02 +00:00
2014-02-17 10:16:02 +00:00
// Array of data the cart calculates and stores in the session with defaults
$this -> cart_session_data = array (
'cart_contents_total' => 0 ,
'cart_contents_weight' => 0 ,
'cart_contents_count' => 0 ,
'cart_contents_tax' => 0 ,
'total' => 0 ,
'subtotal' => 0 ,
'subtotal_ex_tax' => 0 ,
'tax_total' => 0 ,
'taxes' => array (),
'shipping_taxes' => array (),
'discount_cart' => 0 ,
'discount_total' => 0 ,
'shipping_total' => 0 ,
'shipping_tax_total' => 0 ,
'coupon_discount_amounts' => array (),
2014-08-26 14:39:15 +00:00
'fee_total' => 0 ,
'fees' => array ()
2014-02-17 10:16:02 +00:00
);
2012-12-03 16:36:54 +00:00
2012-12-15 11:53:32 +00:00
add_action ( 'init' , array ( $this , 'init' ), 5 ); // Get cart on init
2014-06-30 14:14:32 +00:00
add_action ( 'wp' , array ( $this , 'maybe_set_cart_cookies' ), 99 ); // Set cookies
2014-07-31 06:26:49 +00:00
add_action ( 'shutdown' , array ( $this , 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing
2011-08-09 15:16:18 +00:00
}
2012-08-06 12:24:59 +00:00
2014-09-14 19:29:32 +00:00
/**
2012-08-15 17:08:42 +00:00
* Loads the cart data from the PHP session during WordPress init and hooks in other methods .
2014-09-14 19:29:32 +00:00
*
* @ access public
* @ return void
*/
public function init () {
2011-09-06 11:11:22 +00:00
$this -> get_cart_from_session ();
2014-09-14 19:44:56 +00:00
2013-10-25 18:28:09 +00:00
add_action ( 'woocommerce_check_cart_items' , array ( $this , 'check_cart_items' ), 1 );
add_action ( 'woocommerce_check_cart_items' , array ( $this , 'check_cart_coupons' ), 1 );
add_action ( 'woocommerce_after_checkout_validation' , array ( $this , 'check_customer_coupons' ), 1 );
2014-09-14 19:29:32 +00:00
}
/**
2014-09-14 19:50:22 +00:00
* Will set cart cookies if needed , once , during WP hook
*
* @ access public
* @ return void
*/
2014-09-14 19:29:32 +00:00
public function maybe_set_cart_cookies () {
if ( ! headers_sent () ) {
if ( sizeof ( $this -> cart_contents ) > 0 ) {
$this -> set_cart_cookies ( true );
} elseif ( isset ( $_COOKIE [ 'woocommerce_items_in_cart' ] ) ) {
$this -> set_cart_cookies ( false );
}
}
}
2014-06-30 14:14:32 +00:00
/**
* Set cart hash cookie and items in cart .
*
* @ access private
* @ param bool $set ( default : true )
* @ return void
*/
private function set_cart_cookies ( $set = true ) {
if ( $set ) {
wc_setcookie ( 'woocommerce_items_in_cart' , 1 );
wc_setcookie ( 'woocommerce_cart_hash' , md5 ( json_encode ( $this -> get_cart () ) ) );
} elseif ( isset ( $_COOKIE [ 'woocommerce_items_in_cart' ] ) ) {
2014-09-14 19:32:14 +00:00
wc_setcookie ( 'woocommerce_items_in_cart' , 0 , time () - HOUR_IN_SECONDS );
2014-09-14 19:33:46 +00:00
wc_setcookie ( 'woocommerce_cart_hash' , '' , time () - HOUR_IN_SECONDS );
2014-06-30 14:14:32 +00:00
}
do_action ( 'woocommerce_set_cart_cookies' , $set );
}
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Cart Session Handling */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get the cart data from the PHP session and store it in class variables .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function get_cart_from_session () {
2012-08-06 12:24:59 +00:00
2014-02-17 10:16:02 +00:00
// Load cart session data from session
foreach ( $this -> cart_session_data as $key => $default ) {
2013-10-25 18:28:09 +00:00
$this -> $key = WC () -> session -> get ( $key , $default );
2014-02-17 10:16:02 +00:00
}
2013-10-25 18:28:09 +00:00
// Load coupons
$this -> applied_coupons = array_filter ( WC () -> session -> get ( 'applied_coupons' , array () ) );
2012-08-06 12:24:59 +00:00
2012-01-13 11:15:01 +00:00
// Load the cart
2013-10-18 17:10:55 +00:00
$cart = WC () -> session -> get ( 'cart' , array () );
2012-08-06 12:24:59 +00:00
2013-11-22 23:14:08 +00:00
$update_cart_session = false ;
2013-10-18 17:10:55 +00:00
if ( is_array ( $cart ) ) {
2012-03-20 13:22:35 +00:00
foreach ( $cart as $key => $values ) {
2014-08-19 10:09:29 +00:00
$_product = wc_get_product ( $values [ 'variation_id' ] ? $values [ 'variation_id' ] : $values [ 'product_id' ] );
2012-08-06 12:24:59 +00:00
2013-01-31 11:42:27 +00:00
if ( ! empty ( $_product ) && $_product -> exists () && $values [ 'quantity' ] > 0 ) {
2012-08-06 12:24:59 +00:00
2013-11-22 23:14:08 +00:00
if ( ! $_product -> is_purchasable () ) {
// Flag to indicate the stored cart should be update
$update_cart_session = true ;
wc_add_notice ( sprintf ( __ ( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.' , 'woocommerce' ), $_product -> get_title () ), 'error' );
} else {
// Put session data into array. Run through filter so other plugins can load their own session data
$this -> cart_contents [ $key ] = apply_filters ( 'woocommerce_get_cart_item_from_session' , array (
'product_id' => $values [ 'product_id' ],
'variation_id' => $values [ 'variation_id' ],
'variation' => $values [ 'variation' ],
'quantity' => $values [ 'quantity' ],
'data' => $_product
), $values , $key );
}
2012-03-20 13:22:35 +00:00
}
}
2013-04-09 09:38:40 +00:00
}
2012-08-06 12:24:59 +00:00
2014-05-08 09:35:51 +00:00
if ( $update_cart_session ) {
2013-11-22 23:16:53 +00:00
WC () -> session -> cart = $this -> get_cart_for_session ();
2014-05-08 09:35:51 +00:00
}
2013-11-22 23:14:08 +00:00
2013-04-09 09:38:40 +00:00
// Trigger action
do_action ( 'woocommerce_cart_loaded_from_session' , $this );
2012-08-06 12:24:59 +00:00
2012-03-12 09:26:43 +00:00
// Queue re-calc if subtotal is not set
2014-05-08 09:35:51 +00:00
if ( ( ! $this -> subtotal && sizeof ( $this -> cart_contents ) > 0 ) || $update_cart_session ) {
2013-01-31 14:42:46 +00:00
$this -> calculate_totals ();
2014-05-08 09:35:51 +00:00
}
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2013-01-31 14:42:46 +00:00
* Sets the php session data for the cart and coupons .
2014-09-14 19:37:48 +00:00
*
* @ access public
* @ return void
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function set_session () {
2012-01-13 11:15:01 +00:00
// Set cart and coupon session data
2013-11-22 23:16:53 +00:00
$cart_session = $this -> get_cart_for_session ();
2012-08-06 12:24:59 +00:00
2013-10-18 17:10:55 +00:00
WC () -> session -> set ( 'cart' , $cart_session );
2013-10-25 18:28:09 +00:00
WC () -> session -> set ( 'applied_coupons' , $this -> applied_coupons );
WC () -> session -> set ( 'coupon_discount_amounts' , $this -> coupon_discount_amounts );
2014-05-08 09:35:51 +00:00
foreach ( $this -> cart_session_data as $key => $default ) {
2013-10-25 18:28:09 +00:00
WC () -> session -> set ( $key , $this -> $key );
2014-05-08 09:35:51 +00:00
}
2012-09-07 17:48:30 +00:00
2014-05-08 09:35:51 +00:00
if ( get_current_user_id () ) {
2012-03-20 13:22:35 +00:00
$this -> persistent_cart_update ();
2014-05-08 09:35:51 +00:00
}
2012-08-06 12:24:59 +00:00
2012-09-07 17:48:30 +00:00
do_action ( 'woocommerce_cart_updated' );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Empties the cart and optionally the persistent cart too .
2012-03-20 13:22:35 +00:00
*
* @ access public
* @ param bool $clear_persistent_cart ( default : true )
2012-08-14 19:42:38 +00:00
* @ return void
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function empty_cart ( $clear_persistent_cart = true ) {
2011-12-08 12:50:50 +00:00
$this -> cart_contents = array ();
2012-01-04 23:01:47 +00:00
$this -> reset ();
2012-11-27 16:22:47 +00:00
2013-10-25 18:28:09 +00:00
unset ( WC () -> session -> order_awaiting_payment , WC () -> session -> applied_coupons , WC () -> session -> coupon_discount_amounts , WC () -> session -> cart );
2012-08-06 12:24:59 +00:00
2014-05-08 09:35:51 +00:00
if ( $clear_persistent_cart && get_current_user_id () ) {
2012-03-20 13:22:35 +00:00
$this -> persistent_cart_destroy ();
2014-05-08 09:35:51 +00:00
}
2012-08-06 12:24:59 +00:00
2012-09-07 17:48:30 +00:00
do_action ( 'woocommerce_cart_emptied' );
2011-12-08 12:50:50 +00:00
}
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2012-03-04 12:37:41 +00:00
/* Persistent cart handling */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2012-03-04 12:37:41 +00:00
/**
2012-08-15 17:08:42 +00:00
* Save the persistent cart when the cart is updated .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
2012-03-04 12:37:41 +00:00
*/
2012-12-15 12:01:42 +00:00
public function persistent_cart_update () {
2012-03-04 12:37:41 +00:00
update_user_meta ( get_current_user_id (), '_woocommerce_persistent_cart' , array (
2013-10-18 17:10:55 +00:00
'cart' => WC () -> session -> cart ,
2012-09-07 17:48:30 +00:00
) );
2012-03-04 12:37:41 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-04 12:37:41 +00:00
/**
2012-08-15 17:08:42 +00:00
* Delete the persistent cart permanently .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
2012-03-04 12:37:41 +00:00
*/
2012-12-15 12:01:42 +00:00
public function persistent_cart_destroy () {
2012-03-04 12:37:41 +00:00
delete_user_meta ( get_current_user_id (), '_woocommerce_persistent_cart' );
}
2012-08-06 12:24:59 +00:00
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Cart Data Functions */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-08-09 15:16:18 +00:00
2013-02-10 23:02:52 +00:00
/**
2013-02-11 13:53:24 +00:00
* Coupons enabled function . Filterable .
2013-02-10 23:02:52 +00:00
*
* @ access public
2013-11-25 12:40:16 +00:00
* @ return bool
2013-02-10 23:02:52 +00:00
*/
public function coupons_enabled () {
2013-10-25 18:28:09 +00:00
return apply_filters ( 'woocommerce_coupons_enabled' , get_option ( 'woocommerce_enable_coupons' ) == 'yes' );
2013-02-10 23:02:52 +00:00
}
2012-08-06 12:13:32 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get number of items in the cart .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return int
2012-08-06 12:13:32 +00:00
*/
2012-12-15 12:01:42 +00:00
public function get_cart_contents_count () {
2012-08-06 12:13:32 +00:00
return apply_filters ( 'woocommerce_cart_contents_count' , $this -> cart_contents_count );
}
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Check all cart items for errors .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function check_cart_items () {
2014-10-24 18:56:06 +00:00
// Result
$return = true ;
// Check cart item validity
2013-07-30 10:51:50 +00:00
$result = $this -> check_cart_item_validity ();
2014-05-08 09:35:51 +00:00
if ( is_wp_error ( $result ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $result -> get_error_message (), 'error' );
2014-10-24 18:56:06 +00:00
$return = false ;
2014-05-08 09:35:51 +00:00
}
2013-07-30 10:51:50 +00:00
2012-02-27 18:22:54 +00:00
// Check item stock
2011-12-08 12:50:50 +00:00
$result = $this -> check_cart_item_stock ();
2012-08-06 12:24:59 +00:00
2014-07-19 04:08:02 +00:00
if ( is_wp_error ( $result ) ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $result -> get_error_message (), 'error' );
2014-10-24 18:56:06 +00:00
$return = false ;
2014-05-08 09:35:51 +00:00
}
2014-10-24 18:56:06 +00:00
return $return ;
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-06-13 18:58:06 +00:00
/**
2012-08-15 17:08:42 +00:00
* Check cart coupons for errors .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
2012-06-13 18:58:06 +00:00
*/
2012-12-15 12:01:42 +00:00
public function check_cart_coupons () {
2013-10-25 18:28:09 +00:00
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
if ( ! $coupon -> is_valid () ) {
// Error message
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_INVALID_REMOVED );
2012-08-06 12:24:59 +00:00
2013-10-18 17:10:55 +00:00
// Remove the coupon
$this -> remove_coupon ( $code );
2012-12-11 17:02:08 +00:00
2013-10-18 17:10:55 +00:00
// Flag totals for refresh
WC () -> session -> set ( 'refresh_totals' , true );
2012-06-13 18:58:06 +00:00
}
}
}
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines .
2012-08-06 12:24:59 +00:00
*
2012-03-28 17:42:35 +00:00
* @ access public
* @ return array
*/
2012-12-15 12:01:42 +00:00
public function get_cart_item_quantities () {
2012-03-28 17:42:35 +00:00
$quantities = array ();
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2014-08-04 09:51:50 +00:00
$_product = $values [ 'data' ];
if ( $_product -> is_type ( 'variation' ) && true === $_product -> managing_stock () ) {
2013-10-25 18:28:09 +00:00
// Variation has stock levels defined so its handled individually
$quantities [ $values [ 'variation_id' ] ] = isset ( $quantities [ $values [ 'variation_id' ] ] ) ? $quantities [ $values [ 'variation_id' ] ] + $values [ 'quantity' ] : $values [ 'quantity' ];
} else {
$quantities [ $values [ 'product_id' ] ] = isset ( $quantities [ $values [ 'product_id' ] ] ) ? $quantities [ $values [ 'product_id' ] ] + $values [ 'quantity' ] : $values [ 'quantity' ];
2012-03-28 17:42:35 +00:00
}
2012-08-06 12:24:59 +00:00
}
2013-10-25 18:28:09 +00:00
2012-03-28 17:42:35 +00:00
return $quantities ;
}
2012-08-06 12:24:59 +00:00
2013-07-30 10:51:50 +00:00
/**
* Looks through cart items and checks the posts are not trashed or deleted .
2014-09-14 19:44:56 +00:00
*
2014-09-14 19:52:51 +00:00
* @ access public
2013-11-25 12:40:16 +00:00
* @ return bool | WP_Error
2013-07-30 10:51:50 +00:00
*/
public function check_cart_item_validity () {
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
$_product = $values [ 'data' ];
if ( ! $_product || ! $_product -> exists () || $_product -> post -> post_status == 'trash' ) {
$this -> set_quantity ( $cart_item_key , 0 );
return new WP_Error ( 'invalid' , __ ( 'An item which is no longer available was removed from your cart.' , 'woocommerce' ) );
}
}
return true ;
}
2012-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Looks through the cart to check each item is in stock . If not , add an error .
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @ access public
2013-11-25 12:40:16 +00:00
* @ return bool | WP_Error
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function check_cart_item_stock () {
2013-07-30 10:51:50 +00:00
global $wpdb ;
2012-12-12 21:14:19 +00:00
2014-08-04 09:51:50 +00:00
$error = new WP_Error ();
2012-03-28 17:42:35 +00:00
$product_qty_in_cart = $this -> get_cart_item_quantities ();
2012-08-06 12:24:59 +00:00
2012-03-14 15:34:45 +00:00
// First stock check loop
2012-03-20 13:22:35 +00:00
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$_product = $values [ 'data' ];
2014-08-04 09:51:50 +00:00
$key = '_product_id' ;
$value = $values [ 'product_id' ];
$in_cart = $values [ 'quantity' ];
2012-08-06 12:24:59 +00:00
2012-03-14 15:34:45 +00:00
/**
2014-08-04 09:51:50 +00:00
* Check stock based on stock - status
2012-03-14 15:34:45 +00:00
*/
2014-08-04 09:51:50 +00:00
if ( ! $_product -> is_in_stock () ) {
$error -> add ( 'out-of-stock' , sprintf ( __ ( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologise for any inconvenience caused.' , 'woocommerce' ), $_product -> get_title () ) );
return $error ;
}
2012-08-06 12:24:59 +00:00
2014-08-04 09:51:50 +00:00
if ( ! $_product -> managing_stock () ) {
continue ;
}
2012-08-06 12:24:59 +00:00
2014-08-04 09:51:50 +00:00
$check_qty = $_product -> is_type ( 'variation' ) && true === $_product -> managing_stock () ? $product_qty_in_cart [ $values [ 'variation_id' ] ] : $product_qty_in_cart [ $values [ 'product_id' ] ];
2012-12-12 21:14:19 +00:00
2014-08-04 09:51:50 +00:00
/**
* Check stock based on all items in the cart
*/
if ( ! $_product -> has_enough_stock ( $check_qty ) ) {
$error -> add ( 'out-of-stock' , sprintf ( __ ( 'Sorry, we do not have enough "%s" in stock to fulfill your order (%s in stock). Please edit your cart and try again. We apologise for any inconvenience caused.' , 'woocommerce' ), $_product -> get_title (), $_product -> get_stock_quantity () ) );
return $error ;
}
2012-12-12 21:14:19 +00:00
2014-08-04 09:51:50 +00:00
/**
* Finally consider any held stock , from pending orders
*/
if ( get_option ( 'woocommerce_hold_stock_minutes' ) > 0 && ! $_product -> backorders_allowed () ) {
$order_id = isset ( WC () -> session -> order_awaiting_payment ) ? absint ( WC () -> session -> order_awaiting_payment ) : 0 ;
2014-08-04 19:37:42 +00:00
$held_stock = $wpdb -> get_var (
2014-08-04 09:51:50 +00:00
$wpdb -> prepare ( "
2012-12-12 21:14:19 +00:00
SELECT SUM ( order_item_meta . meta_value ) AS held_qty
FROM { $wpdb -> posts } AS posts
LEFT JOIN { $wpdb -> prefix } woocommerce_order_items as order_items ON posts . ID = order_items . order_id
LEFT JOIN { $wpdb -> prefix } woocommerce_order_itemmeta as order_item_meta ON order_items . order_item_id = order_item_meta . order_item_id
LEFT JOIN { $wpdb -> prefix } woocommerce_order_itemmeta as order_item_meta2 ON order_items . order_item_id = order_item_meta2 . order_item_id
WHERE order_item_meta . meta_key = '_qty'
AND order_item_meta2 . meta_key = % s AND order_item_meta2 . meta_value = % d
2014-08-04 09:51:50 +00:00
AND posts . post_type IN ( '" . implode( "' , '", wc_get_order_types() ) . "' )
2014-06-03 10:29:01 +00:00
AND posts . post_status = 'wc-pending'
2014-08-04 19:37:42 +00:00
AND posts . ID != % d ; " ,
$_product -> is_type ( 'variation' ) && true === $_product -> managing_stock () ? '_variation_id' : '_product_id' ,
2014-08-04 09:51:50 +00:00
$_product -> is_type ( 'variation' ) && true === $_product -> managing_stock () ? $values [ 'variation_id' ] : $values [ 'product_id' ],
2014-08-04 19:37:42 +00:00
$order_id
)
2014-08-04 09:51:50 +00:00
);
2012-12-12 21:14:19 +00:00
2014-08-04 19:37:42 +00:00
$not_enough_stock = false ;
2012-12-12 21:14:19 +00:00
2014-08-04 09:51:50 +00:00
if ( $_product -> is_type ( 'variation' ) && 'parent' === $_product -> managing_stock () && $_product -> parent -> get_stock_quantity () < ( $held_stock + $check_qty ) ) {
$not_enough_stock = true ;
} elseif ( $_product -> get_stock_quantity () < ( $held_stock + $check_qty ) ) {
$not_enough_stock = true ;
}
if ( $not_enough_stock ) {
$error -> add ( 'out-of-stock' , sprintf ( __ ( 'Sorry, we do not have enough "%s" in stock to fulfill your order right now. Please try again in %d minutes or edit your cart and try again. We apologise for any inconvenience caused.' , 'woocommerce' ), $_product -> get_title (), get_option ( 'woocommerce_hold_stock_minutes' ) ) );
2012-03-28 17:42:35 +00:00
return $error ;
}
2012-03-14 15:34:45 +00:00
}
}
2011-12-08 12:50:50 +00:00
return true ;
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets and formats a list of cart item data + variations for display on the frontend .
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @ access public
* @ param array $cart_item
* @ param bool $flat ( default : false )
* @ return string
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function get_item_data ( $cart_item , $flat = false ) {
2013-08-13 13:14:24 +00:00
$item_data = array ();
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Variation data
2012-11-21 18:39:51 +00:00
if ( ! empty ( $cart_item [ 'data' ] -> variation_id ) && is_array ( $cart_item [ 'variation' ] ) ) {
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$variation_list = array ();
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
foreach ( $cart_item [ 'variation' ] as $name => $value ) {
2012-08-06 12:24:59 +00:00
2014-01-28 12:13:19 +00:00
if ( '' === $value )
2013-08-13 13:14:24 +00:00
continue ;
2012-08-06 12:24:59 +00:00
2013-11-18 14:11:40 +00:00
$taxonomy = wc_attribute_taxonomy_name ( str_replace ( 'attribute_pa_' , '' , urldecode ( $name ) ) );
2011-12-08 12:50:50 +00:00
// If this is a term slug, get the term's nice name
2014-09-14 19:44:56 +00:00
if ( taxonomy_exists ( $taxonomy ) ) {
$term = get_term_by ( 'slug' , $value , $taxonomy );
if ( ! is_wp_error ( $term ) && $term && $term -> name ) {
$value = $term -> name ;
}
$label = wc_attribute_label ( $taxonomy );
// If this is a custom option slug, get the options name
} else {
2013-12-24 14:28:25 +00:00
$value = apply_filters ( 'woocommerce_variation_option_name' , $value );
$product_attributes = $cart_item [ 'data' ] -> get_attributes ();
2014-03-18 11:36:35 +00:00
if ( isset ( $product_attributes [ str_replace ( 'attribute_' , '' , $name ) ] ) ) {
$label = wc_attribute_label ( $product_attributes [ str_replace ( 'attribute_' , '' , $name ) ][ 'name' ] );
2014-02-28 15:42:12 +00:00
} else {
$label = $name ;
}
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
$item_data [] = array (
2013-11-18 14:11:40 +00:00
'key' => $label ,
2013-08-13 13:14:24 +00:00
'value' => $value
);
2012-03-20 13:22:35 +00:00
}
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Other data - returned as array with name/value values
2012-03-20 13:22:35 +00:00
$other_data = apply_filters ( 'woocommerce_get_item_data' , array (), $cart_item );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $other_data && is_array ( $other_data ) && sizeof ( $other_data ) > 0 ) {
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
foreach ( $other_data as $data ) {
2012-07-24 09:18:03 +00:00
// Set hidden to true to not display meta on cart.
2012-08-17 14:44:35 +00:00
if ( empty ( $data [ 'hidden' ] ) ) {
2013-08-13 13:14:24 +00:00
$display_value = ! empty ( $data [ 'display' ] ) ? $data [ 'display' ] : $data [ 'value' ];
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
$item_data [] = array (
'key' => $data [ 'name' ],
'value' => $display_value
);
2012-07-24 09:18:03 +00:00
}
2012-03-20 13:22:35 +00:00
}
2013-08-13 13:14:24 +00:00
}
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
// Output flat or in list format
if ( sizeof ( $item_data ) > 0 ) {
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
ob_start ();
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
if ( $flat ) {
2014-05-08 09:35:51 +00:00
foreach ( $item_data as $data ) {
2013-08-13 13:14:24 +00:00
echo esc_html ( $data [ 'key' ] ) . ': ' . wp_kses_post ( $data [ 'value' ] ) . " \n " ;
2014-05-08 09:35:51 +00:00
}
2013-08-13 13:14:24 +00:00
} else {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'cart/cart-item-data.php' , array ( 'item_data' => $item_data ) );
2013-08-13 13:14:24 +00:00
}
2012-08-06 12:24:59 +00:00
2013-08-13 13:14:24 +00:00
return ob_get_clean ();
}
2013-12-03 14:03:25 +00:00
return '' ;
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets cross sells based on the items in the cart .
2011-12-08 12:50:50 +00:00
*
2012-03-20 13:22:35 +00:00
* @ return array cross_sells ( item ids )
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function get_cross_sells () {
2011-12-08 12:50:50 +00:00
$cross_sells = array ();
$in_cart = array ();
2013-10-25 18:28:09 +00:00
if ( sizeof ( $this -> get_cart () ) > 0 ) {
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2012-03-20 13:22:35 +00:00
if ( $values [ 'quantity' ] > 0 ) {
$cross_sells = array_merge ( $values [ 'data' ] -> get_cross_sells (), $cross_sells );
$in_cart [] = $values [ 'product_id' ];
}
}
}
$cross_sells = array_diff ( $cross_sells , $in_cart );
2011-12-08 12:50:50 +00:00
return $cross_sells ;
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the url to the cart page .
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @ return string url to page
*/
2012-12-15 12:01:42 +00:00
public function get_cart_url () {
2014-01-04 22:46:43 +00:00
$cart_page_id = wc_get_page_id ( 'cart' );
2014-02-12 12:33:02 +00:00
return apply_filters ( 'woocommerce_get_cart_url' , $cart_page_id ? get_permalink ( $cart_page_id ) : '' );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the url to the checkout page .
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @ return string url to page
*/
2012-12-15 12:01:42 +00:00
public function get_checkout_url () {
2014-01-04 22:46:43 +00:00
$checkout_page_id = wc_get_page_id ( 'checkout' );
2013-04-29 15:30:17 +00:00
$checkout_url = '' ;
2012-03-20 13:22:35 +00:00
if ( $checkout_page_id ) {
2014-10-21 13:08:51 +00:00
// Get the checkout URL
$checkout_url = get_permalink ( $checkout_page_id );
// Force SLL if needed
if ( is_ssl () || 'yes' === get_option ( 'woocommerce_force_ssl_checkout' ) ) {
$checkout_url = str_replace ( 'http:' , 'https:' , $checkout_url );
2014-05-08 09:35:51 +00:00
}
2012-03-20 13:22:35 +00:00
}
2014-10-21 13:08:51 +00:00
2013-04-29 15:30:17 +00:00
return apply_filters ( 'woocommerce_get_checkout_url' , $checkout_url );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the url to remove an item from the cart .
2012-08-06 12:24:59 +00:00
*
2013-11-25 15:06:06 +00:00
* @ param string cart_item_key contains the id of the cart item
2012-03-20 13:22:35 +00:00
* @ return string url to page
*/
2012-12-15 12:01:42 +00:00
public function get_remove_url ( $cart_item_key ) {
2013-11-25 14:07:22 +00:00
$cart_page_id = wc_get_page_id ( 'cart' );
2014-02-12 12:33:02 +00:00
return apply_filters ( 'woocommerce_get_remove_url' , $cart_page_id ? wp_nonce_url ( add_query_arg ( 'remove_item' , $cart_item_key , get_permalink ( $cart_page_id ) ), 'woocommerce-cart' ) : '' );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Returns the contents of the cart in an array .
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @ return array contents of the cart
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function get_cart () {
2012-04-12 18:18:55 +00:00
return array_filter ( ( array ) $this -> cart_contents );
2012-01-27 15:00:03 +00:00
}
2012-08-06 12:24:59 +00:00
2013-11-22 23:16:53 +00:00
/**
* Returns the contents of the cart in an array without the 'data' element .
*
* @ return array contents of the cart
*/
private function get_cart_for_session () {
$cart_session = array ();
if ( $this -> get_cart () ) {
foreach ( $this -> get_cart () as $key => $values ) {
$cart_session [ $key ] = $values ;
unset ( $cart_session [ $key ][ 'data' ] ); // Unset product object
}
}
return $cart_session ;
}
2012-01-27 15:00:03 +00:00
/**
2012-08-15 17:08:42 +00:00
* Returns the cart and shipping taxes , merged .
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @ return array merged taxes
2012-01-27 15:00:03 +00:00
*/
2012-12-15 12:01:42 +00:00
public function get_taxes () {
2013-03-27 18:20:56 +00:00
$taxes = array ();
2012-01-27 15:00:03 +00:00
// Merge
2012-03-20 13:22:35 +00:00
foreach ( array_keys ( $this -> taxes + $this -> shipping_taxes ) as $key ) {
2013-03-27 18:20:56 +00:00
$taxes [ $key ] = ( isset ( $this -> shipping_taxes [ $key ] ) ? $this -> shipping_taxes [ $key ] : 0 ) + ( isset ( $this -> taxes [ $key ] ) ? $this -> taxes [ $key ] : 0 );
2012-01-27 15:00:03 +00:00
}
2012-08-06 12:24:59 +00:00
2013-03-27 18:20:56 +00:00
return apply_filters ( 'woocommerce_cart_get_taxes' , $taxes , $this );
2012-05-23 05:24:04 +00:00
}
2012-08-06 12:24:59 +00:00
2013-03-27 18:20:56 +00:00
/**
* Get taxes , merged by code , formatted ready for output .
*
* @ access public
2013-11-25 12:40:16 +00:00
* @ return array
2013-03-27 18:20:56 +00:00
*/
public function get_tax_totals () {
$taxes = $this -> get_taxes ();
$tax_totals = array ();
foreach ( $taxes as $key => $tax ) {
$code = $this -> tax -> get_rate_code ( $key );
2014-02-17 15:11:50 +00:00
if ( $code ) {
if ( ! isset ( $tax_totals [ $code ] ) ) {
$tax_totals [ $code ] = new stdClass ();
$tax_totals [ $code ] -> amount = 0 ;
}
2013-10-28 09:45:59 +00:00
2014-09-14 19:44:56 +00:00
$tax_totals [ $code ] -> tax_rate_id = $key ;
2014-02-17 15:11:50 +00:00
$tax_totals [ $code ] -> is_compound = $this -> tax -> is_compound ( $key );
$tax_totals [ $code ] -> label = $this -> tax -> get_rate_label ( $key );
$tax_totals [ $code ] -> amount += wc_round_tax_total ( $tax );
$tax_totals [ $code ] -> formatted_amount = wc_price ( wc_round_tax_total ( $tax_totals [ $code ] -> amount ) );
}
2013-03-27 18:20:56 +00:00
}
return apply_filters ( 'woocommerce_cart_tax_totals' , $tax_totals , $this );
}
2012-05-23 05:24:04 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Add to cart handling */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/**
2014-09-14 19:44:56 +00:00
* 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 .
*
* @ param mixed id of product to find in the cart
* @ return string cart item key
*/
public function find_product_in_cart ( $cart_id = false ) {
if ( $cart_id !== false ) {
if ( is_array ( $this -> cart_contents ) ) {
foreach ( $this -> cart_contents as $cart_item_key => $cart_item ) {
if ( $cart_item_key == $cart_id ) {
return $cart_item_key ;
}
}
}
}
2013-12-03 14:03:25 +00:00
return '' ;
2013-12-02 13:42:39 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2014-09-14 19:44:56 +00:00
* Generate a unique ID for the cart item being added .
*
* @ param int $product_id - id of the product the key is being generated for
* @ param int $variation_id of the product the key is being generated for
* @ param array $variation data for the cart item
* @ param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart
* @ return string cart item key
*/
public function generate_cart_id ( $product_id , $variation_id = 0 , $variation = array (), $cart_item_data = array () ) {
$id_parts = array ( $product_id );
2014-10-24 13:35:45 +00:00
if ( $variation_id && 0 != $variation_id ) {
2014-09-14 19:44:56 +00:00
$id_parts [] = $variation_id ;
2014-10-24 13:35:45 +00:00
}
2014-09-14 19:44:56 +00:00
if ( is_array ( $variation ) && ! empty ( $variation ) ) {
$variation_key = '' ;
foreach ( $variation as $key => $value ) {
$variation_key .= trim ( $key ) . trim ( $value );
}
$id_parts [] = $variation_key ;
}
if ( is_array ( $cart_item_data ) && ! empty ( $cart_item_data ) ) {
$cart_item_data_key = '' ;
foreach ( $cart_item_data as $key => $value ) {
2014-10-24 13:35:45 +00:00
if ( is_array ( $value ) ) {
$value = http_build_query ( $value );
}
$cart_item_data_key .= trim ( $key ) . trim ( $value );
2014-09-14 19:44:56 +00:00
}
$id_parts [] = $cart_item_data_key ;
}
return md5 ( implode ( '_' , $id_parts ) );
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Add a product to the cart .
2011-12-08 12:50:50 +00:00
*
2012-03-20 13:22:35 +00:00
* @ param string $product_id contains the id of the product to add to the cart
2014-09-07 23:37:55 +00:00
* @ param integer $quantity contains the quantity of the item to add
2012-03-20 13:22:35 +00:00
* @ param int $variation_id
* @ param array $variation attribute values
* @ param array $cart_item_data extra cart item data we want to pass into the item
2014-07-22 16:25:12 +00:00
* @ return string $cart_item_key
2011-12-08 12:50:50 +00:00
*/
2012-12-15 12:01:42 +00:00
public function add_to_cart ( $product_id , $quantity = 1 , $variation_id = '' , $variation = '' , $cart_item_data = array () ) {
2012-08-06 12:24:59 +00:00
2014-02-12 13:05:20 +00:00
if ( $quantity <= 0 ) {
return false ;
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Load cart item data - may be added by other plugins
2012-08-28 15:21:54 +00:00
$cart_item_data = ( array ) apply_filters ( 'woocommerce_add_cart_item_data' , $cart_item_data , $product_id , $variation_id );
2014-07-19 04:08:02 +00:00
2011-12-08 12:50:50 +00:00
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
2014-02-12 13:05:20 +00:00
$cart_id = $this -> generate_cart_id ( $product_id , $variation_id , $variation , $cart_item_data );
2014-07-19 04:08:02 +00:00
2011-12-08 12:50:50 +00:00
// See if this product and its options is already in the cart
2014-02-12 13:05:20 +00:00
$cart_item_key = $this -> find_product_in_cart ( $cart_id );
2012-08-06 12:24:59 +00:00
2014-02-12 13:05:20 +00:00
// Ensure we don't add a variation to the cart directly by variation ID
if ( 'product_variation' == get_post_type ( $product_id ) ) {
$variation_id = $product_id ;
$product_id = wp_get_post_parent_id ( $variation_id );
}
2014-07-19 04:08:02 +00:00
2014-02-12 13:05:20 +00:00
// Get the product
2014-08-19 10:09:29 +00:00
$product_data = wc_get_product ( $variation_id ? $variation_id : $product_id );
2012-08-06 12:24:59 +00:00
2013-01-22 14:30:15 +00:00
if ( ! $product_data )
return false ;
2012-05-11 11:47:23 +00:00
// Force quantity to 1 if sold individually
2014-09-24 20:38:52 +00:00
if ( $product_data -> is_sold_individually () ) {
2014-09-24 20:54:15 +00:00
$quantity = apply_filters ( 'woocommerce_add_to_cart_sold_individually_quantity' , 1 , $quantity , $product_id , $variation_id , $cart_item_data );
2014-09-24 20:38:52 +00:00
}
2012-08-06 12:24:59 +00:00
2012-08-06 23:33:52 +00:00
// Check product is_purchasable
if ( ! $product_data -> is_purchasable () ) {
2014-04-30 09:04:16 +00:00
wc_add_notice ( __ ( 'Sorry, this product cannot be purchased.' , 'woocommerce' ), 'error' );
2012-08-06 12:24:59 +00:00
return false ;
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Stock check - only check if we're managing stock and backorders are not allowed
2012-09-21 20:11:57 +00:00
if ( ! $product_data -> is_in_stock () ) {
2012-11-27 16:22:47 +00:00
2013-11-13 04:29:03 +00:00
wc_add_notice ( sprintf ( __ ( 'You cannot add "%s" to the cart because the product is out of stock.' , 'woocommerce' ), $product_data -> get_title () ), 'error' );
2012-11-27 16:22:47 +00:00
2012-08-06 12:24:59 +00:00
return false ;
2012-09-21 20:11:57 +00:00
} elseif ( ! $product_data -> has_enough_stock ( $quantity ) ) {
2012-11-27 16:22:47 +00:00
2013-11-13 04:29:03 +00:00
wc_add_notice ( sprintf ( __ ( 'You cannot add that amount of "%s" to the cart because there is not enough stock (%s remaining).' , 'woocommerce' ), $product_data -> get_title (), $product_data -> get_stock_quantity () ), 'error' );
2012-11-27 16:22:47 +00:00
2011-12-08 12:50:50 +00:00
return false ;
2012-11-27 16:22:47 +00:00
}
2012-06-10 12:53:26 +00:00
2012-02-27 18:22:54 +00:00
// Downloadable/virtual qty check
2012-05-11 11:47:23 +00:00
if ( $product_data -> is_sold_individually () ) {
2014-08-04 09:51:50 +00:00
$in_cart_quantity = $cart_item_key ? $this -> cart_contents [ $cart_item_key ][ 'quantity' ] : 0 ;
2012-08-06 12:24:59 +00:00
2014-01-04 22:46:43 +00:00
// If it's greater than 0, it's already in the cart
2012-11-19 14:05:03 +00:00
if ( $in_cart_quantity > 0 ) {
2014-01-04 22:46:43 +00:00
wc_add_notice ( sprintf (
'<a href="%s" class="button wc-forward">%s</a> %s' ,
$this -> get_cart_url (),
__ ( 'View Cart' , 'woocommerce' ),
sprintf ( __ ( 'You cannot add another "%s" to your cart.' , 'woocommerce' ), $product_data -> get_title () )
), 'error' );
2012-02-27 18:22:54 +00:00
return false ;
2012-03-20 13:22:35 +00:00
}
2012-03-28 17:42:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
// Stock check - this time accounting for whats already in-cart
2014-08-04 19:37:42 +00:00
if ( $managing_stock = $product_data -> managing_stock () ) {
$products_qty_in_cart = $this -> get_cart_item_quantities ();
2012-08-06 12:24:59 +00:00
2014-08-04 19:37:42 +00:00
if ( $product_data -> is_type ( 'variation' ) && true === $managing_stock ) {
$check_qty = isset ( $products_qty_in_cart [ $variation_id ] ) ? $products_qty_in_cart [ $variation_id ] : 0 ;
} else {
$check_qty = isset ( $products_qty_in_cart [ $product_id ] ) ? $products_qty_in_cart [ $product_id ] : 0 ;
}
2012-08-06 12:24:59 +00:00
2014-08-04 09:51:50 +00:00
/**
* Check stock based on all items in the cart
*/
if ( ! $product_data -> has_enough_stock ( $check_qty + $quantity ) ) {
wc_add_notice ( sprintf (
'<a href="%s" class="button wc-forward">%s</a> %s' ,
$this -> get_cart_url (),
__ ( 'View Cart' , 'woocommerce' ),
sprintf ( __ ( 'You cannot add that amount to the cart — we have %s in stock and you already have %s in your cart.' , 'woocommerce' ), $product_data -> get_stock_quantity (), $check_qty )
), 'error' );
return false ;
2012-03-28 17:42:35 +00:00
}
2012-08-06 12:24:59 +00:00
}
2012-03-28 17:42:35 +00:00
// If cart_item_key is set, the item is already in the cart
if ( $cart_item_key ) {
2012-08-06 12:24:59 +00:00
2012-04-10 11:18:41 +00:00
$new_quantity = $quantity + $this -> cart_contents [ $cart_item_key ][ 'quantity' ];
2012-08-06 12:24:59 +00:00
2013-08-19 14:19:44 +00:00
$this -> set_quantity ( $cart_item_key , $new_quantity , false );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2012-04-03 14:37:04 +00:00
$cart_item_key = $cart_id ;
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
2012-04-03 14:37:04 +00:00
$this -> cart_contents [ $cart_item_key ] = apply_filters ( 'woocommerce_add_cart_item' , array_merge ( $cart_item_data , array (
2011-12-08 12:50:50 +00:00
'product_id' => $product_id ,
'variation_id' => $variation_id ,
'variation' => $variation ,
'quantity' => $quantity ,
'data' => $product_data
2012-04-12 15:11:12 +00:00
) ), $cart_item_key );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2014-06-30 14:14:32 +00:00
if ( did_action ( 'wp' ) ) {
2014-09-14 19:44:56 +00:00
$this -> set_cart_cookies ( sizeof ( $this -> cart_contents ) > 0 );
}
2014-06-30 14:14:32 +00:00
2012-04-10 11:18:41 +00:00
do_action ( 'woocommerce_add_to_cart' , $cart_item_key , $product_id , $quantity , $variation_id , $variation , $cart_item_data );
2012-08-06 12:24:59 +00:00
2013-01-31 14:42:46 +00:00
$this -> calculate_totals ();
2012-08-06 12:24:59 +00:00
2014-07-09 20:33:40 +00:00
return $cart_item_key ;
2011-12-08 12:50:50 +00:00
}
2011-08-09 15:16:18 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Set the quantity for an item in the cart .
2011-12-08 12:50:50 +00:00
*
2013-08-19 14:19:44 +00:00
* @ param string cart_item_key contains the id of the cart item
* @ param string quantity contains the quantity of the item
* @ param boolean $refresh_totals whether or not to calculate totals after setting the new qty
2014-10-24 14:02:31 +00:00
*
* @ return bool
2011-12-08 12:50:50 +00:00
*/
2013-08-19 14:19:44 +00:00
public function set_quantity ( $cart_item_key , $quantity = 1 , $refresh_totals = true ) {
2012-03-20 13:22:35 +00:00
if ( $quantity == 0 || $quantity < 0 ) {
2012-04-23 12:28:47 +00:00
do_action ( 'woocommerce_before_cart_item_quantity_zero' , $cart_item_key );
2013-09-23 13:01:17 +00:00
unset ( $this -> cart_contents [ $cart_item_key ] );
2012-03-20 13:22:35 +00:00
} else {
2013-09-23 13:01:17 +00:00
$this -> cart_contents [ $cart_item_key ][ 'quantity' ] = $quantity ;
2012-04-12 15:11:12 +00:00
do_action ( 'woocommerce_after_cart_item_quantity_update' , $cart_item_key , $quantity );
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2014-05-08 09:35:51 +00:00
if ( $refresh_totals ) {
2013-08-19 14:19:44 +00:00
$this -> calculate_totals ();
2014-05-08 09:35:51 +00:00
}
2014-10-24 14:02:31 +00:00
return true ;
2011-12-08 12:50:50 +00:00
}
2011-11-23 23:19:23 +00:00
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Cart Calculation Functions */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Reset cart totals and clear sessions .
2012-08-14 19:42:38 +00:00
*
* @ access private
* @ return void
2011-12-08 12:50:50 +00:00
*/
2012-01-04 23:01:47 +00:00
private function reset () {
2013-10-25 18:28:09 +00:00
foreach ( $this -> cart_session_data as $key => $default ) {
$this -> $key = $default ;
unset ( WC () -> session -> $key );
}
2013-02-15 10:27:09 +00:00
}
2012-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Calculate totals for the items in the cart .
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function calculate_totals () {
2012-08-06 12:24:59 +00:00
2012-01-04 23:01:47 +00:00
$this -> reset ();
2012-08-06 12:24:59 +00:00
2012-10-01 09:45:07 +00:00
do_action ( 'woocommerce_before_calculate_totals' , $this );
2012-08-06 12:24:59 +00:00
2013-09-23 13:01:17 +00:00
if ( sizeof ( $this -> get_cart () ) == 0 ) {
$this -> set_session ();
2013-06-13 16:01:36 +00:00
return ;
2013-09-23 13:01:17 +00:00
}
2013-06-13 16:01:36 +00:00
$tax_rates = array ();
2013-10-24 12:15:42 +00:00
$shop_tax_rates = array ();
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* Calculate subtotals for items . This is done first so that discount logic can use the values .
*/
2013-06-13 16:01:36 +00:00
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
$_product = $values [ 'data' ];
// Count items + weight
2013-10-24 12:15:42 +00:00
$this -> cart_contents_weight += $_product -> get_weight () * $values [ 'quantity' ];
$this -> cart_contents_count += $values [ 'quantity' ];
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Prices
$base_price = $_product -> get_price ();
$line_price = $_product -> get_price () * $values [ 'quantity' ];
2014-07-19 04:08:02 +00:00
2013-10-26 14:22:09 +00:00
$line_subtotal = 0 ;
$line_subtotal_tax = 0 ;
2013-06-13 16:01:36 +00:00
2013-10-24 12:15:42 +00:00
/**
* No tax to calculate
*/
2013-06-13 16:01:36 +00:00
if ( ! $_product -> is_taxable () ) {
2013-10-24 12:15:42 +00:00
// Subtotal is the undiscounted price
$this -> subtotal += $line_price ;
$this -> subtotal_ex_tax += $line_price ;
/**
* Prices include tax
*
* To prevent rounding issues we need to work with the inclusive price where possible
* otherwise we ' ll see errors such as when working with a 9.99 inc price , 20 % VAT which would
* be 8.325 leading to totals being 1 p off
*
* Pre tax coupons come off the price the customer thinks they are paying - tax is calculated
* afterwards .
*
* e . g . $ 100 bike with $ 10 coupon = customer pays $ 90 and tax worked backwards from that
*/
} elseif ( $this -> prices_include_tax ) {
2013-10-28 09:45:59 +00:00
2013-06-13 16:01:36 +00:00
// Get base tax rates
if ( empty ( $shop_tax_rates [ $_product -> tax_class ] ) )
$shop_tax_rates [ $_product -> tax_class ] = $this -> tax -> get_shop_base_rate ( $_product -> tax_class );
// Get item tax rates
if ( empty ( $tax_rates [ $_product -> get_tax_class () ] ) )
$tax_rates [ $_product -> get_tax_class () ] = $this -> tax -> get_rates ( $_product -> get_tax_class () );
2013-10-24 12:15:42 +00:00
$base_tax_rates = $shop_tax_rates [ $_product -> tax_class ];
2013-06-13 16:01:36 +00:00
$item_tax_rates = $tax_rates [ $_product -> get_tax_class () ];
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* ADJUST TAX - Calculations when base tax is not equal to the item tax
*/
if ( $item_tax_rates !== $base_tax_rates ) {
// Work out a new base price without the shop's base tax
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_price , $base_tax_rates , true , true );
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Now we have a new item price (excluding TAX)
2013-10-24 15:16:39 +00:00
$line_subtotal = $line_price - array_sum ( $taxes );
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Now add modifed taxes
2013-10-24 15:16:39 +00:00
$tax_result = $this -> tax -> calc_tax ( $line_subtotal , $item_tax_rates );
2014-02-05 11:45:21 +00:00
$line_subtotal_tax = array_sum ( $tax_result );
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* Regular tax calculation ( customer inside base and the tax class is unmodified
*/
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Calc tax normally
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_price , $item_tax_rates , true );
$line_subtotal_tax = array_sum ( $taxes );
$line_subtotal = $line_price - array_sum ( $taxes );
2012-03-20 13:22:35 +00:00
}
2013-06-13 16:01:36 +00:00
2012-08-06 12:24:59 +00:00
/**
2013-10-24 12:15:42 +00:00
* Prices exclude tax
*
* This calculation is simpler - work with the base , untaxed price .
2011-11-21 11:33:46 +00:00
*/
2013-10-24 12:15:42 +00:00
} else {
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Get item tax rates
if ( empty ( $tax_rates [ $_product -> get_tax_class () ] ) )
$tax_rates [ $_product -> get_tax_class () ] = $this -> tax -> get_rates ( $_product -> get_tax_class () );
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
$item_tax_rates = $tax_rates [ $_product -> get_tax_class () ];
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Base tax for line before discount - we will store this in the order data
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_price , $item_tax_rates );
$line_subtotal_tax = array_sum ( $taxes );
2013-10-24 12:15:42 +00:00
$line_subtotal = $line_price ;
}
2013-10-24 13:53:01 +00:00
// Add to main subtotal
$this -> subtotal += $line_subtotal + $line_subtotal_tax ;
$this -> subtotal_ex_tax += $line_subtotal ;
2013-10-24 12:15:42 +00:00
}
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* Calculate totals for items
*/
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
$_product = $values [ 'data' ];
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Prices
$base_price = $_product -> get_price ();
$line_price = $_product -> get_price () * $values [ 'quantity' ];
2012-08-06 12:24:59 +00:00
2014-07-19 04:08:02 +00:00
// Tax data
$taxes = array ();
$discounted_taxes = array ();
2013-10-24 12:15:42 +00:00
/**
* No tax to calculate
*/
if ( ! $_product -> is_taxable () ) {
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Discounted Price (price with any pre-tax discounts applied)
$discounted_price = $this -> get_discounted_price ( $values , $base_price , true );
$discounted_tax_amount = 0 ;
$tax_amount = 0 ;
$line_subtotal_tax = 0 ;
$line_subtotal = $line_price ;
$line_tax = 0 ;
$line_total = $this -> tax -> round ( $discounted_price * $values [ 'quantity' ] );
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* Prices include tax
*/
} elseif ( $this -> prices_include_tax ) {
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
$base_tax_rates = $shop_tax_rates [ $_product -> tax_class ];
$item_tax_rates = $tax_rates [ $_product -> get_tax_class () ];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
/**
2013-10-24 12:15:42 +00:00
* ADJUST TAX - Calculations when base tax is not equal to the item tax
2013-06-13 16:01:36 +00:00
*/
2013-10-24 12:15:42 +00:00
if ( $item_tax_rates !== $base_tax_rates ) {
// Work out a new base price without the shop's base tax
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_price , $base_tax_rates , true , true );
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Now we have a new item price (excluding TAX)
2014-03-31 10:12:08 +00:00
$line_subtotal = round ( $line_price - array_sum ( $taxes ), WC_ROUNDING_PRECISION );
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Now add modifed taxes
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_subtotal , $item_tax_rates );
$line_subtotal_tax = array_sum ( $taxes );
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Adjusted price (this is the price including the new tax rate)
$adjusted_price = ( $line_subtotal + $line_subtotal_tax ) / $values [ 'quantity' ];
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Apply discounts
$discounted_price = $this -> get_discounted_price ( $values , $adjusted_price , true );
2013-10-24 15:16:39 +00:00
$discounted_taxes = $this -> tax -> calc_tax ( $discounted_price * $values [ 'quantity' ], $item_tax_rates , true );
$line_tax = array_sum ( $discounted_taxes );
$line_total = ( $discounted_price * $values [ 'quantity' ] ) - $line_tax ;
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* Regular tax calculation ( customer inside base and the tax class is unmodified
*/
2013-06-13 16:01:36 +00:00
} else {
2012-08-06 12:24:59 +00:00
2013-10-24 13:53:01 +00:00
// Work out a new base price without the shop's base tax
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_price , $item_tax_rates , true );
2013-10-28 09:45:59 +00:00
2013-10-24 13:53:01 +00:00
// Now we have a new item price (excluding TAX)
2013-10-24 15:16:39 +00:00
$line_subtotal = $line_price - array_sum ( $taxes );
$line_subtotal_tax = array_sum ( $taxes );
2013-10-24 13:53:01 +00:00
2013-10-24 12:15:42 +00:00
// Calc prices and tax (discounted)
$discounted_price = $this -> get_discounted_price ( $values , $base_price , true );
2013-10-24 15:16:39 +00:00
$discounted_taxes = $this -> tax -> calc_tax ( $discounted_price * $values [ 'quantity' ], $item_tax_rates , true );
$line_tax = array_sum ( $discounted_taxes );
$line_total = ( $discounted_price * $values [ 'quantity' ] ) - $line_tax ;
2013-10-24 12:15:42 +00:00
}
2013-10-24 13:55:23 +00:00
2013-10-24 12:15:42 +00:00
// Tax rows - merge the totals we just got
foreach ( array_keys ( $this -> taxes + $discounted_taxes ) as $key ) {
2014-09-14 19:44:56 +00:00
$this -> taxes [ $key ] = ( isset ( $discounted_taxes [ $key ] ) ? $discounted_taxes [ $key ] : 0 ) + ( isset ( $this -> taxes [ $key ] ) ? $this -> taxes [ $key ] : 0 );
2013-06-13 16:01:36 +00:00
}
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
/**
* Prices exclude tax
*/
} else {
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
$item_tax_rates = $tax_rates [ $_product -> get_tax_class () ];
2013-10-28 09:45:59 +00:00
2013-10-24 13:53:01 +00:00
// Work out a new base price without the shop's base tax
2013-10-24 15:16:39 +00:00
$taxes = $this -> tax -> calc_tax ( $line_price , $item_tax_rates );
2013-10-28 09:45:59 +00:00
2013-11-13 11:37:15 +00:00
// Now we have the item price (excluding TAX)
$line_subtotal = $line_price ;
2013-10-24 15:16:39 +00:00
$line_subtotal_tax = array_sum ( $taxes );
2013-10-28 09:45:59 +00:00
2013-10-24 12:15:42 +00:00
// Now calc product rates
2014-02-03 15:29:40 +00:00
$discounted_price = $this -> get_discounted_price ( $values , $base_price , true );
2013-10-24 15:16:39 +00:00
$discounted_taxes = $this -> tax -> calc_tax ( $discounted_price * $values [ 'quantity' ], $item_tax_rates );
$discounted_tax_amount = array_sum ( $discounted_taxes );
$line_tax = $discounted_tax_amount ;
2013-11-13 11:37:15 +00:00
$line_total = $discounted_price * $values [ 'quantity' ];
2013-10-24 12:15:42 +00:00
// Tax rows - merge the totals we just got
foreach ( array_keys ( $this -> taxes + $discounted_taxes ) as $key ) {
2014-09-14 19:44:56 +00:00
$this -> taxes [ $key ] = ( isset ( $discounted_taxes [ $key ] ) ? $discounted_taxes [ $key ] : 0 ) + ( isset ( $this -> taxes [ $key ] ) ? $this -> taxes [ $key ] : 0 );
2013-10-24 12:15:42 +00:00
}
}
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Add any product discounts (after tax)
$this -> apply_product_discounts_after_tax ( $values , $line_total + $line_tax );
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Cart contents total is based on discounted prices and is used for the final total calculation
2013-10-24 13:53:01 +00:00
$this -> cart_contents_total += $line_total ;
2012-08-06 12:24:59 +00:00
2013-10-24 12:15:42 +00:00
// Store costs + taxes for lines
2014-07-19 04:08:02 +00:00
$this -> cart_contents [ $cart_item_key ][ 'line_total' ] = $line_total ;
$this -> cart_contents [ $cart_item_key ][ 'line_tax' ] = $line_tax ;
$this -> cart_contents [ $cart_item_key ][ 'line_subtotal' ] = $line_subtotal ;
2013-10-24 12:15:42 +00:00
$this -> cart_contents [ $cart_item_key ][ 'line_subtotal_tax' ] = $line_subtotal_tax ;
2014-07-19 04:08:02 +00:00
// Store rates ID and costs - Since 2.2
2014-07-23 10:37:02 +00:00
$this -> cart_contents [ $cart_item_key ][ 'line_tax_data' ] = array ( 'total' => $discounted_taxes , 'subtotal' => $taxes );
2012-03-20 13:22:35 +00:00
}
2012-11-27 16:22:47 +00:00
2013-01-31 14:42:46 +00:00
// Only calculate the grand total + shipping if on the cart/checkout
if ( is_checkout () || is_cart () || defined ( 'WOOCOMMERCE_CHECKOUT' ) || defined ( 'WOOCOMMERCE_CART' ) ) {
2012-08-06 12:24:59 +00:00
2013-08-13 16:19:20 +00:00
// Calculate the Shipping
$this -> calculate_shipping ();
// Trigger the fees API where developers can add fees to the cart
$this -> calculate_fees ();
2013-10-24 12:15:42 +00:00
// Total up/round taxes and shipping taxes
2013-06-13 16:01:36 +00:00
if ( $this -> round_at_subtotal ) {
2013-04-12 09:59:38 +00:00
$this -> tax_total = $this -> tax -> get_tax_total ( $this -> taxes );
$this -> shipping_tax_total = $this -> tax -> get_tax_total ( $this -> shipping_taxes );
2013-10-24 12:15:42 +00:00
$this -> taxes = array_map ( array ( $this -> tax , 'round' ), $this -> taxes );
2013-04-12 09:59:38 +00:00
$this -> shipping_taxes = array_map ( array ( $this -> tax , 'round' ), $this -> shipping_taxes );
} else {
2013-10-24 12:15:42 +00:00
$this -> tax_total = array_sum ( $this -> taxes );
2013-04-12 09:59:38 +00:00
$this -> shipping_tax_total = array_sum ( $this -> shipping_taxes );
2013-01-31 14:42:46 +00:00
}
2013-04-12 09:59:38 +00:00
// VAT exemption done at this point - so all totals are correct before exemption
2014-03-25 11:36:35 +00:00
if ( WC () -> customer -> is_vat_exempt () ) {
2013-04-12 09:59:38 +00:00
$this -> remove_taxes ();
2014-03-25 11:36:35 +00:00
}
2013-04-12 09:59:38 +00:00
// Cart Discounts (after tax)
$this -> apply_cart_discounts_after_tax ();
2013-01-31 14:42:46 +00:00
// Allow plugins to hook and alter totals before final total is calculated
do_action ( 'woocommerce_calculate_totals' , $this );
2012-08-06 12:24:59 +00:00
2013-04-12 09:59:38 +00:00
// Grand Total - Discounted product prices, discounted tax, shipping cost + tax, and any discounts to be added after tax (e.g. store credit)
2013-11-12 17:43:30 +00:00
$this -> total = max ( 0 , apply_filters ( 'woocommerce_calculated_total' , round ( $this -> cart_contents_total + $this -> tax_total + $this -> shipping_tax_total + $this -> shipping_total - $this -> discount_total + $this -> fee_total , $this -> dp ), $this ) );
2012-08-06 12:24:59 +00:00
2013-04-12 09:59:38 +00:00
} else {
// Set tax total to sum of all tax rows
$this -> tax_total = $this -> tax -> get_tax_total ( $this -> taxes );
// VAT exemption done at this point - so all totals are correct before exemption
2014-03-25 11:36:35 +00:00
if ( WC () -> customer -> is_vat_exempt () ) {
2013-04-12 09:59:38 +00:00
$this -> remove_taxes ();
2014-03-25 11:36:35 +00:00
}
2012-05-23 05:24:04 +00:00
2013-04-12 09:59:38 +00:00
// Cart Discounts (after tax)
$this -> apply_cart_discounts_after_tax ();
2013-01-31 14:42:46 +00:00
}
2012-11-27 16:22:47 +00:00
2013-01-31 14:42:46 +00:00
$this -> set_session ();
2011-12-08 12:50:50 +00:00
}
2013-04-12 09:59:38 +00:00
/**
* remove_taxes function .
*
* @ return void
*/
public function remove_taxes () {
$this -> shipping_tax_total = $this -> tax_total = 0 ;
2014-03-25 11:36:35 +00:00
$this -> subtotal = $this -> subtotal_ex_tax ;
2013-04-12 09:59:38 +00:00
2014-03-25 11:36:35 +00:00
foreach ( $this -> cart_contents as $cart_item_key => $item ) {
2013-04-12 09:59:38 +00:00
$this -> cart_contents [ $cart_item_key ][ 'line_subtotal_tax' ] = $this -> cart_contents [ $cart_item_key ][ 'line_tax' ] = 0 ;
2014-03-25 11:36:35 +00:00
}
2013-09-10 10:04:26 +00:00
// If true, zero rate is applied so '0' tax is displayed on the frontend rather than nothing.
if ( apply_filters ( 'woocommerce_cart_remove_taxes_apply_zero_rate' , true ) ) {
$this -> taxes = $this -> shipping_taxes = array ( apply_filters ( 'woocommerce_cart_remove_taxes_zero_rate_id' , 'zero-rated' ) => 0 );
} else {
$this -> taxes = $this -> shipping_taxes = array ();
}
2013-04-12 09:59:38 +00:00
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* looks at the totals to see if payment is actually required .
2012-03-20 13:22:35 +00:00
*
* @ return bool
2011-11-19 20:59:16 +00:00
*/
2012-12-14 21:26:07 +00:00
public function needs_payment () {
2013-10-25 18:28:09 +00:00
return apply_filters ( 'woocommerce_cart_needs_payment' , $this -> total > 0 , $this );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Shipping related functions */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
2012-04-13 11:16:24 +00:00
/**
* Uses the shipping class to calculate shipping then gets the totals when its finished .
*
* @ access public
* @ return void
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function calculate_shipping () {
2012-04-11 12:08:04 +00:00
if ( $this -> needs_shipping () && $this -> show_shipping () ) {
2013-10-18 17:10:55 +00:00
WC () -> shipping -> calculate_shipping ( $this -> get_shipping_packages () );
2012-03-20 13:22:35 +00:00
} else {
2013-10-18 17:10:55 +00:00
WC () -> shipping -> reset_shipping ();
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-04-13 11:16:24 +00:00
// Get totals for the chosen shipping method
2013-10-18 17:10:55 +00:00
$this -> shipping_total = WC () -> shipping -> shipping_total ; // Shipping Total
$this -> shipping_taxes = WC () -> shipping -> shipping_taxes ; // Shipping Taxes
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-04-13 11:16:24 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get packages to calculate shipping for .
2012-04-13 11:16:24 +00:00
*
2012-08-15 17:08:42 +00:00
* This lets us calculate costs for carts that are shipped to multiple locations .
2012-04-13 11:16:24 +00:00
*
2013-03-03 17:07:31 +00:00
* Shipping methods are responsible for looping through these packages .
2012-04-13 11:16:24 +00:00
*
* By default we pass the cart itself as a package - plugins can change this
* through the filter and break it up .
2012-08-06 12:24:59 +00:00
*
2012-04-13 11:16:24 +00:00
* @ since 1.5 . 4
* @ access public
* @ return array of cart items
*/
2012-12-14 21:26:07 +00:00
public function get_shipping_packages () {
2012-04-13 11:16:24 +00:00
// Packages array for storing 'carts'
$packages = array ();
2012-08-06 12:24:59 +00:00
2013-01-18 12:10:19 +00:00
$packages [ 0 ][ 'contents' ] = $this -> get_cart (); // Items in the package
$packages [ 0 ][ 'contents_cost' ] = 0 ; // Cost of items in the package, set below
2013-10-28 09:45:59 +00:00
$packages [ 0 ][ 'applied_coupons' ] = $this -> applied_coupons ;
2014-02-02 22:30:17 +00:00
$packages [ 0 ][ 'user' ][ 'ID' ] = get_current_user_id ();
2013-10-18 17:10:55 +00:00
$packages [ 0 ][ 'destination' ][ 'country' ] = WC () -> customer -> get_shipping_country ();
$packages [ 0 ][ 'destination' ][ 'state' ] = WC () -> customer -> get_shipping_state ();
$packages [ 0 ][ 'destination' ][ 'postcode' ] = WC () -> customer -> get_shipping_postcode ();
$packages [ 0 ][ 'destination' ][ 'city' ] = WC () -> customer -> get_shipping_city ();
$packages [ 0 ][ 'destination' ][ 'address' ] = WC () -> customer -> get_shipping_address ();
$packages [ 0 ][ 'destination' ][ 'address_2' ] = WC () -> customer -> get_shipping_address_2 ();
2012-08-06 12:24:59 +00:00
foreach ( $this -> get_cart () as $item )
if ( $item [ 'data' ] -> needs_shipping () )
2013-08-14 20:00:34 +00:00
if ( isset ( $item [ 'line_total' ] ) )
$packages [ 0 ][ 'contents_cost' ] += $item [ 'line_total' ];
2012-08-06 12:24:59 +00:00
2012-04-13 11:16:24 +00:00
return apply_filters ( 'woocommerce_cart_shipping_packages' , $packages );
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Looks through the cart to see if shipping is actually required .
2012-03-20 13:22:35 +00:00
*
* @ return bool whether or not the cart needs shipping
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function needs_shipping () {
2013-08-02 15:54:28 +00:00
if ( get_option ( 'woocommerce_calc_shipping' ) == 'no' )
return false ;
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$needs_shipping = false ;
2012-08-06 12:24:59 +00:00
2013-08-02 15:54:28 +00:00
if ( $this -> cart_contents ) {
foreach ( $this -> cart_contents as $cart_item_key => $values ) {
$_product = $values [ 'data' ];
if ( $_product -> needs_shipping () ) {
$needs_shipping = true ;
}
2012-03-20 13:22:35 +00:00
}
}
2012-08-06 12:24:59 +00:00
2013-01-02 16:34:23 +00:00
return apply_filters ( 'woocommerce_cart_needs_shipping' , $needs_shipping );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2014-03-28 10:41:17 +00:00
/**
2014-03-28 14:40:56 +00:00
* Should the shipping address form be shown
2014-07-19 04:08:02 +00:00
*
2014-03-28 10:41:17 +00:00
* @ return bool
*/
function needs_shipping_address () {
2014-03-28 14:40:56 +00:00
$needs_shipping_address = false ;
2014-06-11 14:10:03 +00:00
if ( $this -> needs_shipping () === true && ! $this -> ship_to_billing_address_only () ) {
2014-03-28 14:40:56 +00:00
$needs_shipping_address = true ;
}
return apply_filters ( 'woocommerce_cart_needs_shipping_address' , $needs_shipping_address );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Sees if the customer has entered enough data to calc the shipping yet .
2012-04-11 12:08:04 +00:00
*
* @ return bool
*/
2012-12-14 21:26:07 +00:00
public function show_shipping () {
2013-08-14 20:00:34 +00:00
if ( get_option ( 'woocommerce_calc_shipping' ) == 'no' || ! is_array ( $this -> cart_contents ) )
return false ;
2012-08-06 12:24:59 +00:00
2012-04-11 12:08:04 +00:00
if ( get_option ( 'woocommerce_shipping_cost_requires_address' ) == 'yes' ) {
2013-10-18 17:10:55 +00:00
if ( ! WC () -> customer -> has_calculated_shipping () ) {
if ( ! WC () -> customer -> get_shipping_country () || ( ! WC () -> customer -> get_shipping_state () && ! WC () -> customer -> get_shipping_postcode () ) )
2013-08-14 20:00:34 +00:00
return false ;
2012-04-11 12:08:04 +00:00
}
}
2012-08-06 12:24:59 +00:00
2012-04-11 12:08:04 +00:00
$show_shipping = true ;
2012-08-06 12:24:59 +00:00
2013-01-02 16:34:23 +00:00
return apply_filters ( 'woocommerce_cart_ready_to_calc_shipping' , $show_shipping );
2012-04-11 12:08:04 +00:00
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Sees if we need a shipping address .
2012-03-20 13:22:35 +00:00
*
* @ return bool
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function ship_to_billing_address_only () {
2014-04-08 15:28:27 +00:00
return wc_ship_to_billing_address_only ();
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the shipping total ( after calculation ) .
2012-03-20 13:22:35 +00:00
*
2013-12-02 13:42:39 +00:00
* @ return string price or string for the shipping total
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_cart_shipping_total () {
2013-08-14 20:00:34 +00:00
if ( isset ( $this -> shipping_total ) ) {
2012-03-20 13:22:35 +00:00
if ( $this -> shipping_total > 0 ) {
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
// Display varies depending on settings
if ( $this -> tax_display_cart == 'excl' ) {
2012-08-06 12:24:59 +00:00
2013-11-25 13:34:21 +00:00
$return = wc_price ( $this -> shipping_total );
2012-12-03 16:36:54 +00:00
2012-03-20 13:22:35 +00:00
if ( $this -> shipping_tax_total > 0 && $this -> prices_include_tax ) {
2013-10-18 17:10:55 +00:00
$return .= ' <small>' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
2012-12-03 16:36:54 +00:00
2011-12-08 12:50:50 +00:00
return $return ;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2013-11-25 13:34:21 +00:00
$return = wc_price ( $this -> shipping_total + $this -> shipping_tax_total );
2012-12-03 16:36:54 +00:00
2012-03-20 13:22:35 +00:00
if ( $this -> shipping_tax_total > 0 && ! $this -> prices_include_tax ) {
2013-10-18 17:10:55 +00:00
$return .= ' <small>' . WC () -> countries -> inc_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
2012-12-03 16:36:54 +00:00
2011-12-08 12:50:50 +00:00
return $return ;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} else {
2012-10-16 09:45:33 +00:00
return __ ( 'Free!' , 'woocommerce' );
2012-03-20 13:22:35 +00:00
}
}
2013-12-02 13:42:39 +00:00
2013-12-03 14:03:25 +00:00
return '' ;
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Coupons/Discount related functions */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
2013-10-18 17:10:55 +00:00
/**
* 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 )
*
* @ access public
* @ param array $posted
*/
public function check_customer_coupons ( $posted ) {
2013-10-25 18:28:09 +00:00
if ( ! empty ( $this -> applied_coupons ) ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
if ( $coupon -> is_valid () ) {
// Limit to defined email addresses
if ( is_array ( $coupon -> customer_email ) && sizeof ( $coupon -> customer_email ) > 0 ) {
2014-05-22 15:23:01 +00:00
$check_emails = array ();
2013-10-18 17:10:55 +00:00
$coupon -> customer_email = array_map ( 'sanitize_email' , $coupon -> customer_email );
if ( is_user_logged_in () ) {
$current_user = wp_get_current_user ();
$check_emails [] = $current_user -> user_email ;
}
$check_emails [] = $posted [ 'billing_email' ];
$check_emails = array_map ( 'sanitize_email' , array_map ( 'strtolower' , $check_emails ) );
if ( 0 == sizeof ( array_intersect ( $check_emails , $coupon -> customer_email ) ) ) {
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_NOT_YOURS_REMOVED );
// Remove the coupon
$this -> remove_coupon ( $code );
// Flag totals for refresh
WC () -> session -> set ( 'refresh_totals' , true );
}
}
// Usage limits per user - check against billing and user email and user ID
if ( $coupon -> usage_limit_per_user > 0 ) {
2014-05-22 15:23:01 +00:00
$check_emails = array ();
$used_by = array_filter ( ( array ) get_post_meta ( $coupon -> id , '_used_by' ) );
2013-10-18 17:10:55 +00:00
if ( is_user_logged_in () ) {
$current_user = wp_get_current_user ();
2014-05-22 15:23:01 +00:00
$check_emails [] = sanitize_email ( $current_user -> user_email );
$usage_count = sizeof ( array_keys ( $used_by , get_current_user_id () ) );
} else {
$check_emails [] = sanitize_email ( $posted [ 'billing_email' ] );
$user = get_user_by ( 'email' , $posted [ 'billing_email' ] );
if ( $user ) {
$usage_count = sizeof ( array_keys ( $used_by , $user -> ID ) );
} else {
$usage_count = 0 ;
}
2013-10-18 17:10:55 +00:00
}
2014-07-19 04:08:02 +00:00
2014-04-24 14:26:34 +00:00
foreach ( $check_emails as $check_email ) {
2014-05-22 15:23:01 +00:00
$usage_count = $usage_count + sizeof ( array_keys ( $used_by , $check_email ) );
2014-04-24 14:26:34 +00:00
}
2013-10-18 17:10:55 +00:00
if ( $usage_count >= $coupon -> usage_limit_per_user ) {
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_USAGE_LIMIT_REACHED );
// Remove the coupon
$this -> remove_coupon ( $code );
// Flag totals for refresh
WC () -> session -> set ( 'refresh_totals' , true );
}
}
}
}
}
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Returns whether or not a discount has been applied .
2012-03-20 13:22:35 +00:00
*
* @ return bool
2011-12-08 12:50:50 +00:00
*/
2013-04-12 08:59:33 +00:00
public function has_discount ( $coupon_code ) {
2013-10-25 18:28:09 +00:00
return in_array ( apply_filters ( 'woocommerce_coupon_code' , $coupon_code ), $this -> applied_coupons );
2011-12-08 12:50:50 +00:00
}
/**
2012-08-15 17:08:42 +00:00
* Applies a coupon code passed to the method .
2011-12-08 12:50:50 +00:00
*
2012-03-20 13:22:35 +00:00
* @ param string $coupon_code - The code to apply
* @ return bool True if the coupon is applied , false if it does not exist or cannot be applied
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function add_discount ( $coupon_code ) {
2012-03-12 09:25:06 +00:00
// Coupons are globally disabled
2013-08-14 20:00:34 +00:00
if ( ! $this -> coupons_enabled () )
2013-02-10 23:02:52 +00:00
return false ;
2012-03-12 09:25:06 +00:00
2013-04-12 08:59:33 +00:00
// Sanitize coupon code
$coupon_code = apply_filters ( 'woocommerce_coupon_code' , $coupon_code );
// Get the coupon
2012-11-08 16:57:53 +00:00
$the_coupon = new WC_Coupon ( $coupon_code );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $the_coupon -> id ) {
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Check it can be used with cart
2012-12-19 14:57:46 +00:00
if ( ! $the_coupon -> is_valid () ) {
2013-11-13 04:29:03 +00:00
wc_add_notice ( $the_coupon -> get_error_message (), 'error' );
2011-12-08 12:50:50 +00:00
return false ;
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-06 17:27:02 +00:00
// Check if applied
2013-08-14 20:00:34 +00:00
if ( $this -> has_discount ( $coupon_code ) ) {
2013-02-18 12:29:10 +00:00
$the_coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_ALREADY_APPLIED );
2012-03-06 17:27:02 +00:00
return false ;
2012-08-06 12:24:59 +00:00
}
2011-12-08 12:50:50 +00:00
// If its individual use then remove other coupons
2012-03-20 13:22:35 +00:00
if ( $the_coupon -> individual_use == 'yes' ) {
2013-10-25 18:28:09 +00:00
$this -> applied_coupons = apply_filters ( 'woocommerce_apply_individual_use_coupon' , array (), $the_coupon , $this -> applied_coupons );
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2013-10-25 18:28:09 +00:00
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
if ( $coupon -> individual_use == 'yes' && false === apply_filters ( 'woocommerce_apply_with_individual_use_coupon' , false , $the_coupon , $coupon , $this -> applied_coupons ) ) {
2013-01-16 10:42:44 +00:00
// Reject new coupon
2013-10-18 17:10:55 +00:00
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
2013-01-16 10:42:44 +00:00
return false ;
}
2012-03-20 13:22:35 +00:00
}
}
2012-08-06 12:24:59 +00:00
2013-10-25 18:28:09 +00:00
$this -> applied_coupons [] = $coupon_code ;
2012-11-27 16:22:47 +00:00
2012-11-15 15:12:42 +00:00
// Choose free shipping
if ( $the_coupon -> enable_free_shipping () ) {
2013-08-14 20:00:34 +00:00
$packages = WC () -> shipping -> get_packages ();
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
foreach ( $packages as $i => $package ) {
$chosen_shipping_methods [ $i ] = 'free_shipping' ;
}
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
2012-11-15 15:12:42 +00:00
}
2012-08-06 12:24:59 +00:00
2013-01-31 14:42:46 +00:00
$this -> calculate_totals ();
2013-02-18 12:29:10 +00:00
$the_coupon -> add_coupon_message ( WC_Coupon :: WC_COUPON_SUCCESS );
2012-08-06 12:24:59 +00:00
do_action ( 'woocommerce_applied_coupon' , $coupon_code );
2011-12-08 12:50:50 +00:00
return true ;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} else {
2013-02-18 12:29:10 +00:00
$the_coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_NOT_EXIST );
2011-12-08 12:50:50 +00:00
return false ;
2012-03-20 13:22:35 +00:00
}
2011-12-08 12:50:50 +00:00
return false ;
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2013-10-18 17:10:55 +00:00
* Get array of applied coupon objects and codes .
2013-10-25 18:28:09 +00:00
* @ param string Type of coupons to get . Can be 'cart' or 'order' which are before and after tax respectively .
2012-03-20 13:22:35 +00:00
* @ return array of applied coupons
2011-12-08 12:50:50 +00:00
*/
2013-10-25 18:28:09 +00:00
public function get_coupons ( $type = null ) {
2013-08-14 20:00:34 +00:00
$coupons = array ();
2013-10-25 18:28:09 +00:00
if ( 'cart' == $type || is_null ( $type ) ) {
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
if ( $coupon -> apply_before_tax () )
$coupons [ $code ] = $coupon ;
2013-08-14 20:00:34 +00:00
}
}
2013-10-25 18:28:09 +00:00
}
if ( 'order' == $type || is_null ( $type ) ) {
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
if ( ! $coupon -> apply_before_tax () )
$coupons [ $code ] = $coupon ;
2013-08-14 20:00:34 +00:00
}
}
}
return $coupons ;
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2013-10-18 17:10:55 +00:00
/**
* Gets the array of applied coupon codes .
*
* @ return array of applied coupons
*/
2013-10-25 18:28:09 +00:00
public function get_applied_coupons () {
return $this -> applied_coupons ;
2013-10-18 17:10:55 +00:00
}
2014-06-11 14:10:03 +00:00
/**
* Get the discount amount for a used coupon
* @ param string $code coupon code
* @ return float discount amount
*/
public function get_coupon_discount_amount ( $code ) {
return isset ( $this -> coupon_discount_amounts [ $code ] ) ? $this -> coupon_discount_amounts [ $code ] : 0 ;
}
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Remove coupons from the cart of a defined type . Type 1 is before tax , type 2 is after tax .
2012-03-20 13:22:35 +00:00
*
2013-10-25 18:28:09 +00:00
* @ params string type - cart for before tax , order for after tax
2011-12-08 12:50:50 +00:00
*/
2013-10-25 18:28:09 +00:00
public function remove_coupons ( $type = null ) {
if ( 'cart' == $type || 1 == $type ) {
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-28 09:45:59 +00:00
if ( $coupon -> apply_before_tax () )
2013-10-18 17:10:55 +00:00
$this -> remove_coupon ( $code );
2012-03-20 13:22:35 +00:00
}
}
2013-10-25 18:28:09 +00:00
} elseif ( 'order' == $type || 2 == $type ) {
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-28 09:45:59 +00:00
if ( ! $coupon -> apply_before_tax () )
2013-10-18 17:10:55 +00:00
$this -> remove_coupon ( $code );
2012-03-20 13:22:35 +00:00
}
}
} else {
2013-10-25 18:28:09 +00:00
$this -> applied_coupons = $this -> coupon_discount_amounts = $this -> coupon_applied_count = array ();
WC () -> session -> set ( 'applied_coupons' , array () );
WC () -> session -> set ( 'coupon_discount_amounts' , array () );
2012-03-20 13:22:35 +00:00
}
2011-12-08 12:50:50 +00:00
}
2013-08-14 20:00:34 +00:00
/**
* Remove a single coupon by code
2013-10-25 18:28:09 +00:00
* @ param string $coupon_code Code of the coupon to remove
2013-11-28 17:59:09 +00:00
* @ return bool
2013-08-14 20:00:34 +00:00
*/
public function remove_coupon ( $coupon_code ) {
// Coupons are globally disabled
if ( ! $this -> coupons_enabled () )
return false ;
// Get the coupon
2013-10-18 17:10:55 +00:00
$coupon_code = apply_filters ( 'woocommerce_coupon_code' , $coupon_code );
2013-10-25 18:28:09 +00:00
$position = array_search ( $coupon_code , $this -> applied_coupons );
2013-10-28 09:45:59 +00:00
if ( $position !== false )
2013-10-25 18:28:09 +00:00
unset ( $this -> applied_coupons [ $position ] );
2013-10-18 17:10:55 +00:00
2013-10-25 18:28:09 +00:00
WC () -> session -> set ( 'applied_coupons' , $this -> applied_coupons );
2013-11-29 09:15:58 +00:00
return true ;
2013-10-18 17:10:55 +00:00
}
/**
* Function to apply discounts to a product and get the discounted price ( before tax is applied ) .
*
* @ access public
* @ param mixed $values
* @ param mixed $price
* @ param bool $add_totals ( default : false )
* @ return float price
*/
public function get_discounted_price ( $values , $price , $add_totals = false ) {
2013-10-28 09:45:59 +00:00
if ( ! $price )
2013-10-18 17:10:55 +00:00
return $price ;
2013-10-25 18:28:09 +00:00
if ( ! empty ( $this -> applied_coupons ) ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
if ( $coupon -> apply_before_tax () && $coupon -> is_valid () ) {
2014-10-10 14:44:18 +00:00
if ( $coupon -> is_valid_for_product ( $values [ 'data' ], $values ) || $coupon -> is_valid_for_cart () ) {
2013-10-28 09:45:59 +00:00
2013-10-18 17:10:55 +00:00
$discount_amount = $coupon -> get_discount_amount ( $price , $values , $single = true );
$price = max ( $price - $discount_amount , 0 );
2013-10-28 09:45:59 +00:00
2013-10-18 17:10:55 +00:00
if ( $add_totals ) {
$this -> discount_cart += $discount_amount * $values [ 'quantity' ];
$this -> increase_coupon_discount_amount ( $code , $discount_amount * $values [ 'quantity' ] );
$this -> increase_coupon_applied_count ( $code , $values [ 'quantity' ] );
}
}
}
}
}
2013-08-14 20:00:34 +00:00
2013-10-18 17:10:55 +00:00
return apply_filters ( 'woocommerce_get_discounted_price' , $price , $values , $this );
}
2013-08-14 20:00:34 +00:00
2013-10-18 17:10:55 +00:00
/**
* Function to apply cart discounts after tax .
*
* @ access public
*/
public function apply_cart_discounts_after_tax () {
2013-11-12 17:43:30 +00:00
$pre_discount_total = round ( $this -> cart_contents_total + $this -> tax_total + $this -> shipping_tax_total + $this -> shipping_total + $this -> fee_total , $this -> dp );
2013-08-14 20:00:34 +00:00
2013-10-25 18:28:09 +00:00
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
do_action ( 'woocommerce_cart_discount_after_tax_' . $coupon -> type , $coupon );
if ( $coupon -> is_valid () && ! $coupon -> apply_before_tax () && $coupon -> is_valid_for_cart () ) {
$discount_amount = $coupon -> get_discount_amount ( $pre_discount_total );
$pre_discount_total = $pre_discount_total - $discount_amount ;
$this -> discount_total += $discount_amount ;
$this -> increase_coupon_discount_amount ( $code , $discount_amount );
$this -> increase_coupon_applied_count ( $code );
}
}
2013-08-14 20:00:34 +00:00
}
2013-10-18 17:10:55 +00:00
}
/**
* Function to apply product discounts after tax .
*
* @ access public
* @ param mixed $values
2014-09-07 23:37:55 +00:00
* @ param double $price
2013-10-18 17:10:55 +00:00
*/
public function apply_product_discounts_after_tax ( $values , $price ) {
2013-10-25 18:28:09 +00:00
if ( ! empty ( $this -> applied_coupons ) ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
do_action ( 'woocommerce_product_discount_after_tax_' . $coupon -> type , $coupon , $values , $price );
if ( $coupon -> is_valid () && ! $coupon -> apply_before_tax () && $coupon -> is_valid_for_product ( $values [ 'data' ] ) ) {
$discount_amount = $coupon -> get_discount_amount ( $price , $values );
2014-02-27 14:14:14 +00:00
$this -> discount_total += $discount_amount ;
$this -> increase_coupon_discount_amount ( $code , $discount_amount );
2013-10-18 17:10:55 +00:00
$this -> increase_coupon_applied_count ( $code , $values [ 'quantity' ] );
}
}
}
}
/**
* Store how much discount each coupon grants .
*
* @ access private
* @ param mixed $code
2014-09-07 23:37:55 +00:00
* @ param double $amount
2013-10-18 17:10:55 +00:00
*/
private function increase_coupon_discount_amount ( $code , $amount ) {
if ( empty ( $this -> coupon_discount_amounts [ $code ] ) )
$this -> coupon_discount_amounts [ $code ] = 0 ;
$this -> coupon_discount_amounts [ $code ] += $amount ;
}
/**
* Store how many times each coupon is applied to cart / items
*
* @ access private
* @ param mixed $code
2014-09-08 15:35:37 +00:00
* @ param integer $count
2013-10-18 17:10:55 +00:00
*/
private function increase_coupon_applied_count ( $code , $count = 1 ) {
if ( empty ( $this -> coupon_applied_count [ $code ] ) )
$this -> coupon_applied_count [ $code ] = 0 ;
2013-08-14 20:00:34 +00:00
2013-10-18 17:10:55 +00:00
$this -> coupon_applied_count [ $code ] += $count ;
2013-08-14 20:00:34 +00:00
}
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2014-04-15 15:56:35 +00:00
/* Fees API to add additional costs to orders */
2012-11-09 21:15:15 +00:00
/*-----------------------------------------------------------------------------------*/
2012-11-12 16:08:05 +00:00
/**
* add_fee function .
2012-11-27 16:22:47 +00:00
*
2012-11-12 16:08:05 +00:00
* @ param mixed $name
* @ param mixed $amount
* @ param bool $taxable ( default : false )
* @ param string $tax_class ( default : '' )
*/
2012-12-14 21:26:07 +00:00
public function add_fee ( $name , $amount , $taxable = false , $tax_class = '' ) {
2013-12-13 21:10:25 +00:00
$new_fee_id = sanitize_title ( $name );
// Only add each fee once
foreach ( $this -> fees as $fee ) {
if ( $fee -> id == $new_fee_id ) {
return ;
}
}
2014-07-20 04:28:16 +00:00
$new_fee = new stdClass ();
$new_fee -> id = $new_fee_id ;
$new_fee -> name = esc_attr ( $name );
$new_fee -> amount = ( float ) esc_attr ( $amount );
$new_fee -> tax_class = $tax_class ;
$new_fee -> taxable = $taxable ? true : false ;
$new_fee -> tax = 0 ;
$new_fee -> tax_data = array ();
$this -> fees [] = $new_fee ;
2012-11-09 21:15:15 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-12 16:08:05 +00:00
/**
* get_fees function .
2012-11-27 16:22:47 +00:00
*
2012-11-12 16:08:05 +00:00
* @ access public
2013-11-25 12:40:16 +00:00
* @ return array
2012-11-12 16:08:05 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_fees () {
2013-08-13 16:19:20 +00:00
return array_filter ( ( array ) $this -> fees );
}
/**
* Calculate fees
*/
public function calculate_fees () {
2014-08-26 14:39:15 +00:00
// Reset fees before calculation
$this -> fee_total = 0 ;
$this -> fees = array ();
2013-08-13 16:19:20 +00:00
// Fire an action where developers can add their fees
do_action ( 'woocommerce_cart_calculate_fees' , $this );
// If fees were added, total them and calculate tax
2014-02-13 15:42:17 +00:00
if ( ! empty ( $this -> fees ) ) {
foreach ( $this -> fees as $fee_key => $fee ) {
2013-08-13 16:19:20 +00:00
$this -> fee_total += $fee -> amount ;
if ( $fee -> taxable ) {
// Get tax rates
$tax_rates = $this -> tax -> get_rates ( $fee -> tax_class );
$fee_taxes = $this -> tax -> calc_tax ( $fee -> amount , $tax_rates , false );
2014-07-19 04:08:02 +00:00
2014-01-22 14:09:55 +00:00
if ( ! empty ( $fee_taxes ) ) {
2014-02-13 15:42:17 +00:00
// Set the tax total for this fee
$this -> fees [ $fee_key ] -> tax = array_sum ( $fee_taxes );
2013-08-13 16:19:20 +00:00
2014-07-20 04:28:16 +00:00
// Set tax data - Since 2.2
$this -> fees [ $fee_key ] -> tax_data = $fee_taxes ;
2014-01-22 14:09:55 +00:00
// Tax rows - merge the totals we just got
2014-02-20 16:39:57 +00:00
foreach ( array_keys ( $this -> taxes + $fee_taxes ) as $key ) {
2014-02-05 11:30:47 +00:00
$this -> taxes [ $key ] = ( isset ( $fee_taxes [ $key ] ) ? $fee_taxes [ $key ] : 0 ) + ( isset ( $this -> taxes [ $key ] ) ? $this -> taxes [ $key ] : 0 );
2014-01-22 14:09:55 +00:00
}
2013-08-13 16:19:20 +00:00
}
}
}
}
2012-11-09 21:15:15 +00:00
}
2014-09-14 19:44:56 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +00:00
/* Get Formatted Totals */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
/**
2012-08-15 17:08:42 +00:00
* Get the total of all order discounts ( after tax discounts ) .
2012-03-20 13:22:35 +00:00
*
* @ return float
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_order_discount_total () {
2011-12-08 12:50:50 +00:00
return $this -> discount_total ;
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get the total of all cart discounts ( before tax discounts ) .
2012-03-20 13:22:35 +00:00
*
* @ return float
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_cart_discount_total () {
2011-12-08 12:50:50 +00:00
return $this -> discount_cart ;
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the order total ( after calculation ) .
2012-03-20 13:22:35 +00:00
*
* @ return string formatted price
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_total () {
2013-11-25 13:34:21 +00:00
return apply_filters ( 'woocommerce_cart_total' , wc_price ( $this -> total ) );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-01-09 18:00:52 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the total excluding taxes .
2012-03-20 13:22:35 +00:00
*
* @ return string formatted price
2012-01-09 18:00:52 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_total_ex_tax () {
2012-01-09 18:08:56 +00:00
$total = $this -> total - $this -> tax_total - $this -> shipping_tax_total ;
2013-10-28 09:45:59 +00:00
if ( $total < 0 )
2013-10-25 18:28:09 +00:00
$total = 0 ;
2013-11-25 13:34:21 +00:00
return apply_filters ( 'woocommerce_cart_total_ex_tax' , wc_price ( $total ) );
2012-01-09 18:00:52 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the cart contents total ( after calculation ) .
2012-03-20 13:22:35 +00:00
*
* @ return string formatted price
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_cart_total () {
2012-03-20 13:22:35 +00:00
if ( ! $this -> prices_include_tax ) {
2013-11-25 13:34:21 +00:00
$cart_contents_total = wc_price ( $this -> cart_contents_total );
2012-03-20 13:22:35 +00:00
} else {
2013-11-25 13:34:21 +00:00
$cart_contents_total = wc_price ( $this -> cart_contents_total + $this -> tax_total );
2012-03-20 13:22:35 +00:00
}
2012-05-24 05:58:19 +00:00
return apply_filters ( 'woocommerce_cart_contents_total' , $cart_contents_total );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the sub total ( after calculation ) .
2012-03-20 13:22:35 +00:00
*
* @ params bool whether to include compound taxes
* @ return string formatted price
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_cart_subtotal ( $compound = false ) {
2012-08-06 12:24:59 +00:00
2011-12-29 01:18:59 +00:00
// If the cart has compound tax, we want to show the subtotal as
// cart + shipping + non-compound taxes (after discount)
2012-03-20 13:22:35 +00:00
if ( $compound ) {
2012-08-06 12:24:59 +00:00
2013-11-25 13:34:21 +00:00
$cart_subtotal = wc_price ( $this -> cart_contents_total + $this -> shipping_total + $this -> get_taxes_total ( false , false ) );
2012-08-06 12:24:59 +00:00
2011-12-29 01:18:59 +00:00
// Otherwise we show cart items totals only (before discount)
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
// Display varies depending on settings
if ( $this -> tax_display_cart == 'excl' ) {
2012-05-23 02:03:41 +00:00
2013-11-25 13:34:21 +00:00
$cart_subtotal = wc_price ( $this -> subtotal_ex_tax );
2012-08-06 12:24:59 +00:00
2012-06-10 14:48:52 +00:00
if ( $this -> tax_total > 0 && $this -> prices_include_tax ) {
2013-10-18 17:10:55 +00:00
$cart_subtotal .= ' <small>' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2013-11-25 13:34:21 +00:00
$cart_subtotal = wc_price ( $this -> subtotal );
2012-08-06 12:24:59 +00:00
2012-06-10 14:48:52 +00:00
if ( $this -> tax_total > 0 && ! $this -> prices_include_tax ) {
2013-10-18 17:10:55 +00:00
$cart_subtotal .= ' <small>' . WC () -> countries -> inc_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
2012-12-03 16:36:54 +00:00
2012-03-20 13:22:35 +00:00
}
}
2012-05-23 02:03:41 +00:00
return apply_filters ( 'woocommerce_cart_subtotal' , $cart_subtotal , $compound , $this );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2013-06-17 11:21:06 +00:00
/**
* Get the product row price per item .
*
2013-10-16 12:24:46 +00:00
* @ param WC_Product $_product
2013-06-17 11:21:06 +00:00
* @ return string formatted price
*/
public function get_product_price ( $_product ) {
if ( $this -> tax_display_cart == 'excl' )
$product_price = $_product -> get_price_excluding_tax ();
else
$product_price = $_product -> get_price_including_tax ();
2013-11-25 13:34:21 +00:00
return apply_filters ( 'woocommerce_cart_product_price' , wc_price ( $product_price ), $_product );
2013-06-17 11:21:06 +00:00
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get the product row subtotal .
2011-12-08 12:50:50 +00:00
*
* 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
2012-03-20 13:22:35 +00:00
*
2013-10-16 13:29:48 +00:00
* @ param WC_Product $_product
* @ param int quantity
2012-03-20 13:22:35 +00:00
* @ return string formatted price
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_product_subtotal ( $_product , $quantity ) {
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$price = $_product -> get_price ();
$taxable = $_product -> is_taxable ();
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// Taxable
2012-03-20 13:22:35 +00:00
if ( $taxable ) {
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
if ( $this -> tax_display_cart == 'excl' ) {
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
$row_price = $_product -> get_price_excluding_tax ( $quantity );
2013-11-25 13:34:21 +00:00
$product_subtotal = wc_price ( $row_price );
2012-08-06 12:24:59 +00:00
2014-08-07 16:26:48 +00:00
if ( $this -> prices_include_tax && $this -> tax_total > 0 ) {
2013-10-18 17:10:55 +00:00
$product_subtotal .= ' <small class="tax_label">' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
2014-08-07 16:26:48 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
$row_price = $_product -> get_price_including_tax ( $quantity );
2013-11-25 13:34:21 +00:00
$product_subtotal = wc_price ( $row_price );
2012-08-06 12:24:59 +00:00
2014-08-07 16:26:48 +00:00
if ( ! $this -> prices_include_tax && $this -> tax_total > 0 ) {
2013-10-18 17:10:55 +00:00
$product_subtotal .= ' <small class="tax_label">' . WC () -> countries -> inc_tax_or_vat () . '</small>' ;
2014-08-07 16:26:48 +00:00
}
2012-12-03 16:36:54 +00:00
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
// Non-taxable
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2012-12-03 16:36:54 +00:00
$row_price = $price * $quantity ;
2013-11-25 13:34:21 +00:00
$product_subtotal = wc_price ( $row_price );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
return apply_filters ( 'woocommerce_cart_product_subtotal' , $product_subtotal , $_product , $quantity , $this );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the cart tax ( after calculation ) .
2012-03-20 13:22:35 +00:00
*
* @ return string formatted price
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_cart_tax () {
2013-11-25 13:34:21 +00:00
$cart_total_tax = wc_round_tax_total ( $this -> tax_total + $this -> shipping_tax_total );
2013-10-25 18:28:09 +00:00
2013-11-25 13:34:21 +00:00
return apply_filters ( 'woocommerce_get_cart_tax' , $cart_total_tax ? wc_price ( $cart_total_tax ) : '' );
2011-12-30 21:11:18 +00:00
}
2012-08-06 12:24:59 +00:00
2014-06-11 14:10:03 +00:00
/**
* Get a tax amount
* @ param string $tax_rate_id
* @ return float amount
*/
public function get_tax_amount ( $tax_rate_id ) {
return isset ( $this -> taxes [ $tax_rate_id ] ) ? $this -> taxes [ $tax_rate_id ] : 0 ;
}
/**
* Get a tax amount
* @ param string $tax_rate_id
* @ return float amount
*/
public function get_shipping_tax_amount ( $tax_rate_id ) {
return isset ( $this -> shipping_taxes [ $tax_rate_id ] ) ? $this -> shipping_taxes [ $tax_rate_id ] : 0 ;
}
2011-12-30 21:11:18 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get tax row amounts with or without compound taxes includes .
2014-03-21 00:54:18 +00:00
*
2013-10-24 12:15:42 +00:00
* @ param boolean $compound True if getting compound taxes
* @ param boolean $display True if getting total to display
2012-03-20 13:22:35 +00:00
* @ return float price
2011-12-30 21:11:18 +00:00
*/
2013-10-24 12:15:42 +00:00
public function get_taxes_total ( $compound = true , $display = true ) {
2011-12-30 21:11:18 +00:00
$total = 0 ;
2012-03-20 13:22:35 +00:00
foreach ( $this -> taxes as $key => $tax ) {
if ( ! $compound && $this -> tax -> is_compound ( $key ) ) continue ;
2011-12-30 21:11:18 +00:00
$total += $tax ;
2012-03-20 13:22:35 +00:00
}
foreach ( $this -> shipping_taxes as $key => $tax ) {
if ( ! $compound && $this -> tax -> is_compound ( $key ) ) continue ;
2012-01-27 15:00:03 +00:00
$total += $tax ;
2012-03-20 13:22:35 +00:00
}
2014-03-21 00:54:18 +00:00
if ( $display ) {
$total = wc_round_tax_total ( $total );
}
return apply_filters ( 'woocommerce_cart_taxes_total' , $total , $compound , $display , $this );
2011-12-30 21:11:18 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the total ( product ) discount amount - these are applied before tax .
2012-03-20 13:22:35 +00:00
*
* @ return mixed formatted price or false if there are none
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_discounts_before_tax () {
2012-03-20 13:22:35 +00:00
if ( $this -> discount_cart ) {
2013-11-25 13:34:21 +00:00
$discounts_before_tax = wc_price ( $this -> discount_cart );
2012-05-23 02:15:04 +00:00
} else {
$discounts_before_tax = false ;
2012-03-20 13:22:35 +00:00
}
2012-05-23 02:15:04 +00:00
return apply_filters ( 'woocommerce_cart_discounts_before_tax' , $discounts_before_tax , $this );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the order discount amount - these are applied after tax .
2012-03-20 13:22:35 +00:00
*
* @ return mixed formatted price or false if there are none
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_discounts_after_tax () {
2012-03-20 13:22:35 +00:00
if ( $this -> discount_total ) {
2013-11-25 13:34:21 +00:00
$discounts_after_tax = wc_price ( $this -> discount_total );
2012-05-23 02:15:04 +00:00
} else {
$discounts_after_tax = false ;
2012-03-20 13:22:35 +00:00
}
2012-05-23 02:15:04 +00:00
return apply_filters ( 'woocommerce_cart_discounts_after_tax' , $discounts_after_tax , $this );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
/**
2012-08-15 17:08:42 +00:00
* Gets the total discount amount - both kinds .
2012-03-20 13:22:35 +00:00
*
* @ return mixed formatted price or false if there are none
2011-12-08 12:50:50 +00:00
*/
2012-12-14 21:26:07 +00:00
public function get_total_discount () {
2012-03-20 13:22:35 +00:00
if ( $this -> discount_total || $this -> discount_cart ) {
2013-11-25 13:34:21 +00:00
$total_discount = wc_price ( $this -> discount_total + $this -> discount_cart );
2012-05-23 02:15:04 +00:00
} else {
$total_discount = false ;
2012-03-20 13:22:35 +00:00
}
2012-05-23 02:15:04 +00:00
return apply_filters ( 'woocommerce_cart_total_discount' , $total_discount , $this );
}
2013-10-04 01:48:47 +00:00
}