Fix term recount during WP callbacks Closes #5133
This commit is contained in:
parent
6dcc387978
commit
c39c4ca5a3
|
@ -395,8 +395,9 @@ function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_
|
|||
global $wpdb;
|
||||
|
||||
// Standard callback
|
||||
if ( $callback )
|
||||
if ( $callback ) {
|
||||
_update_post_term_count( $terms, $taxonomy );
|
||||
}
|
||||
|
||||
// Stock query
|
||||
if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
|
||||
|
@ -426,41 +427,53 @@ function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_
|
|||
";
|
||||
|
||||
// Pre-process term taxonomy ids
|
||||
if ( ! $terms_are_term_taxonomy_ids )
|
||||
$terms = (array) array_keys( $terms );
|
||||
if ( ! $terms_are_term_taxonomy_ids ) {
|
||||
// We passed in an array of TERMS in format id=>parent
|
||||
$terms = array_filter( (array) array_keys( $terms ) );
|
||||
} else {
|
||||
// If we have term taxonomy IDs we need to get the term ID
|
||||
$term_taxonomy_ids = $terms;
|
||||
$terms = array();
|
||||
foreach ( $term_taxonomy_ids as $term_taxonomy_id ) {
|
||||
$term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy->name );
|
||||
$terms[] = $term->term_id;
|
||||
}
|
||||
}
|
||||
|
||||
$terms = array_filter( (array) $terms );
|
||||
// Exit if we have no terms to count
|
||||
if ( ! $terms ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ancestors need counting
|
||||
if ( is_taxonomy_hierarchical( $taxonomy->name ) && $terms ) {
|
||||
if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
|
||||
foreach ( $terms as $term_id ) {
|
||||
$terms = array_merge( $terms, get_ancestors( $term_id, $taxonomy->name ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Unique terms
|
||||
// Unique terms only
|
||||
$terms = array_unique( $terms );
|
||||
|
||||
// Count the terms
|
||||
if ( $terms ) {
|
||||
foreach ( $terms as $term_id ) {
|
||||
$terms_to_count = array( absint( $term_id ) );
|
||||
foreach ( $terms as $term_id ) {
|
||||
$terms_to_count = array( absint( $term_id ) );
|
||||
|
||||
if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
|
||||
// We need to get the $term's hierarchy so we can count its children too
|
||||
if ( ( $children = get_term_children( $term_id, $taxonomy->name ) ) && ! is_wp_error( $children ) )
|
||||
$terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) );
|
||||
if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
|
||||
// We need to get the $term's hierarchy so we can count its children too
|
||||
if ( ( $children = get_term_children( $term_id, $taxonomy->name ) ) && ! is_wp_error( $children ) ) {
|
||||
$terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) );
|
||||
}
|
||||
|
||||
// Generate term query
|
||||
$term_query = 'AND term_id IN ( ' . implode( ',', $terms_to_count ) . ' )';
|
||||
|
||||
// Get the count
|
||||
$count = $wpdb->get_var( $count_query . $term_query );
|
||||
|
||||
// Update the count
|
||||
update_woocommerce_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
|
||||
}
|
||||
|
||||
// Generate term query
|
||||
$term_query = 'AND term_id IN ( ' . implode( ',', $terms_to_count ) . ' )';
|
||||
|
||||
// Get the count
|
||||
$count = $wpdb->get_var( $count_query . $term_query );
|
||||
|
||||
// Update the count
|
||||
update_woocommerce_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue