From 98525111f3cc83b1390d4c1c37ea19188fb323b5 Mon Sep 17 00:00:00 2001 From: Warren Holmes Date: Mon, 17 Dec 2012 14:39:27 +0200 Subject: [PATCH] Check categories and tags are available on a product before doing count updates. --- woocommerce-core-functions.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/woocommerce-core-functions.php b/woocommerce-core-functions.php index 27bc6380939..5ef4b0a997c 100644 --- a/woocommerce-core-functions.php +++ b/woocommerce-core-functions.php @@ -2011,15 +2011,25 @@ function woocommerce_recount_after_stock_change( $product_id ) { $product_cats = $product_tags = array(); $product_terms = get_the_terms( $product_id, 'product_cat' ); - foreach ( $product_terms as $term ) - $product_cats[ $term->term_id ] = $term->parent; + + if( count( $product_terms) ){ + + foreach ( $product_terms as $term ) + $product_cats[ $term->term_id ] = $term->parent; + + } $product_terms = get_the_terms( $product_id, 'product_tag' ); - foreach ( $product_terms as $term ) - $product_tags[ $term->term_id ] = $term->parent; - _woocommerce_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false ); - _woocommerce_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false ); + if( count( $product_terms ) ){ + + foreach ( $product_terms as $term ) + $product_tags[ $term->term_id ] = $term->parent; + + } + + if( count( $product_cats ) ) _woocommerce_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false ); + if( count( $product_cats ) ) _woocommerce_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false ); } add_action( 'woocommerce_product_set_stock_status', 'woocommerce_recount_after_stock_change' );