2011-08-09 15:16:18 +00:00
< ? php
/**
2015-11-03 13:53:50 +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
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Classes
2018-03-16 18:08:52 +00:00
* @ version 2.1 . 0
2011-08-09 15:16:18 +00:00
*/
2017-08-08 09:51:35 +00:00
2018-03-16 18:08:52 +00:00
defined ( 'ABSPATH' ) || exit ;
2017-08-08 09:51:35 +00:00
2018-03-16 18:08:52 +00:00
require_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-cart.php' ;
2018-05-22 11:15:13 +00:00
require_once WC_ABSPATH . 'includes/class-wc-cart-fees.php' ;
2018-03-16 18:08:52 +00:00
require_once WC_ABSPATH . 'includes/class-wc-cart-session.php' ;
2017-08-08 09:51:35 +00:00
/**
* WC_Cart class .
*/
2017-07-28 21:12:56 +00:00
class WC_Cart extends WC_Legacy_Cart {
2012-08-14 19:42:38 +00:00
2017-08-08 09:51:35 +00:00
/**
* Contains an array of cart items .
*
* @ var array
*/
2013-10-18 17:10:55 +00:00
public $cart_contents = array ();
2012-08-14 19:42:38 +00:00
2017-08-08 09:51:35 +00:00
/**
* Contains an array of removed cart items so we can restore them if needed .
*
* @ var array
*/
2015-01-14 12:24:01 +00:00
public $removed_cart_contents = array ();
2015-01-07 18:52:17 +00:00
2017-08-08 09:51:35 +00:00
/**
* Contains an array of coupon codes applied to the cart .
*
* @ var array
*/
2013-10-18 17:10:55 +00:00
public $applied_coupons = array ();
2017-08-18 11:24:17 +00:00
/**
* This stores the chosen shipping methods for the cart item packages .
*
* @ var array
*/
protected $shipping_methods ;
2017-08-18 10:29:26 +00:00
/**
2017-08-18 11:51:45 +00:00
* Total defaults used to reset .
2017-08-18 10:29:26 +00:00
*
* @ var array
*/
protected $default_totals = array (
2017-08-18 11:05:18 +00:00
'subtotal' => 0 ,
'subtotal_tax' => 0 ,
'shipping_total' => 0 ,
'shipping_tax' => 0 ,
2017-08-18 11:24:17 +00:00
'shipping_taxes' => array (),
2017-08-18 11:05:18 +00:00
'discount_total' => 0 ,
'discount_tax' => 0 ,
'cart_contents_total' => 0 ,
'cart_contents_tax' => 0 ,
2017-08-18 11:24:17 +00:00
'cart_contents_taxes' => array (),
2017-08-18 11:05:18 +00:00
'fee_total' => 0 ,
'fee_tax' => 0 ,
2017-08-18 11:24:17 +00:00
'fee_taxes' => array (),
2017-08-18 11:05:18 +00:00
'total' => 0 ,
'total_tax' => 0 ,
2017-08-18 10:29:26 +00:00
);
/**
* Store calculated totals .
*
* @ var array
*/
protected $totals = array ();
/**
* Reference to the cart session handling class .
*
* @ var WC_Cart_Session
*/
protected $session ;
2017-08-22 14:17:58 +00:00
/**
* Reference to the cart fees API class .
*
* @ var WC_Cart_Fees
*/
protected $fees_api ;
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 .
2011-11-06 13:45:18 +00:00
*/
2012-12-15 12:01:42 +00:00
public function __construct () {
2020-06-02 16:57:55 +00:00
$this -> session = new WC_Cart_Session ( $this );
$this -> fees_api = new WC_Cart_Fees ( $this );
2017-08-18 10:29:26 +00:00
2017-11-03 19:49:45 +00:00
// Register hooks for the objects.
2017-11-08 15:17:52 +00:00
$this -> session -> init ();
2017-11-03 19:49:45 +00:00
2015-01-27 16:02:57 +00:00
add_action ( 'woocommerce_add_to_cart' , array ( $this , 'calculate_totals' ), 20 , 0 );
add_action ( 'woocommerce_applied_coupon' , array ( $this , 'calculate_totals' ), 20 , 0 );
2017-08-18 10:29:26 +00:00
add_action ( 'woocommerce_cart_item_removed' , array ( $this , 'calculate_totals' ), 20 , 0 );
add_action ( 'woocommerce_cart_item_restored' , array ( $this , 'calculate_totals' ), 20 , 0 );
add_action ( 'woocommerce_check_cart_items' , array ( $this , 'check_cart_items' ), 1 );
add_action ( 'woocommerce_check_cart_items' , array ( $this , 'check_cart_coupons' ), 1 );
2019-07-09 21:07:44 +00:00
add_action ( 'woocommerce_after_checkout_validation' , array ( $this , 'check_customer_coupons' ), 1 , 2 );
2011-08-09 15:16:18 +00:00
}
2012-08-06 12:24:59 +00:00
2017-11-03 19:50:43 +00:00
/**
* When cloning , ensure object properties are handled .
2017-11-08 15:18:08 +00:00
*
* These properties store a reference to the cart , so we use new instead of clone .
2017-11-03 19:50:43 +00:00
*/
public function __clone () {
2017-12-05 06:21:02 +00:00
$this -> session = clone $this -> session ;
$this -> fees_api = clone $this -> fees_api ;
2017-11-03 19:50:43 +00:00
}
2017-08-18 10:29:26 +00:00
/*
|--------------------------------------------------------------------------
| Getters .
|--------------------------------------------------------------------------
|
| Methods to retrieve class properties and avoid direct access .
*/
2014-09-14 19:29:32 +00:00
/**
2017-08-18 10:29:26 +00:00
* Gets cart contents .
*
* @ since 3.2 . 0
2018-07-01 17:12:04 +00:00
* @ return array of cart items
2014-09-14 19:29:32 +00:00
*/
2017-08-18 10:29:26 +00:00
public function get_cart_contents () {
2018-10-29 20:08:22 +00:00
return apply_filters ( 'woocommerce_get_cart_contents' , ( array ) $this -> cart_contents );
2014-09-14 19:29:32 +00:00
}
/**
2017-08-18 10:29:26 +00:00
* Return items removed from the cart .
*
* @ since 3.2 . 0
* @ return array
2014-09-14 19:50:22 +00:00
*/
2017-08-18 10:29:26 +00:00
public function get_removed_cart_contents () {
2017-08-18 10:44:07 +00:00
return ( array ) $this -> removed_cart_contents ;
2014-09-14 19:29:32 +00:00
}
2014-06-30 14:14:32 +00:00
/**
2017-08-18 10:29:26 +00:00
* Gets the array of applied coupon codes .
2014-06-30 14:14:32 +00:00
*
2017-08-18 10:29:26 +00:00
* @ return array of applied coupons
*/
public function get_applied_coupons () {
2017-08-18 10:44:07 +00:00
return ( array ) $this -> applied_coupons ;
2014-06-30 14:14:32 +00:00
}
2016-09-02 01:03:52 +00:00
/**
2017-08-18 10:29:26 +00:00
* Return all calculated coupon totals .
*
* @ since 3.2 . 0
* @ return array
2016-09-02 01:03:52 +00:00
*/
2017-08-18 10:29:26 +00:00
public function get_coupon_discount_totals () {
2017-08-18 10:44:07 +00:00
return ( array ) $this -> coupon_discount_totals ;
2017-08-18 10:29:26 +00:00
}
/**
* Return all calculated coupon tax totals .
*
* @ since 3.2 . 0
* @ return array
*/
public function get_coupon_discount_tax_totals () {
2017-08-18 10:44:07 +00:00
return ( array ) $this -> coupon_discount_tax_totals ;
2017-08-18 10:29:26 +00:00
}
2012-08-06 12:24:59 +00:00
2017-08-18 10:29:26 +00:00
/**
* Return all calculated totals .
*
* @ since 3.2 . 0
* @ return array
*/
public function get_totals () {
2017-08-18 16:18:41 +00:00
return empty ( $this -> totals ) ? $this -> default_totals : $this -> totals ;
2017-08-18 10:29:26 +00:00
}
2013-11-22 23:14:08 +00:00
2017-08-28 21:12:35 +00:00
/**
* Get a total .
*
* @ since 3.2 . 0
* @ param string $key Key of element in $totals array .
* @ return mixed
*/
protected function get_totals_var ( $key ) {
return isset ( $this -> totals [ $key ] ) ? $this -> totals [ $key ] : $this -> default_totals [ $key ];
}
2017-08-18 11:05:18 +00:00
/**
* Get subtotal .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_subtotal () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'subtotal' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get subtotal .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_subtotal_tax () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'subtotal_tax' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get discount_total .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_discount_total () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'discount_total' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get discount_tax .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_discount_tax () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'discount_tax' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get shipping_total .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_shipping_total () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'shipping_total' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get shipping_tax .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_shipping_tax () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'shipping_tax' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Gets cart total . This is the total of items in the cart , but after discounts . Subtotal is before discounts .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_cart_contents_total () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'cart_contents_total' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Gets cart tax amount .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_cart_contents_tax () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'cart_contents_tax' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Gets cart total after calculation .
*
* @ since 3.2 . 0
* @ param string $context If the context is view , the value will be formatted for display . This keeps it compatible with pre - 3.2 versions .
* @ return float
*/
2017-08-18 11:51:45 +00:00
public function get_total ( $context = 'view' ) {
2017-09-20 21:45:37 +00:00
$total = apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'total' ) );
2017-08-18 11:05:18 +00:00
return 'view' === $context ? apply_filters ( 'woocommerce_cart_total' , wc_price ( $total ) ) : $total ;
}
/**
* Get total tax amount .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_total_tax () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'total_tax' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get total fee amount .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_fee_total () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'fee_total' ) );
2017-08-18 11:05:18 +00:00
}
/**
* Get total fee tax amount .
*
* @ since 3.2 . 0
* @ return float
*/
public function get_fee_tax () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'fee_tax' ) );
2017-08-18 11:05:18 +00:00
}
2017-08-18 11:24:17 +00:00
/**
* Get taxes .
*
* @ since 3.2 . 0
*/
public function get_shipping_taxes () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'shipping_taxes' ) );
2017-08-18 11:24:17 +00:00
}
/**
* Get taxes .
*
* @ since 3.2 . 0
*/
public function get_cart_contents_taxes () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'cart_contents_taxes' ) );
2017-08-18 11:24:17 +00:00
}
/**
* Get taxes .
*
* @ since 3.2 . 0
*/
public function get_fee_taxes () {
2017-09-20 21:45:37 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , $this -> get_totals_var ( 'fee_taxes' ) );
2017-08-18 11:24:17 +00:00
}
2017-12-15 13:39:35 +00:00
/**
* Return whether or not the cart is displaying prices including tax , rather than excluding tax .
*
* @ since 3.3 . 0
* @ return bool
*/
2017-12-15 15:18:17 +00:00
public function display_prices_including_tax () {
2020-07-13 14:21:19 +00:00
return apply_filters ( 'woocommerce_cart_' . __FUNCTION__ , 'incl' === $this -> get_tax_price_display_mode () );
2017-12-15 13:39:35 +00:00
}
2017-08-18 10:29:26 +00:00
/*
|--------------------------------------------------------------------------
| Setters .
|--------------------------------------------------------------------------
|
| Methods to set class properties and avoid direct access .
*/
2016-02-09 23:36:15 +00:00
2017-08-18 10:29:26 +00:00
/**
* Sets the contents of the cart .
*
* @ param array $value Cart array .
*/
public function set_cart_contents ( $value ) {
$this -> cart_contents = ( array ) $value ;
}
2012-08-06 12:24:59 +00:00
2017-08-18 10:29:26 +00:00
/**
* Set items removed from the cart .
*
* @ since 3.2 . 0
* @ param array $value Item array .
*/
public function set_removed_cart_contents ( $value = array () ) {
$this -> removed_cart_contents = ( array ) $value ;
}
2012-08-06 12:24:59 +00:00
2017-08-18 10:29:26 +00:00
/**
* Sets the array of applied coupon codes .
*
* @ param array $value List of applied coupon codes .
*/
public function set_applied_coupons ( $value = array () ) {
$this -> applied_coupons = ( array ) $value ;
}
2013-11-22 23:14:08 +00:00
2017-08-18 10:29:26 +00:00
/**
2018-09-26 15:43:17 +00:00
* Sets the array of calculated coupon totals .
2017-08-18 10:29:26 +00:00
*
* @ since 3.2 . 0
* @ param array $value Value to set .
*/
public function set_coupon_discount_totals ( $value = array () ) {
$this -> coupon_discount_totals = ( array ) $value ;
}
/**
2018-09-26 15:43:17 +00:00
* Sets the array of calculated coupon tax totals .
2017-08-18 10:29:26 +00:00
*
* @ since 3.2 . 0
* @ param array $value Value to set .
*/
public function set_coupon_discount_tax_totals ( $value = array () ) {
$this -> coupon_discount_tax_totals = ( array ) $value ;
}
2013-11-22 23:14:08 +00:00
2017-08-18 10:29:26 +00:00
/**
* Set all calculated totals .
*
* @ since 3.2 . 0
* @ param array $value Value to set .
*/
public function set_totals ( $value = array () ) {
$this -> totals = wp_parse_args ( $value , $this -> default_totals );
}
2012-08-06 12:24:59 +00:00
2017-08-18 11:05:18 +00:00
/**
* Set subtotal .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_subtotal ( $value ) {
2017-12-04 20:38:14 +00:00
$this -> totals [ 'subtotal' ] = wc_format_decimal ( $value , wc_get_price_decimals () );
2017-08-18 11:05:18 +00:00
}
/**
* Set subtotal .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_subtotal_tax ( $value ) {
2020-03-02 08:49:33 +00:00
$this -> totals [ 'subtotal_tax' ] = $value ;
2017-08-18 11:05:18 +00:00
}
/**
* Set discount_total .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_discount_total ( $value ) {
2020-03-02 08:49:33 +00:00
$this -> totals [ 'discount_total' ] = $value ;
2017-08-18 11:05:18 +00:00
}
/**
* Set discount_tax .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_discount_tax ( $value ) {
2020-03-02 08:49:33 +00:00
$this -> totals [ 'discount_tax' ] = $value ;
2017-08-18 11:05:18 +00:00
}
/**
* Set shipping_total .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_shipping_total ( $value ) {
2017-12-04 20:38:14 +00:00
$this -> totals [ 'shipping_total' ] = wc_format_decimal ( $value , wc_get_price_decimals () );
2017-08-18 11:05:18 +00:00
}
/**
* Set shipping_tax .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_shipping_tax ( $value ) {
2020-03-02 08:49:33 +00:00
$this -> totals [ 'shipping_tax' ] = $value ;
2017-08-18 11:05:18 +00:00
}
/**
* Set cart_contents_total .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_cart_contents_total ( $value ) {
2017-12-04 20:38:14 +00:00
$this -> totals [ 'cart_contents_total' ] = wc_format_decimal ( $value , wc_get_price_decimals () );
2017-08-18 11:05:18 +00:00
}
/**
* Set cart tax amount .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_cart_contents_tax ( $value ) {
2020-03-02 08:49:33 +00:00
$this -> totals [ 'cart_contents_tax' ] = $value ;
2017-08-18 11:05:18 +00:00
}
/**
* Set cart total .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_total ( $value ) {
2017-12-04 20:38:14 +00:00
$this -> totals [ 'total' ] = wc_format_decimal ( $value , wc_get_price_decimals () );
2017-08-18 11:05:18 +00:00
}
/**
* Set total tax amount .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_total_tax ( $value ) {
2020-03-02 08:49:33 +00:00
// We round here because this is a total entry, as opposed to line items in other setters.
2017-08-18 11:51:45 +00:00
$this -> totals [ 'total_tax' ] = wc_round_tax_total ( $value );
2017-08-18 11:05:18 +00:00
}
/**
* Set fee amount .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_fee_total ( $value ) {
2017-12-04 20:38:14 +00:00
$this -> totals [ 'fee_total' ] = wc_format_decimal ( $value , wc_get_price_decimals () );
2017-08-18 11:05:18 +00:00
}
/**
* Set fee tax .
*
* @ since 3.2 . 0
* @ param string $value Value to set .
*/
public function set_fee_tax ( $value ) {
2020-03-02 08:49:33 +00:00
$this -> totals [ 'fee_tax' ] = $value ;
2017-08-18 11:05:18 +00:00
}
/**
2017-08-18 11:24:17 +00:00
* Set taxes .
*
* @ since 3.2 . 0
* @ param array $value Tax values .
*/
public function set_shipping_taxes ( $value ) {
$this -> totals [ 'shipping_taxes' ] = ( array ) $value ;
}
/**
* Set taxes .
2017-08-18 11:05:18 +00:00
*
2017-08-18 11:24:17 +00:00
* @ since 3.2 . 0
2017-08-18 11:05:18 +00:00
* @ param array $value Tax values .
*/
2017-08-18 11:24:17 +00:00
public function set_cart_contents_taxes ( $value ) {
$this -> totals [ 'cart_contents_taxes' ] = ( array ) $value ;
}
/**
* Set taxes .
*
* @ since 3.2 . 0
* @ param array $value Tax values .
*/
public function set_fee_taxes ( $value ) {
$this -> totals [ 'fee_taxes' ] = ( array ) $value ;
2017-08-18 11:05:18 +00:00
}
2017-08-18 10:29:26 +00:00
/*
|--------------------------------------------------------------------------
| Helper methods .
|--------------------------------------------------------------------------
*/
2015-05-01 14:33:24 +00:00
2017-08-18 11:24:17 +00:00
/**
* Returns the cart and shipping taxes , merged .
*
* @ return array merged taxes
*/
public function get_taxes () {
return apply_filters ( 'woocommerce_cart_get_taxes' , wc_array_merge_recursive_numeric ( $this -> get_shipping_taxes (), $this -> get_cart_contents_taxes (), $this -> get_fee_taxes () ), $this );
}
2017-08-18 10:29:26 +00:00
/**
* Returns the contents of the cart in an array .
*
* @ return array contents of the cart
*/
public function get_cart () {
if ( ! did_action ( 'wp_loaded' ) ) {
wc_doing_it_wrong ( __FUNCTION__ , __ ( 'Get cart should not be called before the wp_loaded action.' , 'woocommerce' ), '2.3' );
2016-09-02 01:03:52 +00:00
}
2018-10-30 09:58:39 +00:00
if ( ! did_action ( 'woocommerce_load_cart_from_session' ) ) {
2017-08-18 10:29:26 +00:00
$this -> session -> get_cart_from_session ();
2011-12-08 12:50:50 +00:00
}
2017-08-18 10:29:26 +00:00
return array_filter ( $this -> get_cart_contents () );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
2017-08-18 10:29:26 +00:00
* Returns a specific item in the cart .
*
* @ param string $item_key Cart item key .
* @ return array Item data
2016-09-02 01:03:52 +00:00
*/
2017-08-18 10:29:26 +00:00
public function get_cart_item ( $item_key ) {
return isset ( $this -> cart_contents [ $item_key ] ) ? $this -> cart_contents [ $item_key ] : array ();
}
2012-08-06 12:24:59 +00:00
2017-08-18 10:29:26 +00:00
/**
* Checks if the cart is empty .
*
* @ return bool
*/
public function is_empty () {
return 0 === count ( $this -> get_cart () );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Empties the cart and optionally the persistent cart too .
*
2017-08-08 09:51:35 +00:00
* @ param bool $clear_persistent_cart Should the persistant cart be cleared too . Defaults to true .
2016-09-02 01:03:52 +00:00
*/
public function empty_cart ( $clear_persistent_cart = true ) {
2019-04-03 10:36:43 +00:00
2019-10-30 08:14:45 +00:00
do_action ( 'woocommerce_before_cart_emptied' , $clear_persistent_cart );
2019-04-03 10:36:43 +00:00
2017-08-18 10:29:26 +00:00
$this -> cart_contents = array ();
$this -> removed_cart_contents = array ();
$this -> shipping_methods = array ();
$this -> coupon_discount_totals = array ();
$this -> coupon_discount_tax_totals = array ();
$this -> applied_coupons = array ();
$this -> totals = $this -> default_totals ;
2012-08-06 12:24:59 +00:00
2017-08-18 10:29:26 +00:00
if ( $clear_persistent_cart ) {
$this -> session -> persistent_cart_destroy ();
2012-03-04 12:37:41 +00:00
}
2012-08-06 12:24:59 +00:00
2017-12-08 11:15:08 +00:00
$this -> fees_api -> remove_all_fees ();
2019-10-30 08:14:45 +00:00
do_action ( 'woocommerce_cart_emptied' , $clear_persistent_cart );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get number of items in the cart .
2017-08-08 09:51:35 +00:00
*
2016-09-02 01:03:52 +00:00
* @ return int
*/
public function get_cart_contents_count () {
return apply_filters ( 'woocommerce_cart_contents_count' , array_sum ( wp_list_pluck ( $this -> get_cart (), 'quantity' ) ) );
}
2012-08-06 12:13:32 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get weight of items in the cart .
2017-08-08 09:51:35 +00:00
*
2016-09-02 01:03:52 +00:00
* @ since 2.5 . 0
* @ return int
*/
public function get_cart_contents_weight () {
$weight = 0 ;
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2018-03-12 15:54:00 +00:00
if ( $values [ 'data' ] -> has_weight () ) {
$weight += ( float ) $values [ 'data' ] -> get_weight () * $values [ 'quantity' ];
}
2015-05-14 21:18:53 +00:00
}
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_contents_weight' , $weight );
}
2014-10-24 18:56:06 +00:00
2016-09-02 01:03:52 +00:00
/**
2017-08-18 10:29:26 +00:00
* Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines .
2016-09-02 02:40:36 +00:00
*
2017-08-18 10:29:26 +00:00
* @ return array
2016-09-02 02:40:36 +00:00
*/
2017-08-18 10:29:26 +00:00
public function get_cart_item_quantities () {
$quantities = array ();
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
$product = $values [ 'data' ];
$quantities [ $product -> get_stock_managed_by_id () ] = isset ( $quantities [ $product -> get_stock_managed_by_id () ] ) ? $quantities [ $product -> get_stock_managed_by_id () ] + $values [ 'quantity' ] : $values [ 'quantity' ];
}
return $quantities ;
2016-09-02 01:03:52 +00:00
}
2014-10-24 18:56:06 +00:00
2016-09-02 01:03:52 +00:00
/**
* Check all cart items for errors .
*/
public function check_cart_items () {
$return = true ;
$result = $this -> check_cart_item_validity ();
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
if ( is_wp_error ( $result ) ) {
wc_add_notice ( $result -> get_error_message (), 'error' );
$return = false ;
}
2014-10-24 18:56:06 +00:00
2016-09-02 01:03:52 +00:00
$result = $this -> check_cart_item_stock ();
2014-10-24 18:56:06 +00:00
2016-09-02 01:03:52 +00:00
if ( is_wp_error ( $result ) ) {
wc_add_notice ( $result -> get_error_message (), 'error' );
$return = false ;
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
return $return ;
}
2013-10-25 18:28:09 +00:00
2016-09-02 01:03:52 +00:00
/**
* Check cart coupons for errors .
*/
public function check_cart_coupons () {
2017-08-18 11:51:45 +00:00
foreach ( $this -> get_applied_coupons () as $code ) {
2016-09-02 01:03:52 +00:00
$coupon = new WC_Coupon ( $code );
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
if ( ! $coupon -> is_valid () ) {
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_INVALID_REMOVED );
$this -> remove_coupon ( $code );
2012-06-13 18:58:06 +00:00
}
}
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Looks through cart items and checks the posts are not trashed or deleted .
*
* @ return bool | WP_Error
*/
public function check_cart_item_validity () {
$return = true ;
2013-07-30 10:51:50 +00:00
2016-09-02 01:03:52 +00:00
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2016-11-02 18:50:42 +00:00
$product = $values [ 'data' ];
2013-07-30 10:51:50 +00:00
2016-11-02 18:50:42 +00:00
if ( ! $product || ! $product -> exists () || 'trash' === $product -> get_status () ) {
2016-09-02 01:03:52 +00:00
$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' ) );
}
2013-07-30 10:51:50 +00:00
}
2016-09-02 01:03:52 +00:00
return $return ;
}
2012-12-12 21:14:19 +00:00
2016-09-02 01:03:52 +00:00
/**
* Looks through the cart to check each item is in stock . If not , add an error .
*
* @ return bool | WP_Error
*/
public function check_cart_item_stock () {
2018-06-07 16:42:23 +00:00
$error = new WP_Error ();
$product_qty_in_cart = $this -> get_cart_item_quantities ();
$current_session_order_id = isset ( WC () -> session -> order_awaiting_payment ) ? absint ( WC () -> session -> order_awaiting_payment ) : 0 ;
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
2016-11-02 18:50:42 +00:00
$product = $values [ 'data' ];
2012-08-06 12:24:59 +00:00
2018-06-07 16:42:23 +00:00
// Check stock based on stock-status.
2016-11-02 18:50:42 +00:00
if ( ! $product -> is_in_stock () ) {
2016-10-29 17:32:38 +00:00
/* translators: %s: product name */
2017-02-27 17:39:05 +00:00
$error -> add ( 'out-of-stock' , sprintf ( __ ( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologize for any inconvenience caused.' , 'woocommerce' ), $product -> get_name () ) );
2016-09-02 01:03:52 +00:00
return $error ;
}
2012-08-06 12:24:59 +00:00
2018-06-07 16:42:23 +00:00
// We only need to check products managing stock, with a limited stock qty.
if ( ! $product -> managing_stock () || $product -> backorders_allowed () ) {
2016-11-02 18:50:42 +00:00
continue ;
}
2012-12-12 21:14:19 +00:00
2018-06-08 15:58:33 +00:00
// Check stock based on all items in the cart and consider any held stock within pending orders.
2020-05-06 16:14:53 +00:00
$held_stock = wc_get_held_stock_quantity ( $product , $current_session_order_id );
2018-06-08 15:58:33 +00:00
$required_stock = $product_qty_in_cart [ $product -> get_stock_managed_by_id () ];
2012-03-14 15:34:45 +00:00
2018-06-08 15:58:33 +00:00
if ( $product -> get_stock_quantity () < ( $held_stock + $required_stock ) ) {
/* translators: 1: product name 2: quantity in stock */
$error -> add ( 'out-of-stock' , sprintf ( __ ( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.' , 'woocommerce' ), $product -> get_name (), wc_format_stock_quantity_for_display ( $product -> get_stock_quantity () - $held_stock , $product ) ) );
2018-06-07 16:42:23 +00:00
return $error ;
2016-09-02 01:03:52 +00:00
}
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
return true ;
}
/**
* Gets and formats a list of cart item data + variations for display on the frontend .
*
2017-08-08 09:51:35 +00:00
* @ param array $cart_item Cart item object .
* @ param bool $flat Should the data be returned flat or in a list .
2016-09-02 01:03:52 +00:00
* @ return string
*/
public function get_item_data ( $cart_item , $flat = false ) {
2017-12-07 14:53:16 +00:00
wc_deprecated_function ( 'WC_Cart::get_item_data' , '3.3' , 'wc_get_formatted_cart_item_data' );
2012-08-06 12:24:59 +00:00
2017-12-07 14:53:16 +00:00
return wc_get_formatted_cart_item_data ( $cart_item , $flat );
2016-09-02 01:03:52 +00:00
}
/**
* Gets cross sells based on the items in the cart .
*
* @ return array cross_sells ( item ids )
*/
public function get_cross_sells () {
$cross_sells = array ();
2017-04-10 14:20:41 +00:00
$in_cart = array ();
2016-09-02 01:03:52 +00:00
if ( ! $this -> is_empty () ) {
foreach ( $this -> get_cart () as $cart_item_key => $values ) {
if ( $values [ 'quantity' ] > 0 ) {
2016-11-16 12:17:00 +00:00
$cross_sells = array_merge ( $values [ 'data' ] -> get_cross_sell_ids (), $cross_sells );
2016-08-31 13:09:56 +00:00
$in_cart [] = $values [ 'product_id' ];
2012-03-20 13:22:35 +00:00
}
}
2011-12-08 12:50:50 +00:00
}
2016-09-02 01:03:52 +00:00
$cross_sells = array_diff ( $cross_sells , $in_cart );
2017-07-11 18:33:51 +00:00
return apply_filters ( 'woocommerce_cart_crosssell_ids' , wp_parse_id_list ( $cross_sells ), $this );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the url to remove an item from the cart .
*
2017-08-08 09:51:35 +00:00
* @ param string $cart_item_key contains the id of the cart item .
2016-09-02 01:03:52 +00:00
* @ return string url to page
*/
public function get_remove_url ( $cart_item_key ) {
2017-12-05 12:00:21 +00:00
wc_deprecated_function ( 'WC_Cart::get_remove_url' , '3.3' , 'wc_get_cart_remove_url' );
return wc_get_cart_remove_url ( $cart_item_key );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the url to re - add an item into the cart .
*
2017-08-08 09:51:35 +00:00
* @ param string $cart_item_key Cart item key to undo .
2016-09-02 01:03:52 +00:00
* @ return string url to page
*/
public function get_undo_url ( $cart_item_key ) {
2017-12-05 12:00:21 +00:00
wc_deprecated_function ( 'WC_Cart::get_undo_url' , '3.3' , 'wc_get_cart_undo_url' );
2014-11-18 16:30:52 +00:00
2017-12-05 12:00:21 +00:00
return wc_get_cart_undo_url ( $cart_item_key );
2016-09-02 01:03:52 +00:00
}
2014-11-18 16:30:52 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get taxes , merged by code , formatted ready for output .
*
* @ return array
*/
public function get_tax_totals () {
2020-06-30 13:34:10 +00:00
$shipping_taxes = $this -> get_shipping_taxes (); // Shipping taxes are rounded differently, so we will subtract from all taxes, then round and then add them back.
$taxes = $this -> get_taxes ();
2016-09-02 01:03:52 +00:00
$tax_totals = array ();
2013-03-27 18:20:56 +00:00
2016-09-02 01:03:52 +00:00
foreach ( $taxes as $key => $tax ) {
$code = WC_Tax :: get_rate_code ( $key );
2013-03-27 18:20:56 +00:00
2018-03-16 18:08:52 +00:00
if ( $code || apply_filters ( 'woocommerce_cart_remove_taxes_zero_rate_id' , 'zero-rated' ) === $key ) {
2016-09-02 01:03:52 +00:00
if ( ! isset ( $tax_totals [ $code ] ) ) {
2018-03-16 18:08:52 +00:00
$tax_totals [ $code ] = new stdClass ();
2016-09-02 01:03:52 +00:00
$tax_totals [ $code ] -> amount = 0 ;
2014-02-17 15:11:50 +00:00
}
2020-06-30 13:34:10 +00:00
$tax_totals [ $code ] -> tax_rate_id = $key ;
$tax_totals [ $code ] -> is_compound = WC_Tax :: is_compound ( $key );
$tax_totals [ $code ] -> label = WC_Tax :: get_rate_label ( $key );
if ( isset ( $shipping_taxes [ $key ] ) ) {
$tax -= $shipping_taxes [ $key ];
$tax = wc_round_tax_total ( $tax );
$tax += round ( $shipping_taxes [ $key ], wc_get_price_decimals () );
unset ( $shipping_taxes [ $key ] );
}
2018-03-16 18:08:52 +00:00
$tax_totals [ $code ] -> amount += wc_round_tax_total ( $tax );
2019-01-11 12:41:06 +00:00
$tax_totals [ $code ] -> formatted_amount = wc_price ( $tax_totals [ $code ] -> amount );
2013-03-27 18:20:56 +00:00
}
2016-09-02 01:03:52 +00:00
}
2013-03-27 18:20:56 +00:00
2016-09-02 01:03:52 +00:00
if ( apply_filters ( 'woocommerce_cart_hide_zero_taxes' , true ) ) {
$amounts = array_filter ( wp_list_pluck ( $tax_totals , 'amount' ) );
$tax_totals = array_intersect_key ( $tax_totals , $amounts );
2013-03-27 18:20:56 +00:00
}
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_tax_totals' , $tax_totals , $this );
}
2014-11-21 13:06:30 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get all tax classes for items in the cart .
2017-08-08 09:51:35 +00:00
*
2016-09-02 01:03:52 +00:00
* @ return array
*/
public function get_cart_item_tax_classes () {
$found_tax_classes = array ();
2014-11-21 13:06:30 +00:00
2016-09-02 01:03:52 +00:00
foreach ( WC () -> cart -> get_cart () as $item ) {
2017-08-03 06:44:02 +00:00
if ( $item [ 'data' ] && ( $item [ 'data' ] -> is_taxable () || $item [ 'data' ] -> is_shipping_taxable () ) ) {
$found_tax_classes [] = $item [ 'data' ] -> get_tax_class ();
}
2014-11-21 13:06:30 +00:00
}
2016-09-02 01:03:52 +00:00
return array_unique ( $found_tax_classes );
}
2017-11-20 19:14:23 +00:00
/**
* Get all tax classes for shipping based on the items in the cart .
*
* @ return array
*/
public function get_cart_item_tax_classes_for_shipping () {
$found_tax_classes = array ();
foreach ( WC () -> cart -> get_cart () as $item ) {
if ( $item [ 'data' ] && ( $item [ 'data' ] -> is_shipping_taxable () ) ) {
$found_tax_classes [] = $item [ 'data' ] -> get_tax_class ();
}
}
return array_unique ( $found_tax_classes );
}
2016-09-02 01:03:52 +00:00
/**
* Determines the value that the customer spent and the subtotal
* displayed , used for things like coupon validation .
*
* Since the coupon lines are displayed based on the TAX DISPLAY value
* of cart , this is used to determine the spend .
*
* If cart totals are shown including tax , use the subtotal .
* If cart totals are shown excluding tax , use the subtotal ex tax
* ( tax is shown after coupons ) .
*
* @ since 2.6 . 0
* @ return string
*/
public function get_displayed_subtotal () {
2017-12-15 13:39:35 +00:00
return $this -> display_prices_including_tax () ? $this -> get_subtotal () + $this -> get_subtotal_tax () : $this -> get_subtotal ();
2016-09-02 01:03:52 +00:00
}
2016-04-18 10:39:00 +00:00
2016-09-02 01:03:52 +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 .
*
2017-08-08 09:51:35 +00:00
* @ param mixed $cart_id id of product to find in the cart .
2016-09-02 01:03:52 +00:00
* @ return string cart item key
*/
public function find_product_in_cart ( $cart_id = false ) {
2016-09-07 22:32:24 +00:00
if ( false !== $cart_id ) {
2016-09-02 01:03:52 +00:00
if ( is_array ( $this -> cart_contents ) && isset ( $this -> cart_contents [ $cart_id ] ) ) {
return $cart_id ;
2014-09-14 19:44:56 +00:00
}
2013-12-02 13:42:39 +00:00
}
2016-09-02 01:03:52 +00:00
return '' ;
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Generate a unique ID for the cart item being added .
*
2017-08-08 09:51:35 +00:00
* @ 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 .
2016-09-02 01:03:52 +00:00
* @ 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-09-14 19:44:56 +00:00
2017-08-08 09:51:35 +00:00
if ( $variation_id && 0 !== $variation_id ) {
2016-09-02 01:03:52 +00:00
$id_parts [] = $variation_id ;
}
2014-09-14 19:44:56 +00:00
2016-09-02 01:03:52 +00:00
if ( is_array ( $variation ) && ! empty ( $variation ) ) {
$variation_key = '' ;
foreach ( $variation as $key => $value ) {
$variation_key .= trim ( $key ) . trim ( $value );
2014-09-14 19:44:56 +00:00
}
2016-09-02 01:03:52 +00:00
$id_parts [] = $variation_key ;
}
2014-09-14 19:44:56 +00:00
2016-09-02 01:03:52 +00:00
if ( is_array ( $cart_item_data ) && ! empty ( $cart_item_data ) ) {
$cart_item_data_key = '' ;
foreach ( $cart_item_data as $key => $value ) {
if ( is_array ( $value ) || is_object ( $value ) ) {
$value = http_build_query ( $value );
2014-09-14 19:44:56 +00:00
}
2016-09-02 01:03:52 +00:00
$cart_item_data_key .= trim ( $key ) . trim ( $value );
2014-09-14 19:44:56 +00:00
2016-09-02 01:03:52 +00:00
}
$id_parts [] = $cart_item_data_key ;
2014-09-14 19:44:56 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_id' , md5 ( implode ( '_' , $id_parts ) ), $product_id , $variation_id , $variation , $cart_item_data );
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Add a product to the cart .
*
2017-08-18 10:29:26 +00:00
* @ throws Exception Plugins can throw an exception to prevent adding to cart .
2017-08-08 09:51:35 +00:00
* @ param int $product_id contains the id of the product to add to the cart .
* @ param int $quantity contains the quantity of the item to add .
* @ param int $variation_id ID of the variation being added to the cart .
* @ param array $variation attribute values .
* @ param array $cart_item_data extra cart item data we want to pass into the item .
2016-09-02 01:03:52 +00:00
* @ return string | bool $cart_item_key
*/
public function add_to_cart ( $product_id = 0 , $quantity = 1 , $variation_id = 0 , $variation = array (), $cart_item_data = array () ) {
try {
$product_id = absint ( $product_id );
$variation_id = absint ( $variation_id );
2012-08-06 12:24:59 +00:00
2017-08-08 09:51:35 +00:00
// Ensure we don't add a variation to the cart directly by variation ID.
2016-11-02 18:50:42 +00:00
if ( 'product_variation' === get_post_type ( $product_id ) ) {
2016-09-02 01:03:52 +00:00
$variation_id = $product_id ;
$product_id = wp_get_post_parent_id ( $variation_id );
}
2014-07-19 04:08:02 +00:00
2016-09-02 01:03:52 +00:00
$product_data = wc_get_product ( $variation_id ? $variation_id : $product_id );
2017-06-02 04:07:06 +00:00
$quantity = apply_filters ( 'woocommerce_add_to_cart_quantity' , $quantity , $product_id );
2016-11-16 12:17:00 +00:00
if ( $quantity <= 0 || ! $product_data || 'trash' === $product_data -> get_status () ) {
2016-09-02 01:03:52 +00:00
return false ;
}
2014-10-24 22:12:55 +00:00
2017-08-08 09:51:35 +00:00
// Load cart item data - may be added by other plugins.
2017-11-21 15:01:23 +00:00
$cart_item_data = ( array ) apply_filters ( 'woocommerce_add_cart_item_data' , $cart_item_data , $product_id , $variation_id , $quantity );
2013-01-22 14:30:15 +00:00
2017-08-08 09:51:35 +00:00
// Generate a ID based on product ID, variation ID, variation data, and other cart item data.
2018-03-16 18:08:52 +00:00
$cart_id = $this -> generate_cart_id ( $product_id , $variation_id , $variation , $cart_item_data );
2012-08-06 12:24:59 +00:00
2017-08-08 09:51:35 +00:00
// Find the cart item key in the existing cart.
2018-03-16 18:08:52 +00:00
$cart_item_key = $this -> find_product_in_cart ( $cart_id );
2012-08-06 12:24:59 +00:00
2017-08-08 09:51:35 +00:00
// Force quantity to 1 if sold individually and check for existing item in cart.
2016-09-02 01:03:52 +00:00
if ( $product_data -> is_sold_individually () ) {
2017-03-24 08:41:27 +00:00
$quantity = apply_filters ( 'woocommerce_add_to_cart_sold_individually_quantity' , 1 , $quantity , $product_id , $variation_id , $cart_item_data );
$found_in_cart = apply_filters ( 'woocommerce_add_to_cart_sold_individually_found_in_cart' , $cart_item_key && $this -> cart_contents [ $cart_item_key ][ 'quantity' ] > 0 , $product_id , $variation_id , $cart_item_data , $cart_id );
2012-11-27 16:22:47 +00:00
2017-03-24 08:41:27 +00:00
if ( $found_in_cart ) {
2016-10-29 17:32:38 +00:00
/* translators: %s: product name */
2020-05-09 11:03:43 +00:00
$message = sprintf ( __ ( 'You cannot add another "%s" to your cart.' , 'woocommerce' ), $product_data -> get_name () );
/**
* Filters message about more than 1 product being added to cart .
*
* @ param string $message Message .
* @ param WC_Product $product_data Product data .
*/
$message = apply_filters ( 'woocommerce_cart_product_cannot_add_another_message' , $message , $product_data );
throw new Exception ( sprintf ( '<a href="%s" class="button wc-forward">%s</a> %s' , wc_get_cart_url (), __ ( 'View cart' , 'woocommerce' ), $message ) );
2015-01-27 16:02:57 +00:00
}
2016-09-02 01:03:52 +00:00
}
2012-11-27 16:22:47 +00:00
2016-09-02 01:03:52 +00:00
if ( ! $product_data -> is_purchasable () ) {
2019-07-19 21:37:10 +00:00
$message = __ ( 'Sorry, this product cannot be purchased.' , 'woocommerce' );
2019-07-24 22:54:51 +00:00
/**
* Filters message about product unable to be purchased .
*
* @ since 3.8 . 0
* @ param string $message Message .
* @ param WC_Product $product_data Product data .
*/
$message = apply_filters ( 'woocommerce_cart_product_cannot_be_purchased_message' , $message , $product_data );
2019-07-19 21:37:10 +00:00
throw new Exception ( $message );
2016-09-02 01:03:52 +00:00
}
2012-11-27 16:22:47 +00:00
2017-08-08 09:51:35 +00:00
// Stock check - only check if we're managing stock and backorders are not allowed.
2016-09-02 01:03:52 +00:00
if ( ! $product_data -> is_in_stock () ) {
2018-03-16 18:08:52 +00:00
/* translators: %s: product name */
2020-05-09 11:03:43 +00:00
$message = sprintf ( __ ( 'You cannot add "%s" to the cart because the product is out of stock.' , 'woocommerce' ), $product_data -> get_name () );
/**
* Filters message about product being out of stock .
*
* @ param string $message Message .
* @ param WC_Product $product_data Product data .
*/
$message = apply_filters ( 'woocommerce_cart_product_out_of_stock_message' , $message , $product_data );
throw new Exception ( $message );
2016-09-02 01:03:52 +00:00
}
2012-11-27 16:22:47 +00:00
2016-09-02 01:03:52 +00:00
if ( ! $product_data -> has_enough_stock ( $quantity ) ) {
2020-05-09 11:03:43 +00:00
$stock_quantity = $product_data -> get_stock_quantity ();
2016-10-29 17:32:38 +00:00
/* translators: 1: product name 2: quantity in stock */
2020-05-09 11:03:43 +00:00
$message = sprintf ( __ ( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).' , 'woocommerce' ), $product_data -> get_name (), wc_format_stock_quantity_for_display ( $stock_quantity , $product_data ) );
/**
* Filters message about product not having enough stock .
*
* @ param string $message Message .
* @ param WC_Product $product_data Product data .
* @ param int $stock_quantity Quantity remaining .
*/
$message = apply_filters ( 'woocommerce_cart_product_not_enough_stock_message' , $message , $product_data , $stock_quantity );
throw new Exception ( $message );
2016-09-02 01:03:52 +00:00
}
2012-06-10 12:53:26 +00:00
2017-08-08 09:51:35 +00:00
// Stock check - this time accounting for whats already in-cart.
2016-11-09 12:26:46 +00:00
if ( $product_data -> managing_stock () ) {
2016-11-02 18:50:42 +00:00
$products_qty_in_cart = $this -> get_cart_item_quantities ();
2016-11-23 01:47:13 +00:00
if ( isset ( $products_qty_in_cart [ $product_data -> get_stock_managed_by_id () ] ) && ! $product_data -> has_enough_stock ( $products_qty_in_cart [ $product_data -> get_stock_managed_by_id () ] + $quantity ) ) {
2018-03-16 18:08:52 +00:00
throw new Exception (
sprintf (
'<a href="%s" class="button wc-forward">%s</a> %s' ,
wc_get_cart_url (),
2018-08-15 07:13:59 +00:00
__ ( 'View cart' , 'woocommerce' ),
2018-03-16 18:08:52 +00:00
/* translators: 1: quantity in stock 2: current quantity */
sprintf ( __ ( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.' , 'woocommerce' ), wc_format_stock_quantity_for_display ( $product_data -> get_stock_quantity (), $product_data ), wc_format_stock_quantity_for_display ( $products_qty_in_cart [ $product_data -> get_stock_managed_by_id () ], $product_data ) )
)
);
2015-01-27 16:02:57 +00:00
}
2014-09-14 19:44:56 +00:00
}
2011-08-09 15:16:18 +00:00
2017-08-08 09:51:35 +00:00
// If cart_item_key is set, the item is already in the cart.
2016-09-02 01:03:52 +00:00
if ( $cart_item_key ) {
$new_quantity = $quantity + $this -> cart_contents [ $cart_item_key ][ 'quantity' ];
$this -> set_quantity ( $cart_item_key , $new_quantity , false );
} else {
$cart_item_key = $cart_id ;
2015-05-08 17:25:46 +00:00
2017-08-08 09:51:35 +00:00
// Add item after merging with $cart_item_data - hook to allow plugins to modify cart item.
2018-03-16 18:08:52 +00:00
$this -> cart_contents [ $cart_item_key ] = apply_filters (
2018-12-06 18:31:21 +00:00
'woocommerce_add_cart_item' ,
array_merge (
$cart_item_data ,
array (
2018-03-16 18:08:52 +00:00
'key' => $cart_item_key ,
'product_id' => $product_id ,
'variation_id' => $variation_id ,
'variation' => $variation ,
'quantity' => $quantity ,
'data' => $product_data ,
'data_hash' => wc_get_cart_item_data_hash ( $product_data ),
)
2018-12-06 18:31:21 +00:00
),
$cart_item_key
2018-03-16 18:08:52 +00:00
);
2016-09-02 01:03:52 +00:00
}
2015-01-07 18:52:17 +00:00
2018-08-14 16:34:52 +00:00
$this -> cart_contents = apply_filters ( 'woocommerce_cart_contents_changed' , $this -> cart_contents );
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_add_to_cart' , $cart_item_key , $product_id , $quantity , $variation_id , $variation , $cart_item_data );
2015-02-02 14:01:43 +00:00
2016-09-02 01:03:52 +00:00
return $cart_item_key ;
2015-01-07 18:52:17 +00:00
2016-09-02 01:03:52 +00:00
} catch ( Exception $e ) {
if ( $e -> getMessage () ) {
wc_add_notice ( $e -> getMessage (), 'error' );
2015-01-07 18:52:17 +00:00
}
return false ;
}
2016-09-02 01:03:52 +00:00
}
2015-01-07 18:52:17 +00:00
2016-09-02 01:03:52 +00:00
/**
* Remove a cart item .
*
* @ since 2.3 . 0
2017-08-08 09:51:35 +00:00
* @ param string $cart_item_key Cart item key to remove from the cart .
2016-09-02 01:03:52 +00:00
* @ return bool
*/
public function remove_cart_item ( $cart_item_key ) {
if ( isset ( $this -> cart_contents [ $cart_item_key ] ) ) {
$this -> removed_cart_contents [ $cart_item_key ] = $this -> cart_contents [ $cart_item_key ];
2017-08-18 10:29:26 +00:00
2016-09-02 01:03:52 +00:00
unset ( $this -> removed_cart_contents [ $cart_item_key ][ 'data' ] );
2015-01-07 18:52:17 +00:00
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_remove_cart_item' , $cart_item_key , $this );
2015-01-07 18:52:17 +00:00
2016-09-02 01:03:52 +00:00
unset ( $this -> cart_contents [ $cart_item_key ] );
2015-02-02 14:01:43 +00:00
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_cart_item_removed' , $cart_item_key , $this );
2015-01-07 18:52:17 +00:00
2016-09-02 01:03:52 +00:00
return true ;
2015-01-07 18:52:17 +00:00
}
2016-09-02 01:03:52 +00:00
return false ;
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Restore a cart item .
*
2017-08-08 09:51:35 +00:00
* @ param string $cart_item_key Cart item key to restore to the cart .
2016-09-02 01:03:52 +00:00
* @ return bool
*/
public function restore_cart_item ( $cart_item_key ) {
if ( isset ( $this -> removed_cart_contents [ $cart_item_key ] ) ) {
2017-08-18 10:29:26 +00:00
$restore_item = $this -> removed_cart_contents [ $cart_item_key ];
$this -> cart_contents [ $cart_item_key ] = $restore_item ;
$this -> cart_contents [ $cart_item_key ][ 'data' ] = wc_get_product ( $restore_item [ 'variation_id' ] ? $restore_item [ 'variation_id' ] : $restore_item [ 'product_id' ] );
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_restore_cart_item' , $cart_item_key , $this );
unset ( $this -> removed_cart_contents [ $cart_item_key ] );
do_action ( 'woocommerce_cart_item_restored' , $cart_item_key , $this );
2014-10-24 14:02:31 +00:00
return true ;
2011-12-08 12:50:50 +00:00
}
2016-09-02 01:03:52 +00:00
return false ;
}
2011-12-08 12:50:50 +00:00
2016-09-02 01:03:52 +00:00
/**
2019-03-22 13:04:47 +00:00
* Set the quantity for an item in the cart using it ' s key .
2016-09-02 01:03:52 +00:00
*
2018-03-16 18:08:52 +00:00
* @ param string $cart_item_key contains the id of the cart item .
2017-08-08 09:51:35 +00:00
* @ param int $quantity contains the quantity of the item .
2019-03-22 13:04:47 +00:00
* @ param bool $refresh_totals whether or not to calculate totals after setting the new qty . Can be used to defer calculations if setting quantities in bulk .
2016-09-02 01:03:52 +00:00
* @ return bool
*/
public function set_quantity ( $cart_item_key , $quantity = 1 , $refresh_totals = true ) {
2017-08-08 09:51:35 +00:00
if ( 0 === $quantity || $quantity < 0 ) {
2019-07-17 02:31:17 +00:00
wc_do_deprecated_action ( 'woocommerce_before_cart_item_quantity_zero' , array ( $cart_item_key , $this ), '3.7.0' , 'woocommerce_remove_cart_item' );
2019-03-22 13:04:47 +00:00
// If we're setting qty to 0 we're removing the item from the cart.
return $this -> remove_cart_item ( $cart_item_key );
2013-02-15 10:27:09 +00:00
}
2019-03-22 13:04:47 +00:00
// Update qty.
$old_quantity = $this -> cart_contents [ $cart_item_key ][ 'quantity' ];
$this -> cart_contents [ $cart_item_key ][ 'quantity' ] = $quantity ;
do_action ( 'woocommerce_after_cart_item_quantity_update' , $cart_item_key , $quantity , $old_quantity , $this );
2016-09-02 01:03:52 +00:00
if ( $refresh_totals ) {
$this -> calculate_totals ();
2015-09-01 12:16:55 +00:00
}
2019-03-22 13:04:47 +00:00
/**
* Fired after qty has been changed .
*
* @ since 3.6 . 0
* @ param string $cart_item_key contains the id of the cart item . This may be empty if the cart item does not exist any more .
* @ param int $quantity contains the quantity of the item .
* @ param WC_Cart $this Cart class .
*/
do_action ( 'woocommerce_cart_item_set_quantity' , $cart_item_key , $quantity , $this );
2016-09-02 01:03:52 +00:00
return true ;
}
2012-08-06 12:24:59 +00:00
2017-07-28 12:02:39 +00:00
/**
* Get cart ' s owner .
*
* @ since 3.2 . 0
* @ return WC_Customer
*/
public function get_customer () {
return WC () -> customer ;
}
2016-09-02 01:03:52 +00:00
/**
* Calculate totals for the items in the cart .
2017-08-08 09:51:35 +00:00
*
* @ uses WC_Cart_Totals
2016-09-02 01:03:52 +00:00
*/
public function calculate_totals () {
2017-08-18 10:29:26 +00:00
$this -> reset_totals ();
2013-06-13 16:01:36 +00:00
2016-09-02 01:03:52 +00:00
if ( $this -> is_empty () ) {
2017-08-18 15:37:24 +00:00
$this -> session -> set_session ();
2016-09-02 01:03:52 +00:00
return ;
}
2013-10-24 12:15:42 +00:00
2017-08-18 10:29:26 +00:00
do_action ( 'woocommerce_before_calculate_totals' , $this );
2017-07-31 18:48:34 +00:00
new WC_Cart_Totals ( $this );
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_after_calculate_totals' , $this );
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Looks at the totals to see if payment is actually required .
*
* @ return bool
*/
public function needs_payment () {
2017-08-18 11:51:45 +00:00
return apply_filters ( 'woocommerce_cart_needs_payment' , 0 < $this -> get_total ( 'edit' ), $this );
2016-09-02 01:03:52 +00:00
}
/*
* Shipping related functions .
*/
/**
* Uses the shipping class to calculate shipping then gets the totals when its finished .
*/
public function calculate_shipping () {
2018-10-02 15:03:17 +00:00
$this -> shipping_methods = $this -> needs_shipping () ? $this -> get_chosen_shipping_methods ( WC () -> shipping () -> calculate_shipping ( $this -> get_shipping_packages () ) ) : array ();
2016-09-02 01:03:52 +00:00
2017-08-18 12:48:53 +00:00
$shipping_taxes = wp_list_pluck ( $this -> shipping_methods , 'taxes' );
2018-03-16 18:08:52 +00:00
$merged_taxes = array ();
2017-08-18 14:05:01 +00:00
foreach ( $shipping_taxes as $taxes ) {
foreach ( $taxes as $tax_id => $tax_amount ) {
if ( ! isset ( $merged_taxes [ $tax_id ] ) ) {
$merged_taxes [ $tax_id ] = 0 ;
}
$merged_taxes [ $tax_id ] += $tax_amount ;
}
2017-08-18 12:48:53 +00:00
}
2017-08-18 11:51:45 +00:00
$this -> set_shipping_total ( array_sum ( wp_list_pluck ( $this -> shipping_methods , 'cost' ) ) );
2017-09-21 23:48:32 +00:00
$this -> set_shipping_tax ( array_sum ( $merged_taxes ) );
2017-08-18 12:48:53 +00:00
$this -> set_shipping_taxes ( $merged_taxes );
2017-07-25 14:11:32 +00:00
return $this -> shipping_methods ;
}
/**
* Given a set of packages with rates , get the chosen ones only .
*
* @ since 3.2 . 0
2017-08-08 09:51:35 +00:00
* @ param array $calculated_shipping_packages Array of packages .
2017-07-25 14:11:32 +00:00
* @ return array
*/
protected function get_chosen_shipping_methods ( $calculated_shipping_packages = array () ) {
$chosen_methods = array ();
// Get chosen methods for each package to get our totals.
foreach ( $calculated_shipping_packages as $key => $package ) {
2018-03-16 18:08:52 +00:00
$chosen_method = wc_get_chosen_shipping_method_for_package ( $key , $package );
2017-07-25 14:11:32 +00:00
if ( $chosen_method ) {
$chosen_methods [ $key ] = $package [ 'rates' ][ $chosen_method ];
}
}
return $chosen_methods ;
2016-09-02 01:03:52 +00:00
}
2016-12-19 16:17:50 +00:00
/**
* Filter items needing shipping callback .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-08-08 09:51:35 +00:00
* @ param array $item Item to check for shipping .
2016-12-19 16:17:50 +00:00
* @ return bool
*/
protected function filter_items_needing_shipping ( $item ) {
$product = $item [ 'data' ];
return $product && $product -> needs_shipping ();
}
/**
* Get only items that need shipping .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-12-19 16:17:50 +00:00
* @ return array
*/
protected function get_items_needing_shipping () {
return array_filter ( $this -> get_cart (), array ( $this , 'filter_items_needing_shipping' ) );
}
2016-09-02 01:03:52 +00:00
/**
* Get packages to calculate shipping for .
*
* This lets us calculate costs for carts that are shipped to multiple locations .
*
* Shipping methods are responsible for looping through these packages .
*
* By default we pass the cart itself as a package - plugins can change this .
* through the filter and break it up .
*
* @ since 1.5 . 4
* @ return array of cart items
*/
public function get_shipping_packages () {
2018-03-16 18:08:52 +00:00
return apply_filters (
'woocommerce_cart_shipping_packages' ,
2016-12-19 16:17:50 +00:00
array (
array (
'contents' => $this -> get_items_needing_shipping (),
'contents_cost' => array_sum ( wp_list_pluck ( $this -> get_items_needing_shipping (), 'line_total' ) ),
2017-08-18 11:51:45 +00:00
'applied_coupons' => $this -> get_applied_coupons (),
2016-12-19 16:17:50 +00:00
'user' => array (
'ID' => get_current_user_id (),
),
'destination' => array (
2017-07-28 12:02:39 +00:00
'country' => $this -> get_customer () -> get_shipping_country (),
'state' => $this -> get_customer () -> get_shipping_state (),
'postcode' => $this -> get_customer () -> get_shipping_postcode (),
'city' => $this -> get_customer () -> get_shipping_city (),
'address' => $this -> get_customer () -> get_shipping_address (),
2018-10-03 18:31:35 +00:00
'address_1' => $this -> get_customer () -> get_shipping_address (), // Provide both address and address_1 for backwards compatibility.
2017-07-28 12:02:39 +00:00
'address_2' => $this -> get_customer () -> get_shipping_address_2 (),
2016-12-19 16:17:50 +00:00
),
2018-03-16 18:08:52 +00:00
'cart_subtotal' => $this -> get_displayed_subtotal (),
2016-12-19 16:17:50 +00:00
),
)
);
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Looks through the cart to see if shipping is actually required .
*
* @ return bool whether or not the cart needs shipping
*/
public function needs_shipping () {
if ( ! wc_shipping_enabled () || 0 === wc_get_shipping_method_count ( true ) ) {
return false ;
2012-04-11 12:08:04 +00:00
}
2016-09-02 01:03:52 +00:00
$needs_shipping = false ;
2017-08-18 11:51:45 +00:00
foreach ( $this -> get_cart_contents () as $cart_item_key => $values ) {
if ( $values [ 'data' ] -> needs_shipping () ) {
$needs_shipping = true ;
break ;
2016-09-02 01:03:52 +00:00
}
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_needs_shipping' , $needs_shipping );
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Should the shipping address form be shown .
*
* @ return bool
*/
public function needs_shipping_address () {
2017-08-22 09:53:34 +00:00
return apply_filters ( 'woocommerce_cart_needs_shipping_address' , true === $this -> needs_shipping () && ! wc_ship_to_billing_address_only () );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Sees if the customer has entered enough data to calc the shipping yet .
*
* @ return bool
*/
public function show_shipping () {
2017-08-18 11:51:45 +00:00
if ( ! wc_shipping_enabled () || ! $this -> get_cart_contents () ) {
2016-09-02 01:03:52 +00:00
return false ;
2017-03-07 20:24:24 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
if ( 'yes' === get_option ( 'woocommerce_shipping_cost_requires_address' ) ) {
2017-07-28 12:02:39 +00:00
if ( ! $this -> get_customer () -> has_calculated_shipping () ) {
if ( ! $this -> get_customer () -> get_shipping_country () || ( ! $this -> get_customer () -> get_shipping_state () && ! $this -> get_customer () -> get_shipping_postcode () ) ) {
2016-09-02 01:03:52 +00:00
return false ;
}
}
}
2012-12-03 16:36:54 +00:00
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_ready_to_calc_shipping' , true );
}
2012-12-03 16:36:54 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the shipping total ( after calculation ) .
*
* @ return string price or string for the shipping total
*/
public function get_cart_shipping_total () {
2018-06-25 11:24:07 +00:00
// Default total assumes Free shipping.
2018-06-25 09:17:08 +00:00
$total = __ ( 'Free!' , 'woocommerce' );
2018-06-25 11:24:07 +00:00
2017-08-18 11:51:45 +00:00
if ( 0 < $this -> get_shipping_total () ) {
2016-09-02 01:03:52 +00:00
2017-12-15 13:39:35 +00:00
if ( $this -> display_prices_including_tax () ) {
2018-06-25 09:17:08 +00:00
$total = wc_price ( $this -> shipping_total + $this -> shipping_tax_total );
2016-09-02 01:03:52 +00:00
2017-12-15 13:39:35 +00:00
if ( $this -> shipping_tax_total > 0 && ! wc_prices_include_tax () ) {
2018-06-25 09:17:08 +00:00
$total .= ' <small class="tax_label">' . WC () -> countries -> inc_tax_or_vat () . '</small>' ;
2017-08-18 11:51:45 +00:00
}
} else {
2018-06-25 09:17:08 +00:00
$total = wc_price ( $this -> shipping_total );
2016-09-02 01:03:52 +00:00
2017-12-15 13:39:35 +00:00
if ( $this -> shipping_tax_total > 0 && wc_prices_include_tax () ) {
2018-06-25 09:17:08 +00:00
$total .= ' <small class="tax_label">' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
}
2011-12-08 12:50:50 +00:00
}
2018-06-25 09:17:08 +00:00
return apply_filters ( 'woocommerce_cart_shipping_total' , $total , $this );
2016-09-02 01:03:52 +00:00
}
2011-12-08 12:50:50 +00:00
2016-09-02 01:03:52 +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 ) .
*
2017-08-08 09:51:35 +00:00
* @ param array $posted Post data .
2016-09-02 01:03:52 +00:00
*/
public function check_customer_coupons ( $posted ) {
2017-08-18 11:51:45 +00:00
foreach ( $this -> get_applied_coupons () as $code ) {
$coupon = new WC_Coupon ( $code );
2013-10-18 17:10:55 +00:00
2017-08-18 11:51:45 +00:00
if ( $coupon -> is_valid () ) {
2013-10-18 17:10:55 +00:00
2017-08-18 11:51:45 +00:00
// Get user and posted emails to compare.
2019-01-11 12:28:52 +00:00
$current_user = wp_get_current_user ();
2018-05-23 14:04:03 +00:00
$billing_email = isset ( $posted [ 'billing_email' ] ) ? $posted [ 'billing_email' ] : '' ;
$check_emails = array_unique (
2018-03-16 18:08:52 +00:00
array_filter (
array_map (
2018-12-06 18:31:21 +00:00
'strtolower' ,
array_map (
'sanitize_email' ,
array (
2018-05-23 14:04:03 +00:00
$billing_email ,
2018-03-16 18:08:52 +00:00
$current_user -> user_email ,
)
)
)
)
);
2016-09-02 01:03:52 +00:00
2017-08-18 11:51:45 +00:00
// Limit to defined email addresses.
$restrictions = $coupon -> get_email_restrictions ();
2016-09-02 01:03:52 +00:00
2018-03-09 08:57:12 +00:00
if ( is_array ( $restrictions ) && 0 < count ( $restrictions ) && ! $this -> is_coupon_emails_allowed ( $check_emails , $restrictions ) ) {
2017-08-18 11:51:45 +00:00
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_NOT_YOURS_REMOVED );
$this -> remove_coupon ( $code );
}
2020-04-02 15:00:06 +00:00
$coupon_usage_limit = $coupon -> get_usage_limit_per_user ();
if ( 0 < $coupon_usage_limit && 0 === get_current_user_id () ) {
// For guest, usage per user has not been enforced yet. Enforce it now.
$coupon_data_store = $coupon -> get_data_store ();
$billing_email = strtolower ( sanitize_email ( $billing_email ) );
if ( $coupon_data_store && $coupon_data_store -> get_usage_by_email ( $coupon , $billing_email ) >= $coupon_usage_limit ) {
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_USAGE_LIMIT_REACHED );
}
}
2013-10-18 17:10:55 +00:00
}
}
2016-09-02 01:03:52 +00:00
}
2013-10-18 17:10:55 +00:00
2018-03-09 08:57:12 +00:00
/**
* Checks if the given email address ( es ) matches the ones specified on the coupon .
*
* @ param array $check_emails Array of customer email addresses .
* @ param array $restrictions Array of allowed email addresses .
* @ return bool
*/
2018-03-13 05:34:47 +00:00
public function is_coupon_emails_allowed ( $check_emails , $restrictions ) {
2018-03-09 08:57:12 +00:00
foreach ( $check_emails as $check_email ) {
// With a direct match we return true.
2018-03-16 18:08:52 +00:00
if ( in_array ( $check_email , $restrictions , true ) ) {
2018-03-09 08:57:12 +00:00
return true ;
}
// Go through the allowed emails and return true if the email matches a wildcard.
foreach ( $restrictions as $restriction ) {
// Convert to PHP-regex syntax.
2018-12-06 18:32:04 +00:00
$regex = '/^' . str_replace ( '*' , '(.+)?' , $restriction ) . '$/' ;
2018-03-09 08:57:12 +00:00
preg_match ( $regex , $check_email , $match );
if ( ! empty ( $match ) ) {
return true ;
}
}
}
// No matches, this one isn't allowed.
return false ;
}
2016-09-02 01:03:52 +00:00
/**
* Returns whether or not a discount has been applied .
2017-08-08 09:51:35 +00:00
*
* @ param string $coupon_code Coupon code to check .
2016-09-02 01:03:52 +00:00
* @ return bool
*/
public function has_discount ( $coupon_code = '' ) {
2017-08-08 09:51:35 +00:00
return $coupon_code ? in_array ( wc_format_coupon_code ( $coupon_code ), $this -> applied_coupons , true ) : count ( $this -> applied_coupons ) > 0 ;
2016-09-02 01:03:52 +00:00
}
2011-12-08 12:50:50 +00:00
2016-09-02 01:03:52 +00:00
/**
* Applies a coupon code passed to the method .
*
2017-08-08 09:51:35 +00:00
* @ param string $coupon_code - The code to apply .
2018-03-16 18:08:52 +00:00
* @ return bool True if the coupon is applied , false if it does not exist or cannot be applied .
2016-09-02 01:03:52 +00:00
*/
2017-08-18 10:29:26 +00:00
public function apply_coupon ( $coupon_code ) {
2017-03-07 23:00:14 +00:00
// Coupons are globally disabled.
2016-09-02 01:03:52 +00:00
if ( ! wc_coupons_enabled () ) {
return false ;
}
2012-03-12 09:25:06 +00:00
2017-03-07 23:00:14 +00:00
// Sanitize coupon code.
2017-02-22 17:40:24 +00:00
$coupon_code = wc_format_coupon_code ( $coupon_code );
2013-04-12 08:59:33 +00:00
2017-03-07 23:00:14 +00:00
// Get the coupon.
2016-09-02 01:03:52 +00:00
$the_coupon = new WC_Coupon ( $coupon_code );
2012-08-06 12:24:59 +00:00
2017-06-27 20:53:56 +00:00
// Prevent adding coupons by post ID.
if ( $the_coupon -> get_code () !== $coupon_code ) {
$the_coupon -> set_code ( $coupon_code );
$the_coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_NOT_EXIST );
return false ;
}
2017-03-07 23:00:14 +00:00
// Check it can be used with cart.
2016-09-02 01:03:52 +00:00
if ( ! $the_coupon -> is_valid () ) {
2020-02-26 11:24:04 +00:00
wc_add_notice ( $the_coupon -> get_error_message (), 'error' );
2016-09-02 01:03:52 +00:00
return false ;
}
2012-08-06 12:24:59 +00:00
2017-03-07 23:00:14 +00:00
// Check if applied.
2016-09-02 01:03:52 +00:00
if ( $this -> has_discount ( $coupon_code ) ) {
$the_coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_ALREADY_APPLIED );
return false ;
}
2012-08-06 12:24:59 +00:00
2017-03-07 23:00:14 +00:00
// If its individual use then remove other coupons.
2016-09-02 01:03:52 +00:00
if ( $the_coupon -> get_individual_use () ) {
2017-03-07 19:31:20 +00:00
$coupons_to_keep = apply_filters ( 'woocommerce_apply_individual_use_coupon' , array (), $the_coupon , $this -> applied_coupons );
foreach ( $this -> applied_coupons as $applied_coupon ) {
2017-08-08 09:51:35 +00:00
$keep_key = array_search ( $applied_coupon , $coupons_to_keep , true );
2017-03-07 19:58:02 +00:00
if ( false === $keep_key ) {
2017-03-07 19:31:20 +00:00
$this -> remove_coupon ( $applied_coupon );
2017-03-07 19:58:02 +00:00
} else {
2017-03-07 19:31:20 +00:00
unset ( $coupons_to_keep [ $keep_key ] );
}
}
if ( ! empty ( $coupons_to_keep ) ) {
$this -> applied_coupons += $coupons_to_keep ;
}
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2017-03-07 23:00:14 +00:00
// Check to see if an individual use coupon is set.
2017-03-07 21:17:36 +00:00
if ( $this -> applied_coupons ) {
foreach ( $this -> applied_coupons as $code ) {
$coupon = new WC_Coupon ( $code );
if ( $coupon -> get_individual_use () && false === apply_filters ( 'woocommerce_apply_with_individual_use_coupon' , false , $the_coupon , $coupon , $this -> applied_coupons ) ) {
2017-03-07 23:00:14 +00:00
// Reject new coupon.
2017-03-07 21:17:36 +00:00
$coupon -> add_coupon_message ( WC_Coupon :: E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
return false ;
}
}
}
2016-09-02 01:03:52 +00:00
$this -> applied_coupons [] = $coupon_code ;
2013-08-14 20:00:34 +00:00
2017-03-07 23:00:14 +00:00
// Choose free shipping.
2016-09-02 01:03:52 +00:00
if ( $the_coupon -> get_free_shipping () ) {
2018-10-02 15:03:17 +00:00
$packages = WC () -> shipping () -> get_packages ();
2016-09-02 01:03:52 +00:00
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
foreach ( $packages as $i => $package ) {
$chosen_shipping_methods [ $i ] = 'free_shipping' ;
2015-01-27 12:11:55 +00:00
}
2013-02-18 12:29:10 +00:00
2016-09-02 01:03:52 +00:00
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
$the_coupon -> add_coupon_message ( WC_Coupon :: WC_COUPON_SUCCESS );
2013-08-14 20:00:34 +00:00
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_applied_coupon' , $coupon_code );
2013-10-25 18:28:09 +00:00
2016-09-02 01:03:52 +00:00
return true ;
}
/**
* Get array of applied coupon objects and codes .
2017-05-15 11:50:52 +00:00
*
2017-08-08 09:51:35 +00:00
* @ param null $deprecated No longer used .
2016-09-02 01:03:52 +00:00
* @ return array of applied coupons
*/
public function get_coupons ( $deprecated = null ) {
$coupons = array ();
2013-08-14 20:00:34 +00:00
2016-09-02 01:03:52 +00:00
if ( 'order' === $deprecated ) {
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
2016-09-02 01:03:52 +00:00
foreach ( $this -> get_applied_coupons () as $code ) {
2018-03-16 18:08:52 +00:00
$coupon = new WC_Coupon ( $code );
2016-09-02 01:03:52 +00:00
$coupons [ $code ] = $coupon ;
2013-10-18 17:10:55 +00:00
}
2016-09-02 01:03:52 +00:00
return $coupons ;
}
2015-04-10 09:28:46 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get the discount amount for a used coupon .
2017-08-08 09:51:35 +00:00
*
* @ param string $code coupon code .
* @ param bool $ex_tax inc or ex tax .
2016-09-02 01:03:52 +00:00
* @ return float discount amount
*/
public function get_coupon_discount_amount ( $code , $ex_tax = true ) {
2017-08-18 11:51:45 +00:00
$totals = $this -> get_coupon_discount_totals ();
2018-03-16 18:08:52 +00:00
$discount_amount = isset ( $totals [ $code ] ) ? $totals [ $code ] : 0 ;
2013-10-25 18:28:09 +00:00
2016-09-02 01:03:52 +00:00
if ( ! $ex_tax ) {
$discount_amount += $this -> get_coupon_discount_tax_amount ( $code );
2011-12-08 12:50:50 +00:00
}
2017-08-18 10:37:22 +00:00
return wc_cart_round_discount ( $discount_amount , wc_get_price_decimals () );
2016-09-02 01:03:52 +00:00
}
2013-08-14 20:00:34 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get the discount tax amount for a used coupon ( for tax inclusive prices ) .
2017-08-08 09:51:35 +00:00
*
* @ param string $code coupon code .
2016-09-02 01:03:52 +00:00
* @ return float discount amount
*/
public function get_coupon_discount_tax_amount ( $code ) {
2017-08-18 11:51:45 +00:00
$totals = $this -> get_coupon_discount_tax_totals ();
return wc_cart_round_discount ( isset ( $totals [ $code ] ) ? $totals [ $code ] : 0 , wc_get_price_decimals () );
2016-09-02 01:03:52 +00:00
}
2013-10-28 09:45:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Remove coupons from the cart of a defined type . Type 1 is before tax , type 2 is after tax .
2017-05-15 11:50:52 +00:00
*
2017-08-08 09:51:35 +00:00
* @ param null $deprecated No longer used .
2016-09-02 01:03:52 +00:00
*/
public function remove_coupons ( $deprecated = null ) {
2017-08-18 10:29:26 +00:00
$this -> set_coupon_discount_totals ( array () );
$this -> set_coupon_discount_tax_totals ( array () );
$this -> set_applied_coupons ( array () );
$this -> session -> set_session ();
2016-09-02 01:03:52 +00:00
}
2013-10-18 17:10:55 +00:00
2016-09-02 01:03:52 +00:00
/**
* Remove a single coupon by code .
2017-08-08 09:51:35 +00:00
*
* @ param string $coupon_code Code of the coupon to remove .
2016-09-02 01:03:52 +00:00
* @ return bool
*/
public function remove_coupon ( $coupon_code ) {
2018-03-16 18:08:52 +00:00
$coupon_code = wc_format_coupon_code ( $coupon_code );
2020-07-27 19:48:15 +00:00
$position = array_search ( $coupon_code , array_map ( 'wc_format_coupon_code' , $this -> get_applied_coupons () ), true );
2015-06-19 12:50:56 +00:00
2016-09-07 22:32:24 +00:00
if ( false !== $position ) {
2016-09-02 01:03:52 +00:00
unset ( $this -> applied_coupons [ $position ] );
2013-10-18 17:10:55 +00:00
}
2017-08-14 11:28:19 +00:00
WC () -> session -> set ( 'refresh_totals' , true );
2016-09-02 01:03:52 +00:00
do_action ( 'woocommerce_removed_coupon' , $coupon_code );
return true ;
}
2013-10-18 17:10:55 +00:00
2016-09-02 01:03:52 +00:00
/**
2017-08-22 14:17:58 +00:00
* Trigger an action so 3 rd parties can add custom fees .
2016-09-02 01:03:52 +00:00
*
2017-08-22 14:17:58 +00:00
* @ since 2.0 . 0
*/
public function calculate_fees () {
do_action ( 'woocommerce_cart_calculate_fees' , $this );
}
2017-08-22 15:20:23 +00:00
/**
* Return reference to fees API .
2016-09-02 01:03:52 +00:00
*
2017-08-22 15:20:23 +00:00
* @ since 3.2 . 0
* @ return WC_Cart_Fees
*/
public function fees_api () {
return $this -> fees_api ;
}
2017-08-22 14:17:58 +00:00
/**
* Add additional fee to the cart .
2017-02-24 18:37:51 +00:00
*
2017-07-03 09:26:21 +00:00
* This method should be called on a callback attached to the
* woocommerce_cart_calculate_fees action during cart / checkout . Fees do not
* persist .
*
2017-08-22 14:17:58 +00:00
* @ uses WC_Cart_Fees :: add_fee
2017-02-24 18:37:51 +00:00
* @ param string $name Unique name for the fee . Multiple fees of the same name cannot be added .
* @ param float $amount Fee amount ( do not enter negative amounts ) .
* @ param bool $taxable Is the fee taxable ? ( default : false ) .
* @ param string $tax_class The tax class for the fee if taxable . A blank string is standard tax class . ( default : '' ) .
2016-09-02 01:03:52 +00:00
*/
public function add_fee ( $name , $amount , $taxable = false , $tax_class = '' ) {
2018-03-16 18:08:52 +00:00
$this -> fees_api () -> add_fee (
array (
'name' => $name ,
'amount' => ( float ) $amount ,
'taxable' => $taxable ,
'tax_class' => $tax_class ,
)
);
2016-09-02 01:03:52 +00:00
}
2013-08-13 16:19:20 +00:00
2016-09-02 01:03:52 +00:00
/**
2017-08-22 14:17:58 +00:00
* Return all added fees from the Fees API .
2016-09-02 01:03:52 +00:00
*
2017-08-22 14:17:58 +00:00
* @ uses WC_Cart_Fees :: get_fees
2016-09-02 01:03:52 +00:00
* @ return array
*/
public function get_fees () {
2017-09-20 14:39:05 +00:00
$fees = $this -> fees_api () -> get_fees ();
2017-10-13 12:46:49 +00:00
if ( property_exists ( $this , 'fees' ) ) {
$fees = $fees + ( array ) $this -> fees ;
2017-09-20 14:39:05 +00:00
}
return $fees ;
2016-09-02 01:03:52 +00:00
}
2012-11-09 21:15:15 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the total excluding taxes .
*
* @ return string formatted price
*/
public function get_total_ex_tax () {
2017-11-08 13:54:33 +00:00
return apply_filters ( 'woocommerce_cart_total_ex_tax' , wc_price ( max ( 0 , $this -> get_total ( 'edit' ) - $this -> get_total_tax () ) ) );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the cart contents total ( after calculation ) .
*
* @ return string formatted price
*/
public function get_cart_total () {
2017-08-18 11:51:45 +00:00
return apply_filters ( 'woocommerce_cart_contents_total' , wc_price ( wc_prices_include_tax () ? $this -> get_cart_contents_total () + $this -> get_cart_contents_tax () : $this -> get_cart_contents_total () ) );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the sub total ( after calculation ) .
*
2017-08-08 09:51:35 +00:00
* @ param bool $compound whether to include compound taxes .
2016-09-02 01:03:52 +00:00
* @ return string formatted price
*/
public function get_cart_subtotal ( $compound = false ) {
2017-08-08 09:51:35 +00:00
/**
* If the cart has compound tax , we want to show the subtotal as cart + shipping + non - compound taxes ( after discount ) .
*/
2016-09-02 01:03:52 +00:00
if ( $compound ) {
2017-08-18 11:51:45 +00:00
$cart_subtotal = wc_price ( $this -> get_cart_contents_total () + $this -> get_shipping_total () + $this -> get_taxes_total ( false , false ) );
2012-08-06 12:24:59 +00:00
2017-12-15 13:39:35 +00:00
} elseif ( $this -> display_prices_including_tax () ) {
2017-08-18 11:51:45 +00:00
$cart_subtotal = wc_price ( $this -> get_subtotal () + $this -> get_subtotal_tax () );
2016-09-02 01:03:52 +00:00
2017-08-18 11:51:45 +00:00
if ( $this -> get_subtotal_tax () > 0 && ! wc_prices_include_tax () ) {
2017-08-08 09:51:35 +00:00
$cart_subtotal .= ' <small class="tax_label">' . WC () -> countries -> inc_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
2017-12-15 13:39:35 +00:00
} else {
$cart_subtotal = wc_price ( $this -> get_subtotal () );
if ( $this -> get_subtotal_tax () > 0 && wc_prices_include_tax () ) {
$cart_subtotal .= ' <small class="tax_label">' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
}
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_subtotal' , $cart_subtotal , $compound , $this );
}
2013-06-17 11:21:06 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get the product row price per item .
*
2017-08-08 09:51:35 +00:00
* @ param WC_Product $product Product object .
2016-09-02 01:03:52 +00:00
* @ return string formatted price
*/
2016-11-02 18:50:42 +00:00
public function get_product_price ( $product ) {
2017-12-15 13:39:35 +00:00
if ( $this -> display_prices_including_tax () ) {
2016-11-09 12:26:46 +00:00
$product_price = wc_get_price_including_tax ( $product );
2017-12-15 13:39:35 +00:00
} else {
$product_price = wc_get_price_excluding_tax ( $product );
2013-06-17 11:21:06 +00:00
}
2016-11-02 18:50:42 +00:00
return apply_filters ( 'woocommerce_cart_product_price' , wc_price ( $product_price ), $product );
2016-09-02 01:03:52 +00:00
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get the product row subtotal .
*
* Gets the tax etc to avoid rounding issues .
*
* When on the checkout ( review order ), this will get the subtotal based on the customer ' s tax rate rather than the base rate .
*
2017-08-08 09:51:35 +00:00
* @ param WC_Product $product Product object .
* @ param int $quantity Quantity being purchased .
2016-09-02 01:03:52 +00:00
* @ return string formatted price
*/
2016-11-02 18:50:42 +00:00
public function get_product_subtotal ( $product , $quantity ) {
2017-08-08 09:51:35 +00:00
$price = $product -> get_price ();
2012-08-06 12:24:59 +00:00
2017-08-08 09:51:35 +00:00
if ( $product -> is_taxable () ) {
2012-08-06 12:24:59 +00:00
2017-12-15 13:39:35 +00:00
if ( $this -> display_prices_including_tax () ) {
$row_price = wc_get_price_including_tax ( $product , array ( 'qty' => $quantity ) );
2016-09-02 01:03:52 +00:00
$product_subtotal = wc_price ( $row_price );
2012-08-06 12:24:59 +00:00
2017-12-15 13:39:35 +00:00
if ( ! wc_prices_include_tax () && $this -> get_subtotal_tax () > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC () -> countries -> inc_tax_or_vat () . '</small>' ;
2012-03-20 13:22:35 +00:00
}
} else {
2017-12-15 13:39:35 +00:00
$row_price = wc_get_price_excluding_tax ( $product , array ( 'qty' => $quantity ) );
2013-11-25 13:34:21 +00:00
$product_subtotal = wc_price ( $row_price );
2012-08-06 12:24:59 +00:00
2017-12-15 13:39:35 +00:00
if ( wc_prices_include_tax () && $this -> get_subtotal_tax () > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC () -> countries -> ex_tax_or_vat () . '</small>' ;
2016-09-02 01:03:52 +00:00
}
2012-03-20 13:22:35 +00:00
}
2016-09-02 01:03:52 +00:00
} else {
$row_price = $price * $quantity ;
$product_subtotal = wc_price ( $row_price );
2011-12-30 21:11:18 +00:00
}
2012-08-06 12:24:59 +00:00
2016-11-02 18:50:42 +00:00
return apply_filters ( 'woocommerce_cart_product_subtotal' , $product_subtotal , $product , $quantity , $this );
2016-09-02 01:03:52 +00:00
}
2014-06-11 14:10:03 +00:00
2016-09-02 01:03:52 +00:00
/**
* Gets the cart tax ( after calculation ) .
*
* @ return string formatted price
*/
public function get_cart_tax () {
2017-08-18 11:51:45 +00:00
$cart_total_tax = wc_round_tax_total ( $this -> get_cart_contents_tax () + $this -> get_shipping_tax () + $this -> get_fee_tax () );
2014-06-11 14:10:03 +00:00
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_get_cart_tax' , $cart_total_tax ? wc_price ( $cart_total_tax ) : '' );
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get a tax amount .
2017-08-08 09:51:35 +00:00
*
* @ param string $tax_rate_id ID of the tax rate to get taxes for .
2016-09-02 01:03:52 +00:00
* @ return float amount
*/
public function get_tax_amount ( $tax_rate_id ) {
2017-08-18 11:51:45 +00:00
$taxes = wc_array_merge_recursive_numeric ( $this -> get_cart_contents_taxes (), $this -> get_fee_taxes () );
return isset ( $taxes [ $tax_rate_id ] ) ? $taxes [ $tax_rate_id ] : 0 ;
2016-09-02 01:03:52 +00:00
}
2014-11-25 13:05:03 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get a tax amount .
2017-08-08 09:51:35 +00:00
*
* @ param string $tax_rate_id ID of the tax rate to get taxes for .
2016-09-02 01:03:52 +00:00
* @ return float amount
*/
public function get_shipping_tax_amount ( $tax_rate_id ) {
2017-08-18 11:51:45 +00:00
$taxes = $this -> get_shipping_taxes ();
return isset ( $taxes [ $tax_rate_id ] ) ? $taxes [ $tax_rate_id ] : 0 ;
2016-09-02 01:03:52 +00:00
}
2014-11-25 13:05:03 +00:00
2016-09-02 01:03:52 +00:00
/**
* Get tax row amounts with or without compound taxes includes .
*
2017-08-08 09:51:35 +00:00
* @ param bool $compound True if getting compound taxes .
* @ param bool $display True if getting total to display .
2016-09-02 01:03:52 +00:00
* @ return float price
*/
public function get_taxes_total ( $compound = true , $display = true ) {
$total = 0 ;
2017-08-18 11:51:45 +00:00
$taxes = $this -> get_taxes ();
foreach ( $taxes as $key => $tax ) {
2017-03-07 20:24:24 +00:00
if ( ! $compound && WC_Tax :: is_compound ( $key ) ) {
continue ;
}
2020-06-23 13:03:58 +00:00
$total += $tax ;
2011-12-08 12:50:50 +00:00
}
2016-09-02 01:03:52 +00:00
if ( $display ) {
2019-01-11 12:41:06 +00:00
$total = wc_format_decimal ( $total , wc_get_price_decimals () );
2011-12-08 12:50:50 +00:00
}
2016-09-02 01:03:52 +00:00
return apply_filters ( 'woocommerce_cart_taxes_total' , $total , $compound , $display , $this );
}
2012-08-06 12:24:59 +00:00
2016-09-02 01:03:52 +00:00
/**
2017-08-18 11:51:45 +00:00
* Gets the total discount amount .
2016-09-02 01:03:52 +00:00
*
* @ return mixed formatted price or false if there are none
*/
public function get_total_discount () {
2017-08-18 11:51:45 +00:00
return apply_filters ( 'woocommerce_cart_total_discount' , $this -> get_discount_total () ? wc_price ( $this -> get_discount_total () ) : false , $this );
2016-09-02 01:03:52 +00:00
}
2017-08-18 10:29:26 +00:00
/**
* Reset cart totals to the defaults . Useful before running calculations .
*/
private function reset_totals () {
$this -> totals = $this -> default_totals ;
2017-12-08 11:15:08 +00:00
$this -> fees_api -> remove_all_fees ();
2017-08-18 10:29:26 +00:00
do_action ( 'woocommerce_cart_reset' , $this , false );
}
2018-09-06 15:40:21 +00:00
/**
* Returns 'incl' if tax should be included in cart , otherwise returns 'excl' .
*
* @ return string
*/
2020-07-13 14:21:19 +00:00
public function get_tax_price_display_mode () {
2018-09-06 15:40:21 +00:00
if ( $this -> get_customer () && $this -> get_customer () -> get_is_vat_exempt () ) {
return 'excl' ;
}
return get_option ( 'woocommerce_tax_display_cart' );
}
2019-01-31 15:47:52 +00:00
2018-08-14 16:08:55 +00:00
/**
* Returns the hash based on cart contents .
*
2019-01-31 15:46:26 +00:00
* @ since 3.6 . 0
2018-08-14 16:08:55 +00:00
* @ return string hash for cart content
*/
public function get_cart_hash () {
2019-01-31 15:46:26 +00:00
$cart_session = $this -> session -> get_cart_for_session ();
$hash = $cart_session ? md5 ( wp_json_encode ( $cart_session ) . $this -> get_total ( 'edit' ) ) : '' ;
$hash = apply_filters_deprecated ( 'woocommerce_add_to_cart_hash' , array ( $hash , $cart_session ), '3.6.0' , 'woocommerce_cart_hash' );
2018-08-14 16:08:55 +00:00
2019-01-31 15:46:26 +00:00
return apply_filters ( 'woocommerce_cart_hash' , $hash , $cart_session );
2018-08-14 16:08:55 +00:00
}
2013-10-04 01:48:47 +00:00
}