Merge pull request #23112 from woocommerce/fix/persistent-cart-filter

Persistent cart improvements
This commit is contained in:
Rodrigo Primo 2019-05-21 15:07:55 -03:00 committed by GitHub
commit 3fed0d7cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 10 deletions

View File

@ -50,7 +50,12 @@ final class WC_Cart_Session {
add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_session' ) );
add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'set_session' ) );
add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) );
add_action( 'woocommerce_cart_updated', array( $this, 'persistent_cart_update' ) );
// Persistent cart stored to usermeta.
add_action( 'woocommerce_add_to_cart', array( $this, 'persistent_cart_update' ) );
add_action( 'woocommerce_cart_item_removed', array( $this, 'persistent_cart_update' ) );
add_action( 'woocommerce_cart_item_restored', array( $this, 'persistent_cart_update' ) );
add_action( 'woocommerce_cart_item_set_quantity', array( $this, 'persistent_cart_update' ) );
// Cookie events - cart cookies need to be set before headers are sent.
add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
@ -249,7 +254,7 @@ final class WC_Cart_Session {
* Delete the persistent cart permanently.
*/
public function persistent_cart_destroy() {
if ( get_current_user_id() ) {
if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() );
}
}

View File

@ -1163,27 +1163,39 @@ class WC_Cart extends WC_Legacy_Cart {
}
/**
* Set the quantity for an item in the cart.
* Set the quantity for an item in the cart using it's key.
*
* @param string $cart_item_key contains the id of the cart item.
* @param int $quantity contains the quantity of the item.
* @param bool $refresh_totals whether or not to calculate totals after setting the new qty.
* @param bool $refresh_totals whether or not to calculate totals after setting the new qty. Can be used to defer calculations if setting quantities in bulk.
* @return bool
*/
public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
if ( 0 === $quantity || $quantity < 0 ) {
do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key, $this );
unset( $this->cart_contents[ $cart_item_key ] );
} else {
$old_quantity = $this->cart_contents[ $cart_item_key ]['quantity'];
$this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;
do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $this );
// If we're setting qty to 0 we're removing the item from the cart.
return $this->remove_cart_item( $cart_item_key );
}
// Update qty.
$old_quantity = $this->cart_contents[ $cart_item_key ]['quantity'];
$this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;
do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $this );
if ( $refresh_totals ) {
$this->calculate_totals();
}
/**
* Fired after qty has been changed.
*
* @since 3.6.0
* @param string $cart_item_key contains the id of the cart item. This may be empty if the cart item does not exist any more.
* @param int $quantity contains the quantity of the item.
* @param WC_Cart $this Cart class.
*/
do_action( 'woocommerce_cart_item_set_quantity', $cart_item_key, $quantity, $this );
return true;
}