Merge pull request #17599 from JPry/allow_clone_cart

Allow for cloning the WC_Cart object
This commit is contained in:
Mike Jolley 2017-11-10 12:02:59 +00:00 committed by GitHub
commit 78466a6665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 0 deletions

View File

@ -63,6 +63,12 @@ final class WC_Cart_Fees {
}
$this->cart = $cart;
}
/**
* Register methods for this object on the appropriate WordPress hooks.
*/
public function init() {
add_action( 'woocommerce_cart_emptied', array( $this, 'remove_all_fees' ) );
add_action( 'woocommerce_cart_reset', array( $this, 'remove_all_fees' ) );
}

View File

@ -38,7 +38,12 @@ final class WC_Cart_Session {
}
$this->cart = $cart;
}
/**
* Register methods for this object on the appropriate WordPress hooks.
*/
public function init() {
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 );

View File

@ -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->init();
$this->fees_api->init();
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 );
@ -118,6 +122,16 @@ class WC_Cart extends WC_Legacy_Cart {
add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 );
}
/**
* When cloning, ensure object properties are handled.
*
* These properties store a reference to the cart, so we use new instead of clone.
*/
public function __clone() {
$this->session = new WC_Cart_Session( $this );
$this->fees_api = new WC_Cart_Fees( $this );
}
/*
|--------------------------------------------------------------------------
| Getters.

View File

@ -15,6 +15,7 @@ class WC_Tests_WC_Cart_Fees extends WC_Unit_Test_Case {
public function test_set_get_remove_fees() {
$cart_fees = new WC_Cart_Fees( wc()->cart );
$cart_fees->init();
// Test add_fee.
$args = array(