Improve cart update performance. Closes #3495.

This commit is contained in:
Mike Jolley 2013-08-19 15:19:44 +01:00
parent 7a132cb1df
commit 44b9c72651
2 changed files with 12 additions and 9 deletions

View File

@ -910,7 +910,7 @@ class WC_Cart {
$new_quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
$this->set_quantity( $cart_item_key, $new_quantity );
$this->set_quantity( $cart_item_key, $new_quantity, false );
} else {
@ -938,10 +938,11 @@ class WC_Cart {
/**
* Set the quantity for an item in the cart.
*
* @param string cart_item_key contains the id of the cart item
* @param string quantity contains the quantity of the item
* @param string cart_item_key contains the id of the cart item
* @param string quantity contains the quantity of the item
* @param boolean $refresh_totals whether or not to calculate totals after setting the new qty
*/
public function set_quantity( $cart_item_key, $quantity = 1 ) {
public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
if ( $quantity == 0 || $quantity < 0 ) {
do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key );
@ -951,8 +952,8 @@ class WC_Cart {
do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity );
}
$this->calculate_totals();
$this->set_session();
if ( $refresh_totals )
$this->calculate_totals();
}
/**

View File

@ -292,13 +292,13 @@ class WC_Form_Handler {
$_product = $values['data'];
// Skip product if no updated quantity was posted
if ( ! isset( $cart_totals[$cart_item_key]['qty'] ) )
if ( ! isset( $cart_totals[ $cart_item_key ]['qty'] ) )
continue;
// Sanitize
$quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", "", $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
if ( "" === $quantity )
if ( "" === $quantity || $quantity == $values['quantity'] )
continue;
// Update cart validation
@ -311,9 +311,11 @@ class WC_Form_Handler {
}
if ( $passed_validation )
WC()->cart->set_quantity( $cart_item_key, $quantity );
WC()->cart->set_quantity( $cart_item_key, $quantity, false );
}
WC()->cart->calculate_totals();
}
if ( ! empty( $_POST['proceed'] ) ) {