woocommerce/includes/class-wc-cart.php

2285 lines
78 KiB
PHP
Raw Normal View History

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
* @version 2.0.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. */
public $cart_contents;
2012-08-14 19:42:38 +00:00
/** @var array Contains an array of coupon codes applied to the cart. */
public $applied_coupons;
2012-08-14 19:42:38 +00:00
/** @var array Contains an array of coupon code discounts after they have been applied. */
public $coupon_discount_amounts;
2012-08-15 17:08:42 +00:00
/** @var float The total cost of the cart items. */
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. */
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. */
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. */
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. */
public $total;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Cart subtotal. */
public $subtotal;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Cart subtotal without tax. */
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. */
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. */
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. */
public $shipping_taxes;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Discounts before tax. */
public $discount_cart;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Discounts after tax. */
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. */
public $fee_total;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Shipping cost. */
public $shipping_total;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Shipping tax. */
public $shipping_tax_total;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var float Shipping title/label. */
public $shipping_label;
2012-08-14 19:42:38 +00:00
2012-08-15 17:08:42 +00:00
/** @var WC_Tax */
public $tax;
2012-11-27 16:22:47 +00:00
/** @var array An array of fees. */
public $fees;
2012-08-06 12:24:59 +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
*/
public function __construct() {
2013-06-13 16:01:36 +00:00
$this->tax = new WC_Tax();
$this->prices_include_tax = get_option( 'woocommerce_prices_include_tax' ) == 'yes';
$this->round_at_subtotal = get_option('woocommerce_tax_round_at_subtotal') == 'yes';
$this->tax_display_cart = get_option( 'woocommerce_tax_display_cart' );
$this->dp = (int) get_option( 'woocommerce_price_num_decimals' );
2012-12-03 16:36:54 +00:00
$this->display_totals_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
$this->display_cart_ex_tax = $this->tax_display_cart == 'excl' ? true : false;
add_action( 'init', array( $this, 'init' ), 5 ); // Get cart on init
2011-08-09 15:16:18 +00:00
}
2012-08-06 12:24:59 +00:00
2012-08-14 19:42:38 +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.
2012-08-14 19:42:38 +00:00
*
* @access public
* @return void
*/
public function init() {
$this->get_cart_from_session();
2012-08-06 12:24:59 +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 );
2011-08-09 15:16:18 +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
*/
public function get_cart_from_session() {
2012-02-24 21:05:15 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
// Load the coupons
$this->applied_coupons = ( empty( $woocommerce->session->coupon_codes ) ) ? array() : array_filter( (array) $woocommerce->session->coupon_codes );
$this->coupon_discount_amounts = ( empty( $woocommerce->session->coupon_amounts ) ) ? array() : array_filter( (array) $woocommerce->session->coupon_amounts );
2012-08-06 12:24:59 +00:00
// Load the cart
2012-09-07 17:48:30 +00:00
if ( isset( $woocommerce->session->cart ) && is_array( $woocommerce->session->cart ) ) {
$cart = $woocommerce->session->cart;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
foreach ( $cart as $key => $values ) {
2012-08-06 12:24:59 +00:00
$_product = 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
2011-12-08 12:50:50 +00:00
// Put session data into array. Run through filter so other plugins can load their own session data
2012-09-07 17:48:30 +00:00
$this->cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', array(
2011-12-08 12:50:50 +00:00
'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
2013-04-09 09:38:40 +00:00
if ( empty( $this->cart_contents ) || ! is_array( $this->cart_contents ) )
2011-12-08 12:50:50 +00:00
$this->cart_contents = array();
2012-08-06 12:24:59 +00:00
if ( sizeof( $this->cart_contents ) > 0 )
2013-04-09 09:38:40 +00:00
$this->set_cart_cookies();
2012-08-06 12:24:59 +00:00
else
2013-04-09 09:38:40 +00:00
$this->set_cart_cookies( false );
// Trigger action
do_action( 'woocommerce_cart_loaded_from_session', $this );
2012-08-06 12:24:59 +00:00
// Load totals
2012-09-07 17:48:30 +00:00
$this->cart_contents_total = isset( $woocommerce->session->cart_contents_total ) ? $woocommerce->session->cart_contents_total : 0;
$this->cart_contents_weight = isset( $woocommerce->session->cart_contents_weight ) ? $woocommerce->session->cart_contents_weight : 0;
$this->cart_contents_count = isset( $woocommerce->session->cart_contents_count ) ? $woocommerce->session->cart_contents_count : 0;
$this->cart_contents_tax = isset( $woocommerce->session->cart_contents_tax ) ? $woocommerce->session->cart_contents_tax : 0;
$this->total = isset( $woocommerce->session->total ) ? $woocommerce->session->total : 0;
$this->subtotal = isset( $woocommerce->session->subtotal ) ? $woocommerce->session->subtotal : 0;
$this->subtotal_ex_tax = isset( $woocommerce->session->subtotal_ex_tax ) ? $woocommerce->session->subtotal_ex_tax : 0;
$this->tax_total = isset( $woocommerce->session->tax_total ) ? $woocommerce->session->tax_total : 0;
$this->taxes = isset( $woocommerce->session->taxes ) ? $woocommerce->session->taxes : array();
$this->shipping_taxes = isset( $woocommerce->session->shipping_taxes ) ? $woocommerce->session->shipping_taxes : array();
$this->discount_cart = isset( $woocommerce->session->discount_cart ) ? $woocommerce->session->discount_cart : 0;
$this->discount_total = isset( $woocommerce->session->discount_total ) ? $woocommerce->session->discount_total : 0;
$this->shipping_total = isset( $woocommerce->session->shipping_total ) ? $woocommerce->session->shipping_total : 0;
$this->shipping_tax_total = isset( $woocommerce->session->shipping_tax_total ) ? $woocommerce->session->shipping_tax_total : 0;
$this->shipping_label = isset( $woocommerce->session->shipping_label ) ? $woocommerce->session->shipping_label : '';
2012-08-06 12:24:59 +00:00
2012-03-12 09:26:43 +00:00
// Queue re-calc if subtotal is not set
2012-08-06 12:24:59 +00:00
if ( ! $this->subtotal && sizeof( $this->cart_contents ) > 0 )
$this->calculate_totals();
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-08-14 19:42:38 +00:00
2011-12-08 12:50:50 +00:00
/**
* Sets the php session data for the cart and coupons.
2012-08-14 19:42:38 +00:00
*
* @access public
* @return void
2011-12-08 12:50:50 +00:00
*/
public function set_session() {
2012-09-07 17:48:30 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
// Set cart and coupon session data
2012-03-07 20:12:14 +00:00
$cart_session = array();
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $this->cart_contents ) {
foreach ( $this->cart_contents as $key => $values ) {
2012-08-06 12:24:59 +00:00
2012-09-07 17:48:30 +00:00
$cart_session[ $key ] = $values;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Unset product object
2012-09-07 17:48:30 +00:00
unset( $cart_session[ $key ]['data'] );
2012-03-20 13:22:35 +00:00
}
2012-03-07 20:12:14 +00:00
}
2012-08-06 12:24:59 +00:00
$woocommerce->session->cart = $cart_session;
$woocommerce->session->coupon_codes = $this->applied_coupons;
$woocommerce->session->coupon_amounts = $this->coupon_discount_amounts;
2012-08-06 12:24:59 +00:00
// Store totals to avoid re-calc on page load
$woocommerce->session->cart_contents_total = $this->cart_contents_total;
2012-09-07 17:48:30 +00:00
$woocommerce->session->cart_contents_weight = $this->cart_contents_weight;
$woocommerce->session->cart_contents_count = $this->cart_contents_count;
$woocommerce->session->cart_contents_tax = $this->cart_contents_tax;
$woocommerce->session->total = $this->total;
$woocommerce->session->subtotal = $this->subtotal;
$woocommerce->session->subtotal_ex_tax = $this->subtotal_ex_tax;
$woocommerce->session->tax_total = $this->tax_total;
$woocommerce->session->shipping_taxes = $this->shipping_taxes;
$woocommerce->session->taxes = $this->taxes;
$woocommerce->session->discount_cart = $this->discount_cart;
$woocommerce->session->discount_total = $this->discount_total;
$woocommerce->session->shipping_total = $this->shipping_total;
$woocommerce->session->shipping_tax_total = $this->shipping_tax_total;
$woocommerce->session->shipping_label = $this->shipping_label;
2012-09-07 17:48:30 +00:00
if ( get_current_user_id() )
2012-03-20 13:22:35 +00:00
$this->persistent_cart_update();
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
*/
public function empty_cart( $clear_persistent_cart = true ) {
2012-09-07 17:48:30 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$this->cart_contents = array();
$this->reset();
2012-11-27 16:22:47 +00:00
unset( $woocommerce->session->order_awaiting_payment, $woocommerce->session->coupon_codes, $woocommerce->session->coupon_amounts, $woocommerce->session->cart );
2012-08-06 12:24:59 +00:00
if ( $clear_persistent_cart && get_current_user_id() )
2012-03-20 13:22:35 +00:00
$this->persistent_cart_destroy();
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
}
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
*/
public function persistent_cart_update() {
2012-09-07 17:48:30 +00:00
global $woocommerce;
2012-03-04 12:37:41 +00:00
update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', array(
2012-09-07 17:48:30 +00:00
'cart' => $woocommerce->session->cart,
) );
2012-03-04 12:37:41 +00:00
}
2012-08-06 12:24:59 +00:00
2012-08-14 19:42:38 +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
*/
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
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-11 13:53:24 +00:00
* Coupons enabled function. Filterable.
*
* @access public
* @return void
*/
public function coupons_enabled() {
2013-02-11 13:53:24 +00:00
$coupons_enabled = get_option( 'woocommerce_enable_coupons' ) == 'no' ? false : true;
return apply_filters( 'woocommerce_coupons_enabled', $coupons_enabled );
}
/**
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
*/
public function get_cart_contents_count() {
return apply_filters( 'woocommerce_cart_contents_count', $this->cart_contents_count );
}
2012-08-14 19:42:38 +00:00
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
*/
public function check_cart_items() {
2011-12-08 12:50:50 +00:00
global $woocommerce;
$result = $this->check_cart_item_validity();
if ( is_wp_error( $result ) )
wc_add_error( $result->get_error_message() );
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
if ( is_wp_error( $result ) )
2013-06-11 14:59:54 +00:00
wc_add_error( $result->get_error_message() );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2012-08-14 19:42:38 +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
*/
public function check_cart_coupons() {
global $woocommerce;
2012-08-06 12:24:59 +00:00
if ( ! empty( $this->applied_coupons ) ) {
foreach ( $this->applied_coupons as $key => $code ) {
$coupon = new WC_Coupon( $code );
2012-08-06 12:24:59 +00:00
if ( is_wp_error( $coupon->is_valid() ) ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
2012-08-06 12:24:59 +00:00
// Remove the coupon
2012-09-07 17:48:30 +00:00
unset( $this->applied_coupons[ $key ] );
$woocommerce->session->coupon_codes = $this->applied_coupons;
$woocommerce->session->refresh_totals = true;
}
}
}
}
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
*/
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 ) {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
if ( $values['data']->managing_stock() ) {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
if ( $values['variation_id'] > 0 ) {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
if ( $values['data']->variation_has_stock ) {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +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'];
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
// Variation has no stock levels defined so use parents
$quantities[ $values['product_id'] ] = isset( $quantities[ $values['product_id'] ] ) ? $quantities[ $values['product_id'] ] + $values['quantity'] : $values['quantity'];
2012-08-06 12:24:59 +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
} else {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
$quantities[ $values['product_id'] ] = isset( $quantities[ $values['product_id'] ] ) ? $quantities[ $values['product_id'] ] + $values['quantity'] : $values['quantity'];
2012-08-06 12:24:59 +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
return $quantities;
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @access public
* @param array $posted
*/
public function check_customer_coupons( $posted ) {
global $woocommerce;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( ! empty( $this->applied_coupons ) ) {
foreach ( $this->applied_coupons as $key => $code ) {
$coupon = new WC_Coupon( $code );
2012-08-06 12:24:59 +00:00
if ( ! is_wp_error( $coupon->is_valid() ) && is_array( $coupon->customer_email ) && sizeof( $coupon->customer_email ) > 0 ) {
2012-08-06 12:24:59 +00:00
2012-10-18 10:33:47 +00:00
$coupon->customer_email = array_map( 'sanitize_email', $coupon->customer_email );
2012-03-20 13:22:35 +00:00
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$check_emails[] = $current_user->user_email;
}
$check_emails[] = $posted['billing_email'];
2012-08-06 12:24:59 +00:00
2012-10-18 10:33:47 +00:00
$check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) );
2012-03-20 18:36:25 +00:00
2012-09-07 17:48:30 +00:00
if ( 0 == sizeof( array_intersect( $check_emails, $coupon->customer_email ) ) ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
2012-03-20 13:22:35 +00:00
// Remove the coupon
2012-09-07 17:48:30 +00:00
unset( $this->applied_coupons[ $key ] );
$woocommerce->session->coupon_codes = $this->applied_coupons;
$woocommerce->session->refresh_totals = true;
2012-03-20 13:22:35 +00:00
}
}
}
}
}
2012-03-20 13:22:35 +00:00
/**
* Looks through cart items and checks the posts are not trashed or deleted.
* @return bool or WP_ERROR
*/
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
* @return bool or WP_ERROR
2011-12-08 12:50:50 +00:00
*/
public function check_cart_item_stock() {
global $wpdb;
2011-12-08 12:50:50 +00:00
$error = new WP_Error();
2012-08-06 12:24:59 +00:00
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
// 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'];
2012-08-06 12:24:59 +00:00
/**
* Check stock based on inventory
*/
2012-03-20 13:22:35 +00:00
if ( $_product->managing_stock() ) {
2012-08-06 12:24:59 +00:00
/**
* Check the stock for this item individually
*/
2012-03-20 13:22:35 +00:00
if ( ! $_product->is_in_stock() || ! $_product->has_enough_stock( $values['quantity'] ) ) {
2012-10-16 09:45:33 +00:00
$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->stock ) );
2011-12-08 12:50:50 +00:00
return $error;
}
2012-08-06 12:24:59 +00:00
// For later on...
$key = '_product_id';
$value = $values['product_id'];
$in_cart = $values['quantity'];
/**
2012-03-28 17:42:35 +00:00
* Next check entire cart quantities
*/
if ( $values['variation_id'] && $_product->variation_has_stock && isset( $product_qty_in_cart[ $values['variation_id'] ] ) ) {
2012-08-06 12:24:59 +00:00
$key = '_variation_id';
$value = $values['variation_id'];
$in_cart = $product_qty_in_cart[ $values['variation_id'] ];
if ( ! $_product->has_enough_stock( $product_qty_in_cart[ $values['variation_id'] ] ) ) {
2013-08-02 10:17:45 +00:00
$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->stock ) );
return $error;
}
2012-08-06 12:24:59 +00:00
} elseif ( isset( $product_qty_in_cart[ $values['product_id'] ] ) ) {
$in_cart = $product_qty_in_cart[ $values['product_id'] ];
2012-08-06 12:24:59 +00:00
if ( ! $_product->has_enough_stock( $product_qty_in_cart[ $values['product_id'] ] ) ) {
2013-08-02 10:17:45 +00:00
$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->stock ) );
return $error;
}
2012-08-06 12:24:59 +00:00
}
2012-08-06 12:24:59 +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;
$held_stock = $wpdb->get_var( $wpdb->prepare( "
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
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE order_item_meta.meta_key = '_qty'
AND order_item_meta2.meta_key = %s AND order_item_meta2.meta_value = %d
AND posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('pending')
AND posts.ID != %d
", $key, $value, $order_id ) );
if ( $_product->stock < ( $held_stock + $in_cart ) ) {
2013-08-02 10:17:45 +00:00
$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' ) ) );
return $error;
}
}
2012-03-28 17:42:35 +00:00
/**
* Check stock based on stock-status
*/
} else {
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() ) );
2012-03-28 17:42:35 +00:00
return $error;
}
}
}
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
*/
public function get_item_data( $cart_item, $flat = false ) {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
$return = '';
2012-01-03 17:23:42 +00:00
$has_data = false;
2012-08-06 12:24:59 +00:00
if ( ! $flat ) $return .= '<dl class="variation">';
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
2012-03-20 13:22:35 +00:00
if ( ! $value ) continue;
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// If this is a term slug, get the term's nice name
2012-03-20 13:22:35 +00:00
if ( taxonomy_exists( esc_attr( str_replace( 'attribute_', '', $name ) ) ) ) {
$term = get_term_by( 'slug', $value, esc_attr( str_replace( 'attribute_', '', $name ) ) );
if ( ! is_wp_error( $term ) && $term->name )
2011-12-08 12:50:50 +00:00
$value = $term->name;
2013-03-07 19:34:29 +00:00
// If this is a custom option slug, get the options name
2012-03-20 13:22:35 +00:00
} else {
2013-03-07 19:34:29 +00:00
$value = apply_filters( 'woocommerce_variation_option_name', $value );
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
if ( $flat )
$variation_list[] = $woocommerce->get_helper( 'attribute' )->attribute_label( str_replace( 'attribute_', '', $name ) ) . ': ' . $value;
2012-03-20 13:22:35 +00:00
else
$variation_list[] = '<dt>' . $woocommerce->get_helper( 'attribute' )->attribute_label( str_replace( 'attribute_', '', $name ) ) . ':</dt><dd>' . $value . '</dd>';
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
if ($flat)
2012-06-10 18:07:19 +00:00
$return .= implode( ", \n", $variation_list );
2012-03-20 13:22:35 +00:00
else
$return .= implode( '', $variation_list );
2012-08-06 12:24:59 +00:00
2012-01-03 17:23:42 +00:00
$has_data = true;
2012-08-06 12:24:59 +00:00
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
2011-12-08 12:50:50 +00:00
$data_list = array();
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +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'] ) ) {
2012-07-24 09:18:03 +00:00
$display_value = !empty($data['display']) ? $data['display'] : $data['value'];
2012-08-06 12:24:59 +00:00
2012-07-24 09:18:03 +00:00
if ($flat)
$data_list[] = $data['name'].': '.$display_value;
else
$data_list[] = '<dt>'.$data['name'].':</dt><dd>'.$display_value.'</dd>';
}
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ($flat)
2011-12-08 12:50:50 +00:00
$return .= implode(', ', $data_list);
2012-03-20 13:22:35 +00:00
else
2011-12-08 12:50:50 +00:00
$return .= implode('', $data_list);
2012-08-06 12:24:59 +00:00
2012-01-03 17:23:42 +00:00
$has_data = true;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
if ( ! $flat )
2012-03-20 13:22:35 +00:00
$return .= '</dl>';
2012-08-06 12:24:59 +00:00
if ( $has_data )
return $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
*/
public function get_cross_sells() {
2011-12-08 12:50:50 +00:00
$cross_sells = array();
$in_cart = array();
2012-03-20 13:22:35 +00:00
if ( sizeof( $this->cart_contents) > 0 ) {
foreach ( $this->cart_contents as $cart_item_key => $values ) {
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
*/
public function get_cart_url() {
2012-01-06 17:14:31 +00:00
$cart_page_id = woocommerce_get_page_id('cart');
2012-03-20 13:22:35 +00:00
if ( $cart_page_id ) return apply_filters( 'woocommerce_get_cart_url', 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
*/
public function get_checkout_url() {
2012-01-06 17:14:31 +00:00
$checkout_page_id = woocommerce_get_page_id('checkout');
$checkout_url = '';
2012-03-20 13:22:35 +00:00
if ( $checkout_page_id ) {
if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' )
$checkout_url = str_replace( 'http:', 'https:', get_permalink( $checkout_page_id ) );
2012-03-20 13:22:35 +00:00
else
$checkout_url = get_permalink( $checkout_page_id );
2012-03-20 13:22:35 +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
*
2012-03-20 13:22:35 +00:00
* @return string url to page
*/
public function get_remove_url( $cart_item_key ) {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-01-06 17:14:31 +00:00
$cart_page_id = woocommerce_get_page_id('cart');
2012-08-06 12:24:59 +00:00
if ($cart_page_id)
2013-06-11 16:55:55 +00:00
return apply_filters( 'woocommerce_get_remove_url', 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
*/
public function get_cart() {
2012-04-12 18:18:55 +00:00
return array_filter( (array) $this->cart_contents );
}
2012-08-06 12:24:59 +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
*/
public function get_taxes() {
$taxes = array();
// Merge
2012-03-20 13:22:35 +00:00
foreach ( array_keys( $this->taxes + $this->shipping_taxes ) as $key ) {
$taxes[ $key ] = ( isset( $this->shipping_taxes[ $key ] ) ? $this->shipping_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
}
2012-08-06 12:24:59 +00:00
return apply_filters( 'woocommerce_cart_get_taxes', $taxes, $this );
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Returns the cart and shipping taxes, merged & formatted.
2012-08-06 12:24:59 +00:00
*
* @return array merged taxes
*/
public function get_formatted_taxes() {
$taxes = $this->get_taxes();
foreach ( $taxes as $key => $tax )
if ( is_numeric( $tax ) )
$taxes[ $key ] = woocommerce_price( $tax );
return apply_filters( 'woocommerce_cart_formatted_taxes', $taxes, $this );
}
/**
* Get taxes, merged by code, formatted ready for output.
*
* @access public
* @return void
*/
public function get_tax_totals() {
$taxes = $this->get_taxes();
$tax_totals = array();
foreach ( $taxes as $key => $tax ) {
$code = $this->tax->get_rate_code( $key );
if ( ! isset( $tax_totals[ $code ] ) ) {
$tax_totals[ $code ] = new stdClass();
$tax_totals[ $code ]->amount = 0;
}
$tax_totals[ $code ]->is_compound = $this->tax->is_compound( $key );
$tax_totals[ $code ]->label = $this->tax->get_rate_label( $key );
$tax_totals[ $code ]->amount += $tax;
$tax_totals[ $code ]->formatted_amount = woocommerce_price( $tax_totals[ $code ]->amount );
}
return apply_filters( 'woocommerce_cart_tax_totals', $tax_totals, $this );
}
/*-----------------------------------------------------------------------------------*/
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
/**
2012-08-15 17:08:42 +00:00
* Check if product is in the cart and return cart item key.
2012-08-06 12:24:59 +00:00
*
2012-08-15 17:08:42 +00:00
* Cart item key will be unique based on the item and its properties, such as variations.
2012-03-20 13:22:35 +00:00
*
* @param mixed id of product to find in the cart
* @return string cart item key
2011-12-08 12:50:50 +00:00
*/
public function find_product_in_cart( $cart_id = false ) {
2012-08-06 12:24:59 +00:00
if ( $cart_id !== false )
foreach ( $this->cart_contents as $cart_item_key => $cart_item )
if ( $cart_item_key == $cart_id )
2012-03-20 13:22:35 +00:00
return $cart_item_key;
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
* Generate a unique ID for the cart item being added.
2012-03-20 13:22: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
* @return string cart item key
2011-12-08 12:50:50 +00:00
*/
public function generate_cart_id( $product_id, $variation_id = '', $variation = '', $cart_item_data = array() ) {
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$id_parts = array( $product_id );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $variation_id ) $id_parts[] = $variation_id;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( is_array( $variation ) ) {
2011-12-08 12:50:50 +00:00
$variation_key = '';
2012-03-20 13:22:35 +00:00
foreach ( $variation as $key => $value ) {
$variation_key .= trim( $key ) . trim( $value );
}
2011-12-08 12:50:50 +00:00
$id_parts[] = $variation_key;
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
2011-12-08 12:50:50 +00:00
$cart_item_data_key = '';
2012-03-20 13:22:35 +00:00
foreach ( $cart_item_data as $key => $value ) {
if ( is_array( $value ) ) $value = http_build_query( $value );
2011-12-08 12:50:50 +00:00
$cart_item_data_key .= trim($key) . trim($value);
2012-03-20 13:22:35 +00:00
}
2011-12-08 12:50:50 +00:00
$id_parts[] = $cart_item_data_key;
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
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
* @param string $quantity contains the quantity of the item to add
* @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
* @return bool
2011-12-08 12:50:50 +00:00
*/
public function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-08-06 12:24:59 +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
$cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id );
2012-08-06 12:24:59 +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
$cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
// See if this product and its options is already in the cart
2012-03-20 13:22:35 +00:00
$cart_item_key = $this->find_product_in_cart( $cart_id );
2012-08-06 12:24:59 +00:00
$product_data = get_product( $variation_id ? $variation_id : $product_id );
2012-08-06 12:24:59 +00:00
if ( ! $product_data )
return false;
2012-05-11 11:47:23 +00:00
// Force quantity to 1 if sold individually
if ( $product_data->is_sold_individually() )
$quantity = 1;
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() ) {
2013-06-11 14:59:54 +00:00
wc_add_error( sprintf( __( 'Sorry, &quot;%s&quot; cannot be purchased.', 'woocommerce' ), $product_data->get_title() ) );
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
if ( ! $product_data->is_in_stock() ) {
2012-11-27 16:22:47 +00:00
2013-06-11 14:59:54 +00:00
wc_add_error( sprintf( __( 'You cannot add &quot;%s&quot; to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_title() ) );
2012-11-27 16:22:47 +00:00
2012-08-06 12:24:59 +00:00
return false;
} elseif ( ! $product_data->has_enough_stock( $quantity ) ) {
2012-11-27 16:22:47 +00:00
2013-06-11 14:59:54 +00:00
wc_add_error( sprintf(__( 'You cannot add that amount of &quot;%s&quot; to the cart because there is not enough stock (%s remaining).', 'woocommerce' ), $product_data->get_title(), $product_data->get_stock_quantity() ));
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-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() ) {
$in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] : 0;
2012-08-06 12:24:59 +00:00
// If its greater than 0, its already in the cart
if ( $in_cart_quantity > 0 ) {
2013-06-11 14:59:54 +00:00
wc_add_error( sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __( 'View Cart &rarr;', 'woocommerce' ), __( 'You already have this item in your cart.', 'woocommerce' ) ) );
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
$product_qty_in_cart = $this->get_cart_item_quantities();
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
if ( $product_data->managing_stock() ) {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
// Variations
if ( $variation_id && $product_data->variation_has_stock ) {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
if ( isset( $product_qty_in_cart[ $variation_id ] ) && ! $product_data->has_enough_stock( $product_qty_in_cart[ $variation_id ] + $quantity ) ) {
2013-06-11 14:59:54 +00:00
wc_add_error( sprintf(__( '<a href="%s" class="button">%s</a> You cannot add that amount to the cart &mdash; we have %s in stock and you already have %s in your cart.', 'woocommerce' ), get_permalink(woocommerce_get_page_id('cart')), __( 'View Cart &rarr;', 'woocommerce' ), $product_data->get_stock_quantity(), $product_qty_in_cart[ $variation_id ] ));
2012-03-28 17:42:35 +00:00
return false;
}
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
// Products
} else {
2012-08-06 12:24:59 +00:00
2012-03-28 17:42:35 +00:00
if ( isset( $product_qty_in_cart[ $product_id ] ) && ! $product_data->has_enough_stock( $product_qty_in_cart[ $product_id ] + $quantity ) ) {
2013-06-11 14:59:54 +00:00
wc_add_error( sprintf(__( '<a href="%s" class="button">%s</a> You cannot add that amount to the cart &mdash; we have %s in stock and you already have %s in your cart.', 'woocommerce' ), get_permalink(woocommerce_get_page_id('cart')), __( 'View Cart &rarr;', 'woocommerce' ), $product_data->get_stock_quantity(), $product_qty_in_cart[ $product_id ] ));
2012-03-28 17:42:35 +00:00
return false;
}
2012-08-06 12:24:59 +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
// 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
2012-04-10 11:18:41 +00:00
$this->set_quantity( $cart_item_key, $new_quantity );
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
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-04-09 09:38:40 +00:00
$this->set_cart_cookies();
$this->calculate_totals();
2012-08-06 12:24:59 +00:00
return true;
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
*
* @param string cart_item_key contains the id of the cart item
* @param string quantity contains the quantity of the item
*/
public function set_quantity( $cart_item_key, $quantity = 1 ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $quantity == 0 || $quantity < 0 ) {
do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key );
2012-03-20 13:22:35 +00:00
unset( $this->cart_contents[$cart_item_key] );
} else {
2011-12-08 12:50:50 +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
$this->calculate_totals();
$this->set_session();
2011-12-08 12:50:50 +00:00
}
2011-11-23 23:19:23 +00:00
2013-04-09 09:38:40 +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 ( ! headers_sent() ) {
if ( $set ) {
setcookie( "woocommerce_items_in_cart", "1", 0, COOKIEPATH, COOKIE_DOMAIN, false );
setcookie( "woocommerce_cart_hash", md5( json_encode( $this->get_cart() ) ), 0, COOKIEPATH, COOKIE_DOMAIN, false );
} else {
setcookie( "woocommerce_items_in_cart", "0", time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false );
setcookie( "woocommerce_cart_hash", "0", time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false );
}
}
}
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
*/
private function reset() {
2012-09-07 17:48:30 +00:00
global $woocommerce;
$this->total = $this->cart_contents_total = $this->cart_contents_weight = $this->cart_contents_count = $this->cart_contents_tax = $this->tax_total = $this->shipping_tax_total = $this->subtotal = $this->subtotal_ex_tax = $this->discount_total = $this->discount_cart = $this->shipping_total = $this->fee_total = 0;
2013-02-15 10:27:09 +00:00
$this->shipping_taxes = $this->taxes = $this->coupon_discount_amounts = array();
2012-08-06 12:24:59 +00:00
2012-09-07 17:48:30 +00:00
unset( $woocommerce->session->cart_contents_total, $woocommerce->session->cart_contents_weight, $woocommerce->session->cart_contents_count, $woocommerce->session->cart_contents_tax, $woocommerce->session->total, $woocommerce->session->subtotal, $woocommerce->session->subtotal_ex_tax, $woocommerce->session->tax_total, $woocommerce->session->taxes, $woocommerce->session->shipping_taxes, $woocommerce->session->discount_cart, $woocommerce->session->discount_total, $woocommerce->session->shipping_total, $woocommerce->session->shipping_tax_total, $woocommerce->session->shipping_label );
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
* Function to apply discounts to a product and get the discounted price (before tax is applied).
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @access public
* @param mixed $values
* @param mixed $price
* @param bool $add_totals (default: false)
* @return float price
2011-12-08 12:50:50 +00:00
*/
public function get_discounted_price( $values, $price, $add_totals = false ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( ! $price ) return $price;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( ! empty( $this->applied_coupons ) ) {
foreach ( $this->applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $coupon->apply_before_tax() && $coupon->is_valid() ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
switch ( $coupon->type ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
case "fixed_product" :
case "percent_product" :
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = false;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$product_cats = wp_get_post_terms( $values['product_id'], 'product_cat', array("fields" => "ids") );
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Specific products get the discount
if ( sizeof( $coupon->product_ids ) > 0 ) {
2012-08-06 12:24:59 +00:00
if ( in_array( $values['product_id'], $coupon->product_ids ) || in_array( $values['variation_id'], $coupon->product_ids ) || in_array( $values['data']->get_parent(), $coupon->product_ids ) )
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = true;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Category discounts
} elseif ( sizeof($coupon->product_categories ) > 0 ) {
2012-08-06 12:24:59 +00:00
if ( sizeof( array_intersect( $product_cats, $coupon->product_categories ) ) > 0 )
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = true;
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-03-20 13:22:35 +00:00
// No product ids - all items discounted
2011-12-08 12:50:50 +00:00
$this_item_is_discounted = true;
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
// Specific product ID's excluded from the discount
2012-08-06 12:24:59 +00:00
if ( sizeof( $coupon->exclude_product_ids ) > 0 )
2012-03-20 13:22:35 +00:00
if ( in_array( $values['product_id'], $coupon->exclude_product_ids ) || in_array( $values['variation_id'], $coupon->exclude_product_ids ) || in_array( $values['data']->get_parent(), $coupon->exclude_product_ids ) )
$this_item_is_discounted = false;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Specific categories excluded from the discount
2012-08-06 12:24:59 +00:00
if ( sizeof( $coupon->exclude_product_categories ) > 0 )
if ( sizeof( array_intersect( $product_cats, $coupon->exclude_product_categories ) ) > 0 )
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = false;
2012-08-06 12:24:59 +00:00
// Sale Items excluded from discount
if ( $coupon->exclude_sale_items == 'yes' )
if ( in_array( $values['product_id'], $product_ids_on_sale, true ) || in_array( $values['variation_id'], $product_ids_on_sale, true ) || in_array( $values['data']->get_parent(), $product_ids_on_sale, true ) )
$this_item_is_discounted = false;
2012-03-20 13:22:35 +00:00
// Apply filter
$this_item_is_discounted = apply_filters( 'woocommerce_item_is_discounted', $this_item_is_discounted, $values, $before_tax = true, $coupon );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Apply the discount
if ( $this_item_is_discounted ) {
if ( $coupon->type=='fixed_product' ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $price < $coupon->amount ) {
$discount_amount = $price;
} else {
$discount_amount = $coupon->amount;
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$price = $price - $coupon->amount;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $price < 0 ) $price = 0;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $add_totals ) {
$this->discount_cart = $this->discount_cart + ( $discount_amount * $values['quantity'] );
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, $discount_amount * $values['quantity'] );
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
} elseif ( $coupon->type == 'percent_product' ) {
2012-08-06 12:24:59 +00:00
2012-12-20 12:44:11 +00:00
$percent_discount = ( $values['data']->get_price() / 100 ) * $coupon->amount;
2012-08-06 12:24:59 +00:00
if ( $add_totals ) {
$this->discount_cart = $this->discount_cart + ( $percent_discount * $values['quantity'] );
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, $percent_discount * $values['quantity'] );
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$price = $price - $percent_discount;
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
break;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
case "fixed_cart" :
2012-08-06 12:24:59 +00:00
/**
2012-03-20 13:22:35 +00:00
* This is the most complex discount - we need to divide the discount between rows based on their price in
* proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows
* with no price (free) don't get discount too.
*/
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Get item discount by dividing item cost by subtotal to get a %
2012-08-06 12:24:59 +00:00
if ( $this->subtotal_ex_tax )
$discount_percent = ( $values['data']->get_price_excluding_tax() * $values['quantity'] ) / $this->subtotal_ex_tax;
2012-03-20 13:22:35 +00:00
else
$discount_percent = 0;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Use pence to help prevent rounding errors
$coupon_amount_pence = $coupon->amount * 100;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Work out the discount for the row
$item_discount = $coupon_amount_pence * $discount_percent;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Work out discount per item
$item_discount = $item_discount / $values['quantity'];
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Pence
$price = $price * 100;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Check if discount is more than price
if ( $price < $item_discount )
$discount_amount = $price;
else
$discount_amount = $item_discount;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Take discount off of price (in pence)
$price = $price - $discount_amount;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Back to pounds
2012-08-06 12:24:59 +00:00
$price = $price / 100;
2012-03-20 13:22:35 +00:00
// Cannot be below 0
if ( $price < 0 )
$price = 0;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Add coupon to discount total (once, since this is a fixed cart discount and we don't want rounding issues)
if ( $add_totals ) {
$this->discount_cart = $this->discount_cart + ( ( $discount_amount * $values['quantity'] ) / 100 );
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, ( $discount_amount * $values['quantity'] ) / 100 );
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
break;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
case "percent" :
2012-08-06 12:24:59 +00:00
2012-08-21 18:01:56 +00:00
$percent_discount = round( ( $values['data']->get_price() / 100 ) * $coupon->amount, $this->dp );
2012-08-06 12:24:59 +00:00
if ( $add_totals ) {
$this->discount_cart = $this->discount_cart + ( $percent_discount * $values['quantity'] );
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, $percent_discount * $values['quantity'] );
}
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$price = $price - $percent_discount;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
break;
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-06 17:58:21 +00:00
return apply_filters( 'woocommerce_get_discounted_price', $price, $values, $this );
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
* Function to apply product discounts after tax.
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @access public
* @param mixed $values
* @param mixed $price
2011-12-08 12:50:50 +00:00
*/
public function apply_product_discounts_after_tax( $values, $price ) {
2013-01-29 13:58:57 +00:00
2012-03-20 13:22:35 +00:00
if ( ! empty( $this->applied_coupons) ) {
foreach ( $this->applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
2012-08-06 12:24:59 +00:00
do_action( 'woocommerce_product_discount_after_tax_' . $coupon->type, $coupon, $values, $price );
2012-08-06 12:24:59 +00:00
if ( ! $coupon->is_valid() ) continue;
2012-03-20 13:22:35 +00:00
if ( $coupon->type != 'fixed_product' && $coupon->type != 'percent_product' ) continue;
2012-08-06 12:24:59 +00:00
if ( ! $coupon->apply_before_tax() ) {
2012-08-06 12:24:59 +00:00
2012-03-23 20:46:17 +00:00
$product_cats = wp_get_post_terms( $values['product_id'], 'product_cat', array("fields" => "ids") );
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = false;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Specific products get the discount
if ( sizeof( $coupon->product_ids ) > 0 ) {
2012-08-06 12:24:59 +00:00
if (in_array($values['product_id'], $coupon->product_ids) || in_array($values['variation_id'], $coupon->product_ids) || in_array($values['data']->get_parent(), $coupon->product_ids))
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = true;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Category discounts
} elseif ( sizeof( $coupon->product_categories ) > 0 ) {
2012-08-06 12:24:59 +00:00
if ( sizeof( array_intersect( $product_cats, $coupon->product_categories ) ) > 0 )
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = true;
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-03-20 13:22:35 +00:00
// No product ids - all items discounted
$this_item_is_discounted = true;
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
// Specific product ID's excluded from the discount
2012-08-06 12:24:59 +00:00
if ( sizeof( $coupon->exclude_product_ids ) > 0 )
2012-03-20 13:22:35 +00:00
if ( in_array( $values['product_id'], $coupon->exclude_product_ids ) || in_array( $values['variation_id'], $coupon->exclude_product_ids ) || in_array( $values['data']->get_parent(), $coupon->exclude_product_ids ) )
$this_item_is_discounted = false;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Specific categories excluded from the discount
2012-08-06 12:24:59 +00:00
if ( sizeof( $coupon->exclude_product_categories ) > 0 )
if ( sizeof( array_intersect( $product_cats, $coupon->exclude_product_categories ) ) > 0 )
2012-03-20 13:22:35 +00:00
$this_item_is_discounted = false;
// Sale Items excluded from discount
2013-01-29 13:58:57 +00:00
if ( $coupon->exclude_sale_items == 'yes' )
if ( in_array( $values['product_id'], $product_ids_on_sale, true ) || in_array( $values['variation_id'], $product_ids_on_sale, true ) || in_array( $values['data']->get_parent(), $product_ids_on_sale, true ) )
$this_item_is_discounted = false;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Apply filter
$this_item_is_discounted = apply_filters( 'woocommerce_item_is_discounted', $this_item_is_discounted, $values, $before_tax = false, $coupon );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
// Apply the discount
if ( $this_item_is_discounted ) {
if ( $coupon->type == 'fixed_product' ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( $price < $coupon->amount )
$discount_amount = $price;
else
$discount_amount = $coupon->amount;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
$this->discount_total = $this->discount_total + ( $discount_amount * $values['quantity'] );
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, $discount_amount * $values['quantity'] );
2012-03-20 13:22:35 +00:00
} elseif ( $coupon->type == 'percent_product' ) {
2012-08-21 18:01:56 +00:00
$this->discount_total = $this->discount_total + round( ( $price / 100 ) * $coupon->amount, $this->dp );
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, round( ( $price / 100 ) * $coupon->amount, $this->dp ) );
2012-03-20 13:22:35 +00:00
}
}
}
}
}
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-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Function to apply cart discounts after tax.
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @access public
2011-12-08 12:50:50 +00:00
*/
public function apply_cart_discounts_after_tax() {
2012-08-06 12:24:59 +00:00
$pre_discount_total = number_format( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp, '.', '' );
2012-03-20 13:22:35 +00:00
if ( $this->applied_coupons ) {
foreach ( $this->applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
do_action( 'woocommerce_cart_discount_after_tax_' . $coupon->type, $coupon );
2012-08-06 12:24:59 +00:00
if ( ! $coupon->apply_before_tax() && $coupon->is_valid() ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
switch ( $coupon->type ) {
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
case "fixed_cart" :
2012-08-06 12:24:59 +00:00
if ( $coupon->amount > $pre_discount_total )
$coupon->amount = $pre_discount_total;
$pre_discount_total = $pre_discount_total - $coupon->amount;
2012-03-20 13:22:35 +00:00
$this->discount_total = $this->discount_total + $coupon->amount;
2012-08-06 12:24:59 +00:00
2013-02-15 10:27:09 +00:00
$this->increase_coupon_discount_amount( $code, $coupon->amount );
2012-03-20 13:22:35 +00:00
break;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
case "percent" :
2012-08-06 12:24:59 +00:00
2013-06-28 11:10:44 +00:00
$percent_discount = round( ( round( $this->cart_contents_total + $this->tax_total + $this->fee_total, $this->dp ) / 100 ) * $coupon->amount, $this->dp );
if ( $coupon->amount > $percent_discount )
$coupon->amount = $percent_discount;
$pre_discount_total = $pre_discount_total - $percent_discount;
2012-08-06 12:24:59 +00:00
$this->discount_total = $this->discount_total + $percent_discount;
2012-08-06 12:24:59 +00:00
$this->increase_coupon_discount_amount( $code, $percent_discount );
2012-03-20 13:22:35 +00:00
break;
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
}
}
}
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
2013-02-15 10:27:09 +00:00
/**
* Store how much discount each coupon grants.
*
* @access private
* @param mixed $code
* @param mixed $amount
* @return void
*/
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;
}
2012-03-20 13:22:35 +00:00
/**
2012-08-15 17:08:42 +00:00
* Calculate totals for the items in the cart.
2012-08-06 12:24:59 +00:00
*
2012-03-20 13:22:35 +00:00
* @access public
2011-12-08 12:50:50 +00:00
*/
public function calculate_totals() {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
$this->reset();
2012-08-06 12:24:59 +00:00
do_action( 'woocommerce_before_calculate_totals', $this );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
if ( sizeof( $this->get_cart() ) == 0 )
return;
2011-12-08 12:50:50 +00:00
// Get count of all items + weights + subtotal (we may need this for discounts)
2013-06-13 16:01:36 +00:00
$subtotal = 0;
$subtotal_tax = 0;
$tax_rates = array();
2012-08-06 12:24:59 +00:00
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
$this->cart_contents_weight = $this->cart_contents_weight + ( $_product->get_weight() * $values['quantity'] );
$this->cart_contents_count = $this->cart_contents_count + $values['quantity'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Line price
$line_price = $_product->get_price() * $values['quantity'];
if ( ! $_product->is_taxable() ) {
$subtotal += $line_price;
} else {
// 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 );
$base_tax_rates = $shop_tax_rates[ $_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() );
$item_tax_rates = $tax_rates[ $_product->get_tax_class() ];
2012-08-06 12:24:59 +00:00
if ( $this->prices_include_tax ) {
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// ADJUST BASE if tax rate is different (different region or modified tax class)
if ( $item_tax_rates !== $base_tax_rates ) {
// Work out a new base price without the shop's base tax
$line_tax = array_sum( $this->tax->calc_tax( $line_price, $base_tax_rates, true ) );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Now we have a new item price (excluding TAX)
$line_price = $this->tax->round( $line_price - $line_tax );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Now add taxes for the users location
$line_tax = array_sum( $this->tax->calc_tax( $line_price, $item_tax_rates, false ) );
$line_tax = $this->round_at_subtotal ? $line_tax : $this->tax->round( $line_tax );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Add to main subtotal
$subtotal += $line_price;
$subtotal_tax += $line_tax;
} else {
// Calc tax normally
$line_tax = array_sum( $this->tax->calc_tax( $line_price, $item_tax_rates, true ) );
$line_tax = $this->round_at_subtotal ? $line_tax : $this->tax->round( $line_tax );
2012-12-20 15:10:27 +00:00
2013-06-13 16:01:36 +00:00
// Add to main subtotal
$subtotal += $line_price - $line_tax;
$subtotal_tax += $line_tax;
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
2012-03-20 13:22:35 +00:00
if ( $_product->is_taxable() ) {
2013-06-13 16:01:36 +00:00
$taxes = $this->tax->calc_tax( $line_price, $item_tax_rates, false );
$line_tax = $this->round_at_subtotal ? array_sum( $taxes ) : $this->tax->get_tax_total( $taxes );
$subtotal_tax += $line_tax;
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
$subtotal += $line_price;
2012-03-20 13:22:35 +00:00
}
2013-06-13 16:01:36 +00:00
2012-03-20 13:22:35 +00:00
}
2013-06-13 16:01:36 +00:00
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
$this->subtotal = $subtotal + $subtotal_tax;
$this->subtotal_ex_tax = $subtotal;
2011-12-08 12:50:50 +00:00
// Now calc the main totals, including discounts
2012-03-20 13:22:35 +00:00
if ( $this->prices_include_tax ) {
2012-08-06 12:24:59 +00:00
/**
2011-12-08 12:50:50 +00:00
* Calculate totals for items
*/
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
/**
* 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 1p 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
*
* Used this excellent article for reference:
* http://developer.practicalecommerce.com/articles/1473-Coding-for-Tax-Calculations-Everything-You-Never-Wanted-to-Know-Part-2
*/
$_product = $values['data'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Prices
$base_price = $_product->get_price();
$line_price = $_product->get_price() * $values['quantity'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Base Price Adjustment
if ( ! $_product->is_taxable() ) {
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +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;
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
} else {
2012-08-06 12:24: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 );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
$base_tax_rates = $shop_tax_rates[ $_product->tax_class ];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +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-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-06-13 16:01:36 +00:00
/**
* ADJUST TAX - Calculations when customer is OUTSIDE the shop base country/state and prices INCLUDE tax
* OR
* ADJUST TAX - Calculations when a tax class is modified
*/
if ( $_product->get_tax_class() !== $_product->tax_class || ( $woocommerce->customer->is_customer_outside_base() && ( defined('WOOCOMMERCE_CHECKOUT') || $woocommerce->customer->has_calculated_shipping() ) ) ) {
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Work out a new base price without the shop's base tax
$line_tax = array_sum( $this->tax->calc_tax( $line_price, $base_tax_rates, true ) );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Now we have a new item price (excluding TAX)
$line_subtotal = $this->tax->round( $line_price - $line_tax );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Now add taxes for the users location
$line_subtotal_tax = array_sum( $this->tax->calc_tax( $line_subtotal, $item_tax_rates, false ) );
$line_subtotal_tax = $this->round_at_subtotal ? $line_subtotal_tax : $this->tax->round( $line_subtotal_tax );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Adjusted price (this is the price including the new tax rate)
$adjusted_price = ( $line_subtotal + $line_subtotal_tax ) / $values['quantity'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Apply discounts
$discounted_price = $this->get_discounted_price( $values, $adjusted_price, true );
$discounted_taxes = $this->tax->calc_tax( $discounted_price * $values['quantity'], $item_tax_rates, true );
$discounted_tax_amount = array_sum( $discounted_taxes ); // Sum taxes
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
/**
* Regular tax calculation (customer inside base and the tax class is unmodified
*/
} else {
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Calc tax normally
$line_subtotal_tax = array_sum( $this->tax->calc_tax( $line_price, $item_tax_rates, true ) );
$line_subtotal_tax = $this->round_at_subtotal ? $line_subtotal_tax : $this->tax->round( $line_subtotal_tax );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Subtotal
$line_subtotal = $line_price - $line_subtotal_tax;
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Calc prices and tax (discounted)
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
$discounted_taxes = $this->tax->calc_tax( $discounted_price * $values['quantity'], $item_tax_rates, true );
$discounted_tax_amount = array_sum( $discounted_taxes ); // Sum taxes
}
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Tax rows - merge the totals we just got
foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) {
$this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
2012-08-06 12:24:59 +00:00
}
2013-06-13 16:01:36 +00:00
}
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Line prices
$line_tax = $this->round_at_subtotal ? $discounted_tax_amount : $this->tax->round( $discounted_tax_amount );
$line_total = $this->tax->round( ( $discounted_price * $values['quantity'] ) - $line_tax );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Add any product discounts (after tax)
$this->apply_product_discounts_after_tax( $values, $line_total + $discounted_tax_amount );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Cart contents total is based on discounted prices and is used for the final total calculation
$this->cart_contents_total = $this->cart_contents_total + $line_total;
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Store costs + taxes for lines
$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;
$this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $line_subtotal_tax;
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-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
/**
* Prices exclude tax
*
* This calculation is simpler - work with the base, untaxed price.
*/
$_product = $values['data'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Prices
$base_price = $_product->get_price();
$line_price = $_product->get_price() * $values['quantity'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Discounted Price (base price with any pre-tax discounts applied
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Taxes
if ( ! $_product->is_taxable() ) {
$discounted_tax_amount = 0;
$line_subtotal_tax = 0;
} else {
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +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-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-06-13 16:01:36 +00:00
// Base tax for line before discount - we will store this in the order data
$line_subtotal_tax = array_sum( $this->tax->calc_tax( $line_price, $item_tax_rates, false ) );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Now calc product rates
$discounted_taxes = $this->tax->calc_tax( $discounted_price * $values['quantity'], $item_tax_rates, false );
$discounted_tax_amount = array_sum( $discounted_taxes );
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Tax rows - merge the totals we just got
foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) {
$this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
2011-12-30 21:11:18 +00:00
}
2013-06-13 16:01:36 +00:00
}
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Line prices
$line_tax = $discounted_tax_amount;
$line_subtotal = $line_price;
$line_total = $discounted_price * $values['quantity'];
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +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-06-13 16:01:36 +00:00
// Cart contents total is based on discounted prices and is used for the final total calculation
$this->cart_contents_total = $this->cart_contents_total + $line_total;
2012-08-06 12:24:59 +00:00
2013-06-13 16:01:36 +00:00
// Store costs + taxes for lines
$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;
$this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $line_subtotal_tax;
2012-03-20 13:22:35 +00:00
}
}
2012-11-27 16:22:47 +00:00
// Add fees
foreach ( $this->get_fees() as $fee ) {
$this->fee_total += $fee->amount;
2012-11-27 16:22:47 +00:00
if ( $fee->taxable ) {
// Get tax rates
2013-02-05 16:43:28 +00:00
$tax_rates = $this->tax->get_rates( $fee->tax_class );
$fee_taxes = $this->tax->calc_tax( $fee->amount, $tax_rates, false );
2012-11-27 16:22:47 +00:00
2012-11-12 17:15:54 +00:00
// Store
$fee->tax = array_sum( $fee_taxes );
2012-11-27 16:22:47 +00:00
// Tax rows - merge the totals we just got
foreach ( array_keys( $this->taxes + $fee_taxes ) as $key ) {
$this->taxes[ $key ] = ( isset( $fee_taxes[ $key ] ) ? $fee_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
}
}
}
2012-08-06 12:24:59 +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-04-12 09:59:38 +00:00
// Total up/round 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->taxes = array_map( array( $this->tax, 'round' ), $this->taxes );
} else {
$this->tax_total = array_sum( $this->taxes );
}
// Cart Shipping
$this->calculate_shipping();
2012-08-06 12:24:59 +00:00
2013-04-12 09:59:38 +00:00
// Total up/round taxes for shipping
2013-06-13 16:01:36 +00:00
if ( $this->round_at_subtotal ) {
2013-04-12 09:59:38 +00:00
$this->shipping_tax_total = $this->tax->get_tax_total( $this->shipping_taxes );
$this->shipping_taxes = array_map( array( $this->tax, 'round' ), $this->shipping_taxes );
} else {
$this->shipping_tax_total = array_sum( $this->shipping_taxes );
}
2013-04-12 09:59:38 +00:00
// VAT exemption done at this point - so all totals are correct before exemption
if ( $woocommerce->customer->is_vat_exempt() )
$this->remove_taxes();
// Cart Discounts (after tax)
$this->apply_cart_discounts_after_tax();
// 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)
$this->total = max( 0, apply_filters( 'woocommerce_calculated_total', number_format( $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
if ( $woocommerce->customer->is_vat_exempt() )
$this->remove_taxes();
2013-04-12 09:59:38 +00:00
// Cart Discounts (after tax)
$this->apply_cart_discounts_after_tax();
}
2012-11-27 16:22:47 +00:00
$this->set_session();
2011-12-08 12:50:50 +00:00
}
2013-04-12 09:59:38 +00:00
/**
* remove_taxes function.
*
* @access public
* @return void
*/
public function remove_taxes() {
$this->shipping_tax_total = $this->tax_total = 0;
$this->taxes = $this->shipping_taxes = array();
$this->subtotal = $this->subtotal_ex_tax;
2013-04-12 09:59:38 +00:00
foreach ( $this->cart_contents as $cart_item_key => $item )
$this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $this->cart_contents[ $cart_item_key ]['line_tax'] = 0;
}
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
*/
public function needs_payment() {
2012-09-24 02:54:41 +00:00
$needs_payment = ( $this->total > 0 ) ? true : false;
return apply_filters( 'woocommerce_cart_needs_payment', $needs_payment, $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
/*-----------------------------------------------------------------------------------*/
/* Shipping related functions */
2012-08-06 12:24:59 +00:00
/*-----------------------------------------------------------------------------------*/
2011-12-08 12:50:50 +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
*/
public function calculate_shipping() {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
2012-04-11 12:08:04 +00:00
if ( $this->needs_shipping() && $this->show_shipping() ) {
$woocommerce->shipping->calculate_shipping( $this->get_shipping_packages() );
2012-03-20 13:22:35 +00:00
} else {
2012-08-06 12:24:59 +00:00
$woocommerce->shipping->reset_shipping();
2012-03-20 13:22:35 +00:00
}
2012-08-06 12:24:59 +00:00
// Get totals for the chosen shipping method
2011-12-08 12:50:50 +00:00
$this->shipping_total = $woocommerce->shipping->shipping_total; // Shipping Total
$this->shipping_label = $woocommerce->shipping->shipping_label; // Shipping Label
$this->shipping_taxes = $woocommerce->shipping->shipping_taxes; // Shipping Taxes
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +00:00
/**
2012-08-15 17:08:42 +00:00
* Get packages to calculate shipping for.
*
2012-08-15 17:08:42 +00:00
* This lets us calculate costs for carts that are shipped to multiple locations.
*
2013-03-03 17:07:31 +00:00
* 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.
2012-08-06 12:24:59 +00:00
*
* @since 1.5.4
* @access public
* @return array of cart items
*/
public function get_shipping_packages() {
global $woocommerce;
2012-08-06 12:24:59 +00:00
// Packages array for storing 'carts'
$packages = array();
2012-08-06 12:24:59 +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
$packages[0]['applied_coupons'] = $this->applied_coupons; // Applied coupons - some, like free shipping, affect costs
$packages[0]['destination']['country'] = $woocommerce->customer->get_shipping_country();
$packages[0]['destination']['state'] = $woocommerce->customer->get_shipping_state();
$packages[0]['destination']['postcode'] = $woocommerce->customer->get_shipping_postcode();
$packages[0]['destination']['city'] = $woocommerce->customer->get_shipping_city();
$packages[0]['destination']['address'] = $woocommerce->customer->get_shipping_address();
$packages[0]['destination']['address_2'] = $woocommerce->customer->get_shipping_address_2();
2012-08-06 12:24:59 +00:00
foreach ( $this->get_cart() as $item )
if ( $item['data']->needs_shipping() )
$packages[0]['contents_cost'] += $item['line_total'];
2012-08-06 12:24:59 +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
*/
public function needs_shipping() {
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
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
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
/**
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
*/
public function show_shipping() {
2012-04-11 12:08:04 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
2012-04-11 12:08:04 +00:00
if ( get_option('woocommerce_calc_shipping')=='no' ) return false;
if ( ! 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' ) {
if ( ! $woocommerce->customer->has_calculated_shipping() ) {
if ( ! $woocommerce->customer->get_shipping_country() || ( ! $woocommerce->customer->get_shipping_state() && ! $woocommerce->customer->get_shipping_postcode() ) ) 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
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
*/
public function ship_to_billing_address_only() {
2012-03-20 13:22:35 +00:00
if ( get_option('woocommerce_ship_to_billing_address_only') == 'yes' ) return true; else return false;
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
*
* @return mixed price or string for the shipping total
2011-12-08 12:50:50 +00:00
*/
public function get_cart_shipping_total() {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
2012-03-20 13:22:35 +00:00
if ( isset( $this->shipping_label ) ) {
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
2012-03-20 13:22:35 +00:00
$return = woocommerce_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 ) {
$return .= ' <small>' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
}
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
2012-03-20 13:22:35 +00:00
$return = woocommerce_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 ) {
$return .= ' <small>' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
}
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
}
}
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 title of the chosen shipping method.
2012-03-20 13:22:35 +00:00
*
* @return string shipping method title
2011-12-08 12:50:50 +00:00
*/
public function get_cart_shipping_title() {
2012-03-20 13:22:35 +00:00
if ( isset( $this->shipping_label ) ) {
2012-10-16 09:45:33 +00:00
return __( 'via', 'woocommerce' ) . ' ' . $this->shipping_label;
2012-03-20 13:22:35 +00:00
}
2011-12-08 12:50:50 +00:00
return false;
}
/*-----------------------------------------------------------------------------------*/
/* Coupons/Discount related 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
* 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
*/
public function has_discount( $coupon_code ) {
// Sanitize coupon code
$coupon_code = apply_filters( 'woocommerce_coupon_code', $coupon_code );
// Check if its set
return in_array( $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
*/
public function add_discount( $coupon_code ) {
2011-12-08 12:50:50 +00:00
global $woocommerce;
// Coupons are globally disabled
if ( ! $woocommerce->cart->coupons_enabled() )
return false;
// Sanitize coupon code
$coupon_code = apply_filters( 'woocommerce_coupon_code', $coupon_code );
// Get the coupon
$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
if ( ! $the_coupon->is_valid() ) {
2013-06-11 14:59:54 +00:00
wc_add_error( $the_coupon->get_error_message() );
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
// Check if applied
2012-03-20 13:22:35 +00:00
if ( $woocommerce->cart->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
}
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' ) {
$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
if ( $this->applied_coupons ) {
foreach ( $this->applied_coupons as $code ) {
$existing_coupon = new WC_Coupon( $code );
if ( $existing_coupon->individual_use == 'yes' && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $existing_coupon, $this->applied_coupons ) ) {
// Reject new coupon
$existing_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
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
$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() ) {
$woocommerce->session->chosen_shipping_method = 'free_shipping';
}
2012-08-06 12:24:59 +00:00
$this->calculate_totals();
$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 {
$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
/**
2012-08-15 17:08:42 +00:00
* Gets the array of applied coupon codes.
2012-03-20 13:22:35 +00:00
*
* @return array of applied coupons
2011-12-08 12:50:50 +00:00
*/
public function get_applied_coupons() {
2011-12-08 12:50:50 +00:00
return (array) $this->applied_coupons;
}
2012-08-06 12:24:59 +00:00
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
*
* @params int type - 0 for all, 1 for before tax, 2 for after tax
2011-12-08 12:50:50 +00:00
*/
public function remove_coupons( $type = 0 ) {
2012-09-07 17:48:30 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
2012-09-07 17:48:30 +00:00
if ( 1 == $type ) {
if ( $this->applied_coupons ) {
2012-03-20 13:22:35 +00:00
foreach ( $this->applied_coupons as $index => $code ) {
$coupon = new WC_Coupon( $code );
if ( $coupon->is_valid() && $coupon->apply_before_tax() ) unset( $this->applied_coupons[ $index ] );
2012-03-20 13:22:35 +00:00
}
}
2012-09-07 17:48:30 +00:00
$woocommerce->session->coupon_codes = $this->applied_coupons;
2012-03-20 13:22:35 +00:00
} elseif ( $type == 2 ) {
if ( $this->applied_coupons ) {
foreach ( $this->applied_coupons as $index => $code ) {
$coupon = new WC_Coupon( $code );
if ( $coupon->is_valid() && ! $coupon->apply_before_tax() ) unset( $this->applied_coupons[ $index ] );
2012-03-20 13:22:35 +00:00
}
}
2012-09-07 17:48:30 +00:00
$woocommerce->session->coupon_codes = $this->applied_coupons;
2012-03-20 13:22:35 +00:00
} else {
unset( $woocommerce->session->coupon_codes, $woocommerce->session->coupon_amounts );
2011-12-08 12:50:50 +00:00
$this->applied_coupons = array();
2012-03-20 13:22:35 +00:00
}
2011-12-08 12:50:50 +00:00
}
/*-----------------------------------------------------------------------------------*/
/* Fees API to add additonal costs to orders */
/*-----------------------------------------------------------------------------------*/
/**
* add_fee function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @param mixed $name
* @param mixed $amount
* @param bool $taxable (default: false)
* @param string $tax_class (default: '')
* @return void
*/
public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) {
2012-11-27 16:22:47 +00:00
if ( empty( $this->fees ) )
$this->fees = array();
2012-11-27 16:22:47 +00:00
$new_fee = new stdClass();
$new_fee->id = sanitize_title( $name );
$new_fee->name = esc_attr( $name );
$new_fee->amount = (float) esc_attr( $amount );
2012-11-12 14:34:10 +00:00
$new_fee->tax_class = $tax_class;
$new_fee->taxable = $taxable ? true : false;
2012-11-12 17:15:54 +00:00
$new_fee->tax = 0;
2012-11-27 16:22:47 +00:00
$this->fees[] = $new_fee;
}
2012-11-27 16:22:47 +00:00
/**
* get_fees function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return void
*/
public function get_fees() {
return (array) $this->fees;
}
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
*/
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
*/
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
*/
public function get_total() {
2012-05-23 11:30:26 +00:00
return apply_filters( 'woocommerce_cart_total', woocommerce_price( $this->total ) );
2011-12-08 12:50:50 +00:00
}
2012-08-06 12:24:59 +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
*/
public function get_total_ex_tax() {
2012-01-09 18:08:56 +00:00
$total = $this->total - $this->tax_total - $this->shipping_tax_total;
2012-03-20 13:22:35 +00:00
if ( $total < 0 ) $total = 0;
2012-05-23 11:30:26 +00:00
return apply_filters( 'woocommerce_cart_total_ex_tax', woocommerce_price( $total ) );
}
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
*/
public function get_cart_total() {
2012-03-20 13:22:35 +00:00
if ( ! $this->prices_include_tax ) {
2012-05-24 05:58:19 +00:00
$cart_contents_total = woocommerce_price( $this->cart_contents_total );
2012-03-20 13:22:35 +00:00
} else {
2012-05-24 05:58:19 +00:00
$cart_contents_total = woocommerce_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
*/
public function get_cart_subtotal( $compound = false ) {
2011-12-08 12:50:50 +00:00
global $woocommerce;
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
2012-05-23 02:03:41 +00:00
$cart_subtotal = woocommerce_price( $this->cart_contents_total + $this->shipping_total + $this->get_taxes_total( 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
$cart_subtotal = woocommerce_price( $this->subtotal_ex_tax );
2012-08-06 12:24:59 +00:00
if ( $this->tax_total > 0 && $this->prices_include_tax ) {
2012-05-23 02:03:41 +00:00
$cart_subtotal .= ' <small>' . $woocommerce->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
2012-05-23 02:03:41 +00:00
$cart_subtotal = woocommerce_price( $this->subtotal );
2012-08-06 12:24:59 +00:00
if ( $this->tax_total > 0 && !$this->prices_include_tax ) {
2012-05-23 02:03:41 +00:00
$cart_subtotal .= ' <small>' . $woocommerce->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
/**
* Get the product row price per item.
*
* @params object product
* @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();
return apply_filters( 'woocommerce_cart_product_price', woocommerce_price( $product_price ), $_product );
}
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
*
* @params object product
* @params int quantity
* @return string formatted price
2011-12-08 12:50:50 +00:00
*/
public function get_product_subtotal( $_product, $quantity ) {
2011-12-08 12:50:50 +00:00
global $woocommerce;
2012-08-06 12:24:59 +00:00
2011-12-08 12:50:50 +00:00
$price = $_product->get_price();
$taxable = $_product->is_taxable();
2011-12-29 01:18:59 +00:00
$base_tax_rates = $this->tax->get_shop_base_rate( $_product->tax_class );
$tax_rates = $this->tax->get_rates( $_product->get_tax_class() ); // This will get the base rate unless we're on the checkout page
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 );
$product_subtotal = woocommerce_price( $row_price );
2012-08-06 12:24:59 +00:00
if ( $this->prices_include_tax && $this->tax_total > 0 )
2012-12-03 16:36:54 +00:00
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->ex_tax_or_vat() . '</small>';
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 );
$product_subtotal = woocommerce_price( $row_price );
2012-08-06 12:24:59 +00:00
if ( ! $this->prices_include_tax && $this->tax_total > 0 )
2012-12-03 16:36:54 +00:00
$product_subtotal .= ' <small class="tax_label">' . $woocommerce->countries->inc_tax_or_vat() . '</small>';
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;
$product_subtotal = woocommerce_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
*/
public function get_cart_tax() {
2011-12-23 18:15:46 +00:00
$return = false;
2011-12-08 12:50:50 +00:00
$cart_total_tax = $this->tax_total + $this->shipping_tax_total;
2012-03-20 13:22:35 +00:00
if ( $cart_total_tax > 0 ) $return = woocommerce_price( $cart_total_tax );
return apply_filters( 'woocommerce_get_cart_tax', $return );
2011-12-30 21:11:18 +00:00
}
2012-08-06 12:24:59 +00:00
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.
2012-03-20 13:22:35 +00:00
*
* @return float price
2011-12-30 21:11:18 +00:00
*/
public function get_taxes_total( $compound = 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;
$total += $tax;
2012-03-20 13:22:35 +00:00
}
2011-12-30 21:11:18 +00:00
return $total;
}
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
*/
public function get_discounts_before_tax() {
2012-03-20 13:22:35 +00:00
if ( $this->discount_cart ) {
2012-08-06 12:24:59 +00:00
$discounts_before_tax = woocommerce_price( $this->discount_cart );
} else {
$discounts_before_tax = false;
2012-03-20 13:22:35 +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
*/
public function get_discounts_after_tax() {
2012-03-20 13:22:35 +00:00
if ( $this->discount_total ) {
2012-08-06 12:24:59 +00:00
$discounts_after_tax = woocommerce_price( $this->discount_total );
} else {
$discounts_after_tax = false;
2012-03-20 13:22:35 +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
*/
public function get_total_discount() {
2012-03-20 13:22:35 +00:00
if ( $this->discount_total || $this->discount_cart ) {
$total_discount = woocommerce_price( $this->discount_total + $this->discount_cart );
} else {
$total_discount = false;
2012-03-20 13:22:35 +00:00
}
return apply_filters( 'woocommerce_cart_total_discount', $total_discount, $this );
}
2011-08-09 15:16:18 +00:00
}