Check products are purchasable when restoring cart

If WC_Product::is_purchasable() is only called when adding a product to the cart,
it's possible to purchase products that aren't purchasable (but were when they were
added to the cart).
This commit is contained in:
thenbrent 2013-11-22 15:14:08 -08:00
parent e2a56a92c5
commit bb14495064
1 changed files with 24 additions and 9 deletions

View File

@ -148,12 +148,22 @@ class WC_Cart {
// Load the cart
$cart = WC()->session->get( 'cart', array() );
$update_cart_session = false;
if ( is_array( $cart ) ) {
foreach ( $cart as $key => $values ) {
$_product = get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
if ( ! empty( $_product ) && $_product->exists() && $values['quantity'] > 0 ) {
if ( ! $_product->is_purchasable() ) {
// Flag to indicate the stored cart should be update
$update_cart_session = true;
wc_add_notice( sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $_product->get_title() ), 'error' );
} else {
// Put session data into array. Run through filter so other plugins can load their own session data
$this->cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', array(
'product_id' => $values['product_id'],
@ -162,9 +172,14 @@ class WC_Cart {
'quantity' => $values['quantity'],
'data' => $_product
), $values, $key );
}
}
}
}
if ( $update_cart_session )
WC()->session->cart = $this->cart_contents;
$this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 );
@ -172,7 +187,7 @@ class WC_Cart {
do_action( 'woocommerce_cart_loaded_from_session', $this );
// Queue re-calc if subtotal is not set
if ( ! $this->subtotal && sizeof( $this->cart_contents ) > 0 )
if ( ( ! $this->subtotal && sizeof( $this->cart_contents ) > 0 ) || $update_cart_session )
$this->calculate_totals();
}