diff --git a/classes/cart.class.php b/classes/cart.class.php index c3e9ec3525e..47ca12df544 100644 --- a/classes/cart.class.php +++ b/classes/cart.class.php @@ -45,6 +45,8 @@ class woocommerce_cart { $this->applied_coupons = array(); $this->get_cart_from_session(); if ( isset($_SESSION['coupons']) ) $this->applied_coupons = $_SESSION['coupons']; + + add_action('woocommerce_check_cart_items', array(&$this, 'check_cart_items'), 1); } /** @@ -539,6 +541,17 @@ class woocommerce_cart { function needs_payment() { if ( $this->total > 0 ) return true; else return false; } + + + /** + * Check cart items for errors + */ + function check_cart_items() { + + $result = $this->check_cart_item_stock(); + if (is_wp_error($result)) $woocommerce->add_error( $result->get_error_message() ); + + } /** * looks through the cart to check each item is in stock diff --git a/classes/checkout.class.php b/classes/checkout.class.php index 9668a8b1284..8f75a03385f 100644 --- a/classes/checkout.class.php +++ b/classes/checkout.class.php @@ -677,24 +677,8 @@ class woocommerce_checkout { 'item_meta' => $item_meta->meta ), $values); - // Check stock levels - if ($_product->managing_stock()) : - if (!$_product->is_in_stock() || !$_product->has_enough_stock( $values['quantity'] )) : - - $woocommerce->add_error( sprintf(__('Sorry, we do not have enough "%s" in stock to fulfil your order. Please edit your cart and try again. We apologise for any inconvenience caused.', 'woothemes'), $_product->get_title() ) ); - break; - - endif; - else : - - if (!$_product->is_in_stock()) : - - $woocommerce->add_error( sprintf(__('Sorry, we do not have enough "%s" in stock to fulfil your order. Please edit your cart and try again. We apologise for any inconvenience caused.', 'woothemes'), $_product->get_title() ) ); - break; - - endif; - - endif; + // Check cart items for errors + do_action('woocommerce_check_cart_items'); endforeach; diff --git a/readme.txt b/readme.txt index 5035f00277f..1c6d222e1fd 100644 --- a/readme.txt +++ b/readme.txt @@ -107,6 +107,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo * Directory changed for uploading file downloads - uploads/woocommerce_files * Download directory created on install as well as htaccess for denying access * Formal and informal German translations - thanks to stefahn, jessor, Ramoonus , owcv and deckerweb +* Hook for checking cart contents during cart/checkout - used for plugins too = 1.2 - 03/11/2011 = * Added quick status change buttons (processing/complete) to orders panel diff --git a/shortcodes/shortcode-cart.php b/shortcodes/shortcode-cart.php index 36cf50bbfe7..0b424055afe 100644 --- a/shortcodes/shortcode-cart.php +++ b/shortcodes/shortcode-cart.php @@ -62,10 +62,7 @@ function woocommerce_cart( $atts ) { endif; - $result = $woocommerce->cart->check_cart_item_stock(); - if (is_wp_error($result)) : - $woocommerce->add_error( $result->get_error_message() ); - endif; + do_action('woocommerce_check_cart_items'); $woocommerce->show_messages(); diff --git a/shortcodes/shortcode-checkout.php b/shortcodes/shortcode-checkout.php index a1e48ed1aac..4cdadd957cf 100644 --- a/shortcodes/shortcode-checkout.php +++ b/shortcodes/shortcode-checkout.php @@ -30,9 +30,7 @@ function woocommerce_checkout( $atts ) { $woocommerce_checkout->process_checkout(); - $result = $woocommerce->cart->check_cart_item_stock(); - - if (is_wp_error($result)) $woocommerce->add_error( $result->get_error_message() ); + do_action('woocommerce_check_cart_items'); if ( $woocommerce->error_count()==0 && $non_js_checkout) $woocommerce->add_message( __('The order totals have been updated. Please confirm your order by pressing the Place Order button at the bottom of the page.', 'woothemes') );