From 0cca83ff83cd93b0af30a148ad460b4bfdb7457c Mon Sep 17 00:00:00 2001 From: Jeremy Pry Date: Fri, 3 Nov 2017 15:49:45 -0400 Subject: [PATCH] Add separate methods to register hooks This facilitates cloning the cart without registering additonal hooks unnecessarily. --- includes/class-wc-cart-fees.php | 6 ++++++ includes/class-wc-cart-session.php | 5 +++++ includes/class-wc-cart.php | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/includes/class-wc-cart-fees.php b/includes/class-wc-cart-fees.php index 1feecadcce3..7f37c9217fd 100644 --- a/includes/class-wc-cart-fees.php +++ b/includes/class-wc-cart-fees.php @@ -63,6 +63,12 @@ final class WC_Cart_Fees { } $this->cart = $cart; + } + + /** + * Register methods for this object on the appropriate WordPress hooks. + */ + public function register_hooks() { add_action( 'woocommerce_cart_emptied', array( $this, 'remove_all_fees' ) ); add_action( 'woocommerce_cart_reset', array( $this, 'remove_all_fees' ) ); } diff --git a/includes/class-wc-cart-session.php b/includes/class-wc-cart-session.php index 659e27a7f64..93e190b601e 100644 --- a/includes/class-wc-cart-session.php +++ b/includes/class-wc-cart-session.php @@ -38,7 +38,12 @@ final class WC_Cart_Session { } $this->cart = $cart; + } + /** + * Register methods for this object on the appropriate WordPress hooks. + */ + public function register_hooks() { add_action( 'wp_loaded', array( $this, 'get_cart_from_session' ) ); add_action( 'woocommerce_cart_emptied', array( $this, 'destroy_cart_session' ) ); add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index a9170be2b97..a5d1b762721 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -109,6 +109,10 @@ class WC_Cart extends WC_Legacy_Cart { $this->fees_api = new WC_Cart_Fees( $this ); $this->tax_display_cart = get_option( 'woocommerce_tax_display_cart' ); + // Register hooks for the objects. + $this->session->register_hooks(); + $this->fees_api->register_hooks(); + add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 ); add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 ); add_action( 'woocommerce_cart_item_removed', array( $this, 'calculate_totals' ), 20, 0 );