Added method to check if cart is empty

This commit is contained in:
Nikki DelRosso 2015-05-14 14:18:53 -07:00
parent a1851d1249
commit 940c2369b0
9 changed files with 31 additions and 22 deletions

View File

@ -250,7 +250,7 @@ class WC_AJAX {
define( 'WOOCOMMERCE_CHECKOUT', true );
}
if ( 0 == sizeof( WC()->cart->get_cart() ) ) {
if ( WC()->cart->is_empty() ) {
$data = array(
'fragments' => apply_filters( 'woocommerce_update_order_review_fragments', array(
'form.woocommerce-checkout' => '<div class="woocommerce-error">' . __( 'Sorry, your session has expired.', 'woocommerce' ) . ' <a href="' . home_url() . '" class="wc-backward">' . __( 'Return to homepage', 'woocommerce' ) . '</a></div>'

View File

@ -170,7 +170,7 @@ class WC_Cart {
*/
public function maybe_set_cart_cookies() {
if ( ! headers_sent() ) {
if ( sizeof( $this->cart_contents ) > 0 ) {
if ( ! $this->is_empty() ) {
$this->set_cart_cookies( true );
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
$this->set_cart_cookies( false );
@ -256,7 +256,7 @@ class WC_Cart {
}
// Queue re-calc if subtotal is not set
if ( ( ! $this->subtotal && sizeof( $this->cart_contents ) > 0 ) || $update_cart_session ) {
if ( ( ! $this->subtotal && ! $this->is_empty() || $update_cart_session ) {
$this->calculate_totals();
}
}
@ -345,6 +345,15 @@ class WC_Cart {
return apply_filters( 'woocommerce_cart_contents_count', $this->cart_contents_count );
}
/**
* Checks if the cart is empty.
*
* @return bool
*/
public function is_empty() {
return 0 === sizeof( $this->get_cart() );
}
/**
* Check all cart items for errors.
*/
@ -603,7 +612,7 @@ class WC_Cart {
public function get_cross_sells() {
$cross_sells = array();
$in_cart = array();
if ( sizeof( $this->get_cart() ) > 0 ) {
if ( ! $this->is_empty() ) {
foreach ( $this->get_cart() as $cart_item_key => $values ) {
if ( $values['quantity'] > 0 ) {
$cross_sells = array_merge( $values['data']->get_cross_sells(), $cross_sells );
@ -947,7 +956,7 @@ class WC_Cart {
}
if ( did_action( 'wp' ) ) {
$this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 );
$this->set_cart_cookies( ! $this->is_empty() );
}
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
@ -1066,7 +1075,7 @@ class WC_Cart {
do_action( 'woocommerce_before_calculate_totals', $this );
if ( sizeof( $this->get_cart() ) == 0 ) {
if ( $this->is_empty() ) {
$this->set_session();
return;
}

View File

@ -363,7 +363,7 @@ class WC_Checkout {
do_action( 'woocommerce_before_checkout_process' );
if ( 0 === sizeof( WC()->cart->get_cart() ) ) {
if ( WC()->cart->is_empty() ) {
throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>', 'woocommerce' ), home_url() ) );
}

View File

@ -361,7 +361,7 @@ class WC_Coupon {
private function validate_product_ids() {
if ( sizeof( $this->product_ids ) > 0 ) {
$valid_for_cart = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $this->product_ids ) || in_array( $cart_item['variation_id'], $this->product_ids ) || in_array( $cart_item['data']->get_parent(), $this->product_ids ) ) {
$valid_for_cart = true;
@ -380,7 +380,7 @@ class WC_Coupon {
private function validate_product_categories() {
if ( sizeof( $this->product_categories ) > 0 ) {
$valid_for_cart = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array( "fields" => "ids" ) );
if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) {
@ -401,7 +401,7 @@ class WC_Coupon {
if ( 'yes' === $this->exclude_sale_items && $this->is_type( array( 'fixed_product', 'percent_product' ) ) ) {
$valid_for_cart = false;
$product_ids_on_sale = wc_get_product_ids_on_sale();
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( sizeof( array_intersect( array( absint( $cart_item['product_id'] ), absint( $cart_item['variation_id'] ), $cart_item['data']->get_parent() ), $product_ids_on_sale ) ) === 0 ) {
// not on sale
@ -433,7 +433,7 @@ class WC_Coupon {
// Exclude Products
if ( sizeof( $this->exclude_product_ids ) > 0 ) {
$valid_for_cart = true;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $this->exclude_product_ids ) || in_array( $cart_item['variation_id'], $this->exclude_product_ids ) || in_array( $cart_item['data']->get_parent(), $this->exclude_product_ids ) ) {
$valid_for_cart = false;
@ -452,7 +452,7 @@ class WC_Coupon {
private function validate_cart_excluded_product_categories() {
if ( sizeof( $this->exclude_product_categories ) > 0 ) {
$valid_for_cart = true;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array( "fields" => "ids" ) );
@ -475,7 +475,7 @@ class WC_Coupon {
if ( $this->exclude_sale_items == 'yes' ) {
$valid_for_cart = true;
$product_ids_on_sale = wc_get_product_ids_on_sale();
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( ! empty( $cart_item['variation_id'] ) ) {
if ( in_array( $cart_item['variation_id'], $product_ids_on_sale, true ) ) {
@ -734,7 +734,7 @@ class WC_Coupon {
case self::E_WC_COUPON_EXCLUDED_PRODUCTS:
// Store excluded products that are in cart in $products
$products = array();
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $this->exclude_product_ids ) || in_array( $cart_item['variation_id'], $this->exclude_product_ids ) || in_array( $cart_item['data']->get_parent(), $this->exclude_product_ids ) ) {
$products[] = $cart_item['data']->get_title();
@ -747,7 +747,7 @@ class WC_Coupon {
case self::E_WC_COUPON_EXCLUDED_CATEGORIES:
// Store excluded categories that are in cart in $categories
$categories = array();
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
if ( ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array( "fields" => "ids" ) );

View File

@ -237,7 +237,7 @@ class WC_Form_Handler {
public static function checkout_action() {
if ( isset( $_POST['woocommerce_checkout_place_order'] ) || isset( $_POST['woocommerce_checkout_update_totals'] ) ) {
if ( sizeof( WC()->cart->get_cart() ) == 0 ) {
if ( WC()->cart->is_empty() ) {
wp_redirect( wc_get_page_permalink( 'cart' ) );
exit;
}
@ -410,7 +410,7 @@ class WC_Form_Handler {
$cart_updated = false;
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( WC()->cart->get_cart() ) > 0 && is_array( $cart_totals ) ) {
if ( ! WC()->cart->is_empty() && is_array( $cart_totals ) ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];

View File

@ -70,7 +70,7 @@ class WC_Shortcode_Cart {
// Calc totals
WC()->cart->calculate_totals();
if ( 0 === sizeof( WC()->cart->get_cart() ) ) {
if ( WC()->cart->is_empty() ) {
wc_get_template( 'cart/cart-empty.php' );
} else {
wc_get_template( 'cart/cart.php' );

View File

@ -205,7 +205,7 @@ class WC_Shortcode_Checkout {
wc_print_notices();
// Check cart has contents
if ( sizeof( WC()->cart->get_cart() ) == 0 ) {
if ( WC()->cart->is_empty() ) {
return;
}

View File

@ -29,7 +29,7 @@ function wc_template_redirect() {
}
// When on the checkout with an empty cart, redirect to cart page
elseif ( is_page( wc_get_page_id( 'checkout' ) ) && sizeof( WC()->cart->get_cart() ) == 0 && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ) {
elseif ( is_page( wc_get_page_id( 'checkout' ) ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ) {
wp_redirect( wc_get_page_permalink( 'cart' ) );
exit;
}

View File

@ -19,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<ul class="cart_list product_list_widget <?php echo $args['list_class']; ?>">
<?php if ( sizeof( WC()->cart->get_cart() ) > 0 ) : ?>
<?php if ( ! WC()->cart->is_empty() ) : ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
@ -58,7 +58,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</ul><!-- end product list -->
<?php if ( sizeof( WC()->cart->get_cart() ) > 0 ) : ?>
<?php if ( ! WC()->cart->is_empty() ) : ?>
<p class="total"><strong><?php _e( 'Subtotal', 'woocommerce' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>