From b8636296ff1cae871d375e006eb0ef14ab34b6f5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 30 Aug 2011 12:43:16 +0100 Subject: [PATCH] Ajax cart fix --- assets/js/woocommerce.js | 10 +++++++++- woocommerce_actions.php | 16 +++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/assets/js/woocommerce.js b/assets/js/woocommerce.js index 23714ca1e43..1a84713604d 100755 --- a/assets/js/woocommerce.js +++ b/assets/js/woocommerce.js @@ -30,7 +30,15 @@ jQuery(function(){ jQuery.post( woocommerce_params.ajax_url, data, function(response) { // Get response - fragments = jQuery.parseJSON( response ); + data = jQuery.parseJSON( response ); + + if (data.error) { + alert(data.error); + jQuery(thisbutton).removeClass('loading'); + return; + } + + fragments = data; // Block fragments class if (fragments) { diff --git a/woocommerce_actions.php b/woocommerce_actions.php index e50c3b183b0..12300e86772 100644 --- a/woocommerce_actions.php +++ b/woocommerce_actions.php @@ -69,12 +69,18 @@ function woocommerce_ajax_add_to_cart() { $product_id = (int) $_POST['product_id']; - woocommerce_cart::add_to_cart($product_id, 1); + if (woocommerce_cart::add_to_cart($product_id, 1)) : + // Return html fragments + $data = apply_filters('add_to_cart_fragments', array()); + else : + // Return error + $data = array( + 'error' => woocommerce::$errors[0] + ); + woocommerce::clear_messages(); + endif; - // Return html fragments - $fragments = apply_filters('add_to_cart_fragments', array()); - - echo json_encode( $fragments ); + echo json_encode( $data ); die(); }