Merge pull request #23112 from woocommerce/fix/persistent-cart-filter
Persistent cart improvements
This commit is contained in:
commit
3fed0d7cbc
|
@ -50,7 +50,12 @@ final class WC_Cart_Session {
|
||||||
add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_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_cart_loaded_from_session', array( $this, 'set_session' ) );
|
||||||
add_action( 'woocommerce_removed_coupon', 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.
|
// Cookie events - cart cookies need to be set before headers are sent.
|
||||||
add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
|
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.
|
* Delete the persistent cart permanently.
|
||||||
*/
|
*/
|
||||||
public function persistent_cart_destroy() {
|
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() );
|
delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 string $cart_item_key contains the id of the cart item.
|
||||||
* @param int $quantity contains the quantity of the 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
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
|
public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
|
||||||
if ( 0 === $quantity || $quantity < 0 ) {
|
if ( 0 === $quantity || $quantity < 0 ) {
|
||||||
do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key, $this );
|
// If we're setting qty to 0 we're removing the item from the cart.
|
||||||
unset( $this->cart_contents[ $cart_item_key ] );
|
return $this->remove_cart_item( $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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 ) {
|
if ( $refresh_totals ) {
|
||||||
$this->calculate_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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue