2011-08-09 15:16:18 +00:00
< ? php
/**
2011-08-10 17:11:11 +00:00
* WooCommerce cart
2011-08-09 15:16:18 +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 .
2011-08-09 15:16:18 +00:00
* The cart class also has a price calculation function which calls upon other classes to calcualte totals .
*
2011-08-10 17:11:11 +00:00
* @ class woocommerce_cart
* @ package WooCommerce
* @ category Class
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
class woocommerce_cart {
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
var $cart_contents_total ;
var $cart_contents_total_ex_tax ;
var $cart_contents_weight ;
var $cart_contents_count ;
var $cart_contents_tax ;
var $cart_contents ;
var $total ;
var $subtotal ;
var $subtotal_ex_tax ;
var $tax_total ;
var $discount_total ;
var $shipping_total ;
var $shipping_tax_total ;
var $applied_coupons ;
2011-08-09 15:16:18 +00:00
/** constructor */
function __construct () {
2011-09-06 11:11:22 +00:00
add_action ( 'init' , array ( & $this , 'init' ), 1 );
2011-08-09 15:16:18 +00:00
}
2011-09-06 11:11:22 +00:00
function init () {
$this -> applied_coupons = array ();
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> get_cart_from_session ();
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
if ( isset ( $_SESSION [ 'coupons' ]) ) $this -> applied_coupons = $_SESSION [ 'coupons' ];
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> calculate_totals ();
2011-08-09 15:16:18 +00:00
}
/** Gets the cart data from the PHP session */
function get_cart_from_session () {
if ( isset ( $_SESSION [ 'cart' ]) && is_array ( $_SESSION [ 'cart' ]) ) :
$cart = $_SESSION [ 'cart' ];
foreach ( $cart as $values ) :
if ( $values [ 'variation_id' ] > 0 ) :
2011-08-10 17:11:11 +00:00
$_product = & new woocommerce_product_variation ( $values [ 'variation_id' ]);
2011-08-09 15:16:18 +00:00
else :
2011-08-10 17:11:11 +00:00
$_product = & new woocommerce_product ( $values [ 'product_id' ]);
2011-08-09 15:16:18 +00:00
endif ;
2011-08-22 12:26:17 +00:00
if ( $_product -> exists && $values [ 'quantity' ] > 0 ) :
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> cart_contents [] = array (
2011-08-09 15:16:18 +00:00
'product_id' => $values [ 'product_id' ],
'variation_id' => $values [ 'variation_id' ],
'variation' => $values [ 'variation' ],
'quantity' => $values [ 'quantity' ],
'data' => $_product
);
endif ;
endforeach ;
else :
2011-09-06 11:11:22 +00:00
$this -> cart_contents = array ();
2011-08-09 15:16:18 +00:00
endif ;
2011-09-06 11:11:22 +00:00
if ( ! is_array ( $this -> cart_contents )) $this -> cart_contents = array ();
2011-08-09 15:16:18 +00:00
}
/** sets the php session data for the cart and coupon */
function set_session () {
$cart = array ();
2011-09-06 11:11:22 +00:00
$_SESSION [ 'cart' ] = $this -> cart_contents ;
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$_SESSION [ 'coupons' ] = $this -> applied_coupons ;
$this -> calculate_totals ();
2011-08-09 15:16:18 +00:00
}
/** Empty the cart */
function empty_cart () {
unset ( $_SESSION [ 'cart' ]);
unset ( $_SESSION [ 'coupons' ]);
}
2011-08-22 11:57:50 +00:00
/**
* Check if product is in the cart and return cart item key
*
* @ param int $product_id
* @ param int $variation_id optional variation id
* @ param array $variation array of attributre values
* @ return int | null
*/
function find_product_in_cart ( $product_id , $variation_id , $variation = array ()) {
2011-09-06 11:11:22 +00:00
foreach ( $this -> cart_contents as $cart_item_key => $cart_item ) :
2011-08-22 11:57:50 +00:00
if ( empty ( $variation_id ) && $cart_item [ 'product_id' ] == $product_id ) :
return $cart_item_key ;
elseif ( $cart_item [ 'product_id' ] == $product_id && $cart_item [ 'variation_id' ] == $variation_id ) :
if ( $variation == $cart_item [ 'variation' ]) :
2011-08-09 15:16:18 +00:00
return $cart_item_key ;
2011-08-22 11:57:50 +00:00
endif ;
endif ;
endforeach ;
return NULL ;
}
2011-08-09 15:16:18 +00:00
/**
* Add a product to the cart
*
* @ 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
2011-08-22 12:02:45 +00:00
* @ param int variation_id
* @ param array variation attribute values
2011-08-09 15:16:18 +00:00
*/
function add_to_cart ( $product_id , $quantity = 1 , $variation = '' , $variation_id = '' ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-09 15:16:18 +00:00
2011-08-22 12:02:45 +00:00
if ( $quantity < 1 ) $quantity = 1 ;
2011-09-06 11:11:22 +00:00
$found_cart_item_key = $this -> find_product_in_cart ( $product_id , $variation_id , $variation );
2011-08-09 15:16:18 +00:00
2011-08-30 11:24:28 +00:00
if ( $variation_id > 0 ) :
$product_data = & new woocommerce_product_variation ( $variation_id );
else :
$product_data = & new woocommerce_product ( $product_id );
endif ;
2011-08-27 20:07:16 +00:00
// Price set check
2011-08-30 11:24:28 +00:00
if ( $product_data -> get_price () === '' ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'This product cannot be purchased - the price is not yet announced' , 'woothemes' ) );
2011-08-27 20:07:16 +00:00
return false ;
2011-08-30 11:24:28 +00:00
endif ;
2011-08-27 20:07:16 +00:00
2011-08-30 11:24:28 +00:00
// Stock check - only check if we're managing stock and backorders are not allowed
if ( ! $product_data -> has_enough_stock ( $quantity ) ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( sprintf ( __ ( 'You cannot add that amount to the cart since there is not enough stock. We have %s in stock.' , 'woothemes' ), $product_data -> get_stock_quantity () ));
2011-08-30 11:24:28 +00:00
return false ;
elseif ( ! $product_data -> is_in_stock () ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'You cannot add that product to the cart since the product is out of stock.' , 'woothemes' ) );
2011-08-30 11:24:28 +00:00
return false ;
endif ;
// Add it
2011-08-09 15:16:18 +00:00
if ( is_numeric ( $found_cart_item_key )) :
2011-09-06 11:11:22 +00:00
$quantity = $quantity + $this -> cart_contents [ $found_cart_item_key ][ 'quantity' ];
2011-08-09 15:16:18 +00:00
2011-08-30 11:24:28 +00:00
// Stock check - this time accounting for whats already in-cart
if ( ! $product_data -> has_enough_stock ( $quantity ) ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( sprintf ( __ ( 'You cannot add that amount to the cart since there is not enough stock. We have %s in stock and you already have %s in your cart.' , 'woothemes' ), $product_data -> get_stock_quantity (), $this -> cart_contents [ $found_cart_item_key ][ 'quantity' ] ));
2011-08-30 11:24:28 +00:00
return false ;
elseif ( ! $product_data -> is_in_stock () ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'You cannot add that product to the cart since the product is out of stock.' , 'woothemes' ) );
2011-08-30 11:24:28 +00:00
return false ;
endif ;
2011-09-06 11:11:22 +00:00
$this -> cart_contents [ $found_cart_item_key ][ 'quantity' ] = $quantity ;
2011-08-09 15:16:18 +00:00
else :
2011-09-06 11:11:22 +00:00
$cart_item_key = sizeof ( $this -> cart_contents );
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> cart_contents [ $cart_item_key ] = array (
2011-08-09 15:16:18 +00:00
'product_id' => $product_id ,
'variation_id' => $variation_id ,
'variation' => $variation ,
'quantity' => $quantity ,
2011-08-27 20:07:16 +00:00
'data' => $product_data
2011-08-09 15:16:18 +00:00
);
endif ;
2011-09-06 11:11:22 +00:00
$this -> set_session ();
2011-08-30 11:24:28 +00:00
return true ;
2011-08-09 15:16:18 +00:00
}
/**
* Set the quantity for an item in the cart
*
* @ param string cart_item_key contains the id of the cart item
* @ param string quantity contains the quantity of the item
*/
function set_quantity ( $cart_item , $quantity = 1 ) {
if ( $quantity == 0 || $quantity < 0 ) :
2011-09-06 11:11:22 +00:00
unset ( $this -> cart_contents [ $cart_item ]);
2011-08-09 15:16:18 +00:00
else :
2011-09-06 11:11:22 +00:00
$this -> cart_contents [ $cart_item ][ 'quantity' ] = $quantity ;
2011-08-09 15:16:18 +00:00
endif ;
2011-09-06 11:11:22 +00:00
$this -> set_session ();
2011-08-09 15:16:18 +00:00
}
/**
* Returns the contents of the cart
*
* @ return array cart_contents
*/
function get_cart () {
2011-09-06 11:11:22 +00:00
return $this -> cart_contents ;
2011-08-09 15:16:18 +00:00
}
/**
* Gets cross sells based on the items in the cart
*
* @ return array cross_sells item ids of cross sells
*/
function get_cross_sells () {
$cross_sells = array ();
$in_cart = array ();
2011-09-06 11:11:22 +00:00
if ( sizeof ( $this -> cart_contents ) > 0 ) : foreach ( $this -> cart_contents as $cart_item_key => $values ) :
2011-08-09 15:16:18 +00:00
if ( $values [ 'quantity' ] > 0 ) :
$cross_sells = array_merge ( $values [ 'data' ] -> get_cross_sells (), $cross_sells );
$in_cart [] = $values [ 'product_id' ];
endif ;
endforeach ; endif ;
$cross_sells = array_diff ( $cross_sells , $in_cart );
return $cross_sells ;
}
/** gets the url to the cart page */
function get_cart_url () {
2011-08-10 17:11:11 +00:00
$cart_page_id = get_option ( 'woocommerce_cart_page_id' );
2011-08-09 15:16:18 +00:00
if ( $cart_page_id ) return get_permalink ( $cart_page_id );
}
/** gets the url to the checkout page */
function get_checkout_url () {
2011-08-10 17:11:11 +00:00
$checkout_page_id = get_option ( 'woocommerce_checkout_page_id' );
2011-08-09 15:16:18 +00:00
if ( $checkout_page_id ) :
if ( is_ssl ()) return str_replace ( 'http:' , 'https:' , get_permalink ( $checkout_page_id ));
return get_permalink ( $checkout_page_id );
endif ;
}
/** gets the url to remove an item from the cart */
function get_remove_url ( $cart_item_key ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-10 17:11:11 +00:00
$cart_page_id = get_option ( 'woocommerce_cart_page_id' );
2011-09-06 11:11:22 +00:00
if ( $cart_page_id ) return $woocommerce -> nonce_url ( 'cart' , add_query_arg ( 'remove_item' , $cart_item_key , get_permalink ( $cart_page_id )));
2011-08-09 15:16:18 +00:00
}
/** looks through the cart to see if shipping is actually required */
function needs_shipping () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
if ( ! $woocommerce -> shipping -> enabled ) return false ;
if ( ! is_array ( $this -> cart_contents )) return false ;
2011-08-09 15:16:18 +00:00
$needs_shipping = false ;
2011-09-06 11:11:22 +00:00
foreach ( $this -> cart_contents as $cart_item_key => $values ) :
2011-08-09 15:16:18 +00:00
$_product = $values [ 'data' ];
if ( $_product -> is_type ( 'simple' ) || $_product -> is_type ( 'variable' ) ) :
$needs_shipping = true ;
endif ;
endforeach ;
return $needs_shipping ;
}
/** Sees if we need a shipping address */
function ship_to_billing_address_only () {
2011-08-10 17:11:11 +00:00
$ship_to_billing_address_only = get_option ( 'woocommerce_ship_to_billing_address_only' );
2011-08-09 15:16:18 +00:00
if ( $ship_to_billing_address_only == 'yes' ) return true ;
return false ;
}
/** looks at the totals to see if payment is actually required */
function needs_payment () {
2011-09-06 11:11:22 +00:00
if ( $this -> total > 0 ) return true ;
2011-08-09 15:16:18 +00:00
return false ;
}
/** looks through the cart to check each item is in stock */
function check_cart_item_stock () {
$error = new WP_Error ();
2011-09-06 11:11:22 +00:00
foreach ( $this -> cart_contents as $cart_item_key => $values ) :
2011-08-09 15:16:18 +00:00
$_product = $values [ 'data' ];
if ( $_product -> managing_stock ()) :
if ( $_product -> is_in_stock () && $_product -> has_enough_stock ( $values [ 'quantity' ] )) :
// :)
else :
2011-08-17 23:42:07 +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.' , 'woothemes' ), $_product -> get_title (), $_product -> stock ) );
2011-08-09 15:16:18 +00:00
return $error ;
endif ;
else :
if ( ! $_product -> is_in_stock ()) :
2011-08-10 17:11:11 +00:00
$error -> add ( 'out-of-stock' , sprintf ( __ ( 'Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologise for any inconvenience caused.' , 'woothemes' ), $_product -> get_title () ) );
2011-08-09 15:16:18 +00:00
return $error ;
endif ;
endif ;
endforeach ;
return true ;
}
/** calculate totals for the items in the cart */
2011-09-06 11:11:22 +00:00
function calculate_totals () {
global $woocommerce ;
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
$_tax = & new woocommerce_tax ();
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> total = 0 ;
$this -> cart_contents_total = 0 ;
$this -> cart_contents_total_ex_tax = 0 ;
$this -> cart_contents_weight = 0 ;
$this -> cart_contents_count = 0 ;
$this -> cart_contents_tax = 0 ;
$this -> tax_total = 0 ;
$this -> shipping_tax_total = 0 ;
$this -> subtotal = 0 ;
$this -> subtotal_ex_tax = 0 ;
$this -> discount_total = 0 ;
$this -> shipping_total = 0 ;
if ( sizeof ( $this -> cart_contents ) > 0 ) : foreach ( $this -> cart_contents as $cart_item_key => $values ) :
2011-08-09 15:16:18 +00:00
$_product = $values [ 'data' ];
if ( $_product -> exists () && $values [ 'quantity' ] > 0 ) :
2011-09-06 11:11:22 +00:00
$this -> cart_contents_count = $this -> cart_contents_count + $values [ 'quantity' ];
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> cart_contents_weight = $this -> cart_contents_weight + ( $_product -> get_weight () * $values [ 'quantity' ]);
2011-08-09 15:16:18 +00:00
2011-08-11 22:39:02 +00:00
$total_item_price = $_product -> get_price () * $values [ 'quantity' ];
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_calc_taxes' ) == 'yes' ) :
2011-08-09 15:16:18 +00:00
if ( $_product -> is_taxable () ) :
2011-10-07 22:24:11 +00:00
$rate = $_tax -> get_rate ( $_product -> get_tax_class () );
2011-08-09 15:16:18 +00:00
2011-10-08 10:39:26 +00:00
$tax_amount = $_tax -> calc_tax ( $_product -> get_price (), $rate , true ) * $values [ 'quantity' ];
2011-08-09 15:16:18 +00:00
2011-10-08 10:39:26 +00:00
/**
* Checkout calculations when customer is OUTSIDE the shop base country and price INCLUDE tax
*/
2011-09-06 11:11:22 +00:00
if ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' && $woocommerce -> customer -> is_customer_outside_base () && defined ( 'WOOCOMMERCE_CHECKOUT' ) && WOOCOMMERCE_CHECKOUT ) :
2011-10-08 10:39:26 +00:00
// Get the base rate first
$base_rate = $_tax -> get_shop_base_rate ( $_product -> tax_class );
2011-08-09 15:16:18 +00:00
// Calc tax for base country
2011-08-11 22:39:02 +00:00
$base_tax_amount = $_tax -> calc_tax ( $_product -> get_price (), $base_rate , true );
2011-08-09 15:16:18 +00:00
// Now calc tax for user county (which now excludes tax)
2011-08-11 22:39:02 +00:00
$tax_amount = $_tax -> calc_tax ( ( $_product -> get_price () - $base_tax_amount ), $rate , false );
$tax_amount = $tax_amount * $values [ 'quantity' ];
2011-08-09 15:16:18 +00:00
// Finally, update $total_item_price to reflect tax amounts
2011-08-11 22:39:02 +00:00
$total_item_price = ( $total_item_price - ( $base_tax_amount * $values [ 'quantity' ]) + $tax_amount );
2011-08-09 15:16:18 +00:00
2011-10-08 10:39:26 +00:00
/**
* Checkout calculations when customer is INSIDE the shop base country and price INCLUDE tax
*/
elseif ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' && $_product -> get_tax_class () !== $_product -> tax_class ) :
// Calc tax for original rate
$original_tax_amount = $_tax -> calc_tax ( $_product -> get_price (), $_tax -> get_rate ( $_product -> tax_class ), true );
// Now calc tax for new rate (which now excludes tax)
$tax_amount = $_tax -> calc_tax ( ( $_product -> get_price () - $original_tax_amount ), $rate , false );
$tax_amount = $tax_amount * $values [ 'quantity' ];
$total_item_price = ( $total_item_price - ( $original_tax_amount * $values [ 'quantity' ]) + $tax_amount );
2011-08-09 15:16:18 +00:00
endif ;
endif ;
endif ;
2011-08-11 22:39:02 +00:00
$tax_amount = ( isset ( $tax_amount ) ? $tax_amount : 0 );
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> cart_contents_tax = $this -> cart_contents_tax + $tax_amount ;
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$this -> cart_contents_total = $this -> cart_contents_total + $total_item_price ;
$this -> cart_contents_total_ex_tax = $this -> cart_contents_total_ex_tax + ( $_product -> get_price_excluding_tax () * $values [ 'quantity' ]);
2011-08-09 15:16:18 +00:00
// Product Discounts
2011-09-06 11:11:22 +00:00
if ( $this -> applied_coupons ) foreach ( $this -> applied_coupons as $code ) :
$coupon = & new woocommerce_coupon ( $code );
if ( $coupon -> type == 'fixed_product' && in_array ( $values [ 'product_id' ], $coupon -> product_ids )) :
$this -> discount_total = $this -> discount_total + ( $coupon -> amount * $values [ 'quantity' ] );
2011-08-09 15:16:18 +00:00
endif ;
endforeach ;
endif ;
endforeach ; endif ;
2011-10-16 11:34:28 +00:00
// Calculate final totals
$this -> tax_total = $this -> cart_contents_tax ; // Tax Total
$this -> subtotal_ex_tax = $this -> cart_contents_total_ex_tax ; // Subtotal without tax
$this -> subtotal = $this -> cart_contents_total ; // Subtotal
2011-08-09 15:16:18 +00:00
// Cart Discounts
2011-09-06 11:11:22 +00:00
if ( $this -> applied_coupons ) foreach ( $this -> applied_coupons as $code ) :
$coupon = & new woocommerce_coupon ( $code );
if ( $coupon -> is_valid ()) :
if ( $coupon -> type == 'fixed_cart' ) :
$this -> discount_total = $this -> discount_total + $coupon -> amount ;
elseif ( $coupon -> type == 'percent' ) :
$this -> discount_total = $this -> discount_total + ( $this -> subtotal / 100 ) * $coupon -> amount ;
2011-08-09 15:16:18 +00:00
endif ;
endif ;
endforeach ;
2011-10-16 11:34:28 +00:00
// Cart Shipping
if ( $this -> needs_shipping ()) $woocommerce -> shipping -> calculate_shipping (); else $woocommerce -> shipping -> reset_shipping ();
$this -> shipping_total = $woocommerce -> shipping -> shipping_total ; // Shipping Total
$this -> shipping_tax_total = $woocommerce -> shipping -> shipping_tax ; // Shipping Tax
// VAT excemption done at this point - so all totals are correct before exemption
2011-09-14 14:55:03 +00:00
if ( $woocommerce -> customer -> is_vat_exempt ()) :
$this -> shipping_tax_total = 0 ;
$this -> tax_total = 0 ;
endif ;
2011-10-03 11:27:58 +00:00
// Allow plugins to hook and alter totals before final total is calculated
do_action ( 'woocommerce_calculate_totals' , $this );
2011-09-14 14:55:03 +00:00
2011-10-16 11:34:28 +00:00
// Grand Total
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ) :
2011-09-06 11:11:22 +00:00
$this -> total = $this -> subtotal + $this -> shipping_tax_total - $this -> discount_total + $woocommerce -> shipping -> shipping_total ;
2011-08-09 15:16:18 +00:00
else :
2011-09-06 11:11:22 +00:00
$this -> total = $this -> subtotal + $this -> tax_total + $this -> shipping_tax_total - $this -> discount_total + $woocommerce -> shipping -> shipping_total ;
2011-08-09 15:16:18 +00:00
endif ;
2011-09-06 11:11:22 +00:00
if ( $this -> total < 0 ) $this -> total = 0 ;
2011-08-09 15:16:18 +00:00
}
/** gets the total (after calculation) */
function get_total () {
2011-09-06 11:11:22 +00:00
return woocommerce_price ( $this -> total );
2011-08-09 15:16:18 +00:00
}
/** gets the cart contens total (after calculation) */
function get_cart_total () {
2011-09-06 11:11:22 +00:00
return woocommerce_price ( $this -> cart_contents_total );
2011-08-09 15:16:18 +00:00
}
/** gets the sub total (after calculation) */
function get_cart_subtotal () {
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_display_totals_tax' ) == 'excluding' || ( defined ( 'WOOCOMMERCE_CHECKOUT' ) && WOOCOMMERCE_CHECKOUT )) :
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ) :
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$return = woocommerce_price ( $this -> subtotal - $this -> tax_total );
2011-08-09 15:16:18 +00:00
else :
2011-09-06 11:11:22 +00:00
$return = woocommerce_price ( $this -> subtotal );
2011-08-09 15:16:18 +00:00
endif ;
2011-09-06 11:11:22 +00:00
if ( $this -> tax_total > 0 ) :
2011-08-10 17:11:11 +00:00
$return .= __ ( ' <small>(ex. tax)</small>' , 'woothemes' );
2011-08-09 15:16:18 +00:00
endif ;
return $return ;
else :
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ) :
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$return = woocommerce_price ( $this -> subtotal );
2011-08-09 15:16:18 +00:00
else :
2011-09-06 11:11:22 +00:00
$return = woocommerce_price ( $this -> subtotal + $this -> tax_total );
2011-08-09 15:16:18 +00:00
endif ;
2011-09-06 11:11:22 +00:00
if ( $this -> tax_total > 0 ) :
2011-08-10 17:11:11 +00:00
$return .= __ ( ' <small>(inc. tax)</small>' , 'woothemes' );
2011-08-09 15:16:18 +00:00
endif ;
return $return ;
endif ;
}
/** gets the cart tax (after calculation) */
function get_cart_tax () {
2011-09-06 11:11:22 +00:00
$cart_total_tax = $this -> tax_total + $this -> shipping_tax_total ;
2011-08-10 17:11:11 +00:00
if ( $cart_total_tax > 0 ) return woocommerce_price ( $cart_total_tax );
2011-08-09 15:16:18 +00:00
return false ;
}
/** gets the shipping total (after calculation) */
function get_cart_shipping_total () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
if ( isset ( $woocommerce -> shipping -> shipping_label )) :
if ( $woocommerce -> shipping -> shipping_total > 0 ) :
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_display_totals_tax' ) == 'excluding' ) :
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
$return = woocommerce_price ( $woocommerce -> shipping -> shipping_total );
if ( $this -> shipping_tax_total > 0 ) :
2011-08-10 17:11:11 +00:00
$return .= __ ( ' <small>(ex. tax)</small>' , 'woothemes' );
2011-08-09 15:16:18 +00:00
endif ;
return $return ;
else :
2011-09-06 11:11:22 +00:00
$return = woocommerce_price ( $woocommerce -> shipping -> shipping_total + $woocommerce -> shipping -> shipping_tax );
if ( $this -> shipping_tax_total > 0 ) :
2011-08-10 17:11:11 +00:00
$return .= __ ( ' <small>(inc. tax)</small>' , 'woothemes' );
2011-08-09 15:16:18 +00:00
endif ;
return $return ;
endif ;
else :
2011-08-10 17:11:11 +00:00
return __ ( 'Free!' , 'woothemes' );
2011-08-09 15:16:18 +00:00
endif ;
endif ;
}
/** gets title of the chosen shipping method */
function get_cart_shipping_title () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
if ( isset ( $woocommerce -> shipping -> shipping_label )) :
return __ ( 'via ' , 'woothemes' ) . $woocommerce -> shipping -> shipping_label ;
2011-08-09 15:16:18 +00:00
endif ;
return false ;
}
/**
* Applies a coupon code
*
* @ param string code The code to apply
* @ return bool True if the coupon is applied , false if it does not exist or cannot be applied
*/
function add_discount ( $coupon_code ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
$the_coupon = & new woocommerce_coupon ( $coupon_code );
2011-08-09 15:16:18 +00:00
2011-09-06 11:11:22 +00:00
if ( $the_coupon -> id ) :
2011-08-09 15:16:18 +00:00
// Check if applied
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> cart -> has_discount ( $coupon_code )) :
$woocommerce -> add_error ( __ ( 'Discount code already applied!' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
return false ;
endif ;
// Check it can be used with cart
2011-09-06 11:11:22 +00:00
if ( ! $the_coupon -> is_valid ()) :
$woocommerce -> add_error ( __ ( 'Invalid coupon.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
return false ;
endif ;
// If its individual use then remove other coupons
2011-10-03 11:21:01 +00:00
if ( $the_coupon -> individual_use == 'yes' ) :
2011-09-06 11:11:22 +00:00
$this -> applied_coupons = array ();
2011-08-09 15:16:18 +00:00
endif ;
2011-09-06 11:11:22 +00:00
foreach ( $this -> applied_coupons as $code ) :
$coupon = & new woocommerce_coupon ( $code );
2011-10-03 11:21:01 +00:00
if ( $coupon -> individual_use == 'yes' ) :
2011-09-06 11:11:22 +00:00
$this -> applied_coupons = array ();
2011-08-09 15:16:18 +00:00
endif ;
endforeach ;
2011-09-06 11:11:22 +00:00
$this -> applied_coupons [] = $coupon_code ;
$this -> set_session ();
$woocommerce -> add_message ( __ ( 'Discount code applied successfully.' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
return true ;
else :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( __ ( 'Coupon does not exist!' , 'woothemes' ) );
2011-08-09 15:16:18 +00:00
return false ;
endif ;
return false ;
}
/** returns whether or not a discount has been applied */
function has_discount ( $code ) {
2011-09-06 11:11:22 +00:00
if ( in_array ( $code , $this -> applied_coupons )) return true ;
2011-08-09 15:16:18 +00:00
return false ;
}
/** gets the total discount amount */
function get_total_discount () {
2011-09-06 11:11:22 +00:00
if ( $this -> discount_total ) return woocommerce_price ( $this -> discount_total ); else return false ;
2011-08-09 15:16:18 +00:00
}
/** clears the cart/coupon data and re-calcs totals */
function clear_cache () {
2011-09-06 11:11:22 +00:00
$this -> cart_contents = array ();
$this -> applied_coupons = array ();
2011-08-09 15:16:18 +00:00
unset ( $_SESSION [ 'cart' ] );
unset ( $_SESSION [ 'coupons' ] );
2011-09-06 11:11:22 +00:00
$this -> calculate_totals ();
2011-08-09 15:16:18 +00:00
}
}