From 940c2369b0639ed555ddba2b9bb6a3d9c33b3723 Mon Sep 17 00:00:00 2001 From: Nikki DelRosso Date: Thu, 14 May 2015 14:18:53 -0700 Subject: [PATCH] Added method to check if cart is empty --- includes/class-wc-ajax.php | 2 +- includes/class-wc-cart.php | 19 ++++++++++++++----- includes/class-wc-checkout.php | 2 +- includes/class-wc-coupon.php | 16 ++++++++-------- includes/class-wc-form-handler.php | 4 ++-- .../shortcodes/class-wc-shortcode-cart.php | 2 +- .../class-wc-shortcode-checkout.php | 2 +- includes/wc-template-functions.php | 2 +- templates/cart/mini-cart.php | 4 ++-- 9 files changed, 31 insertions(+), 22 deletions(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index ead190bad7d..4d8241e452c 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -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' => '
' . __( 'Sorry, your session has expired.', 'woocommerce' ) . ' ' . __( 'Return to homepage', 'woocommerce' ) . '
' diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 28cbaf33d37..637d125d0dc 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -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; } diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 7e496496941..80144001a6f 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -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. Return to homepage', 'woocommerce' ), home_url() ) ); } diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index d103c53db59..d19a506f2fd 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -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" ) ); diff --git a/includes/class-wc-form-handler.php b/includes/class-wc-form-handler.php index fb2044f6502..e8d5ce6a92b 100644 --- a/includes/class-wc-form-handler.php +++ b/includes/class-wc-form-handler.php @@ -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']; diff --git a/includes/shortcodes/class-wc-shortcode-cart.php b/includes/shortcodes/class-wc-shortcode-cart.php index 43adc63965f..3848c767b0e 100644 --- a/includes/shortcodes/class-wc-shortcode-cart.php +++ b/includes/shortcodes/class-wc-shortcode-cart.php @@ -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' ); diff --git a/includes/shortcodes/class-wc-shortcode-checkout.php b/includes/shortcodes/class-wc-shortcode-checkout.php index 341844a6ae5..43eddb9e8c4 100644 --- a/includes/shortcodes/class-wc-shortcode-checkout.php +++ b/includes/shortcodes/class-wc-shortcode-checkout.php @@ -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; } diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index 58074dd0415..f8b3f64ace0 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -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; } diff --git a/templates/cart/mini-cart.php b/templates/cart/mini-cart.php index 9df49040a15..d901e800c89 100644 --- a/templates/cart/mini-cart.php +++ b/templates/cart/mini-cart.php @@ -19,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) { -cart->get_cart() ) > 0 ) : ?> +cart->is_empty() ) : ?>

: cart->get_cart_subtotal(); ?>