• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / _wc_term_recount() – Function for recounting product terms, ignoring hidden products.

You appear to be a bot. Output may be restricted

Description

Function for recounting product terms, ignoring hidden products.

Usage

_wc_term_recount( $terms, $taxonomy, $callback, $terms_are_term_taxonomy_ids );

Parameters

$terms
( array ) required – List of terms.
$taxonomy
( object ) required – Taxonomy.
$callback
( bool ) optional default: 1 – Callback.
$terms_are_term_taxonomy_ids
( bool ) optional default: 1 – If terms are from term_taxonomy_id column.

Returns

void

Source

File name: woocommerce/includes/wc-term-functions.php


Lines:

1 to 100 of 107
function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_taxonomy_ids = true ) {
  global $wpdb;

  
/**
 * Filter to allow/prevent recounting of terms as it could be expensive.
 * A likely scenario for this is when bulk importing products. We could
 * then prevent it from recounting per product but instead recount it once
 * when import is done. Of course this means the import logic has to support this.
 *
 * @since 5.2
 * @param bool
 */
  if ( ! apply_filters( 'woocommerce_product_recount_terms', '__return_true' ) ) {
    return;
  }

  // Standard callback.
  if ( $callback ) {
    _update_post_term_count( $terms, $taxonomy );
  }

  $exclude_term_ids            = array();
  $product_visibility_term_ids = wc_get_product_visibility_term_ids();

  if ( $product_visibility_term_ids['exclude-from-catalog'] ) {
    $exclude_term_ids[] = $product_visibility_term_ids['exclude-from-catalog'];
  }

  if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
    $exclude_term_ids[] = $product_visibility_term_ids['outofstock'];
  }

  $query = array(
    'fields' => "
			SELECT COUNT( DISTINCT ID ) FROM {$wpdb->posts} p
		",
    'join'   => '',
    'where'  => "
			WHERE 1=1
			AND p.post_status = 'publish'
			AND p.post_type = 'product'

		",
  );

  if ( count( $exclude_term_ids ) ) {
    $query['join']  .= " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ',', array_map( 'absint', $exclude_term_ids ) ) . ' ) ) AS exclude_join ON exclude_join.object_id = p.ID';
    $query['where'] .= ' AND exclude_join.object_id IS NULL';
  }

  // Pre-process term taxonomy ids.
  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;
    }
  }

  // Exit if we have no terms to count.
  if ( empty( $terms ) ) {
    return;
  }

  // Ancestors need counting.
  if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
    foreach ( $terms as $term_id ) {
      $terms = array_merge( $terms, get_ancestors( $term_id, $taxonomy->name ) );
    }
  }

  // Unique terms only.
  $terms = array_unique( $terms );

  // Count the terms.
  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.
      $children = get_term_children( $term_id, $taxonomy->name );

      if ( $children && ! is_wp_error( $children ) ) {
        $terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) );
      }
    }

    // Generate term query.
    $term_query          = $query;
    $term_query['join'] .= " INNER JOIN ( SELECT object_id FROM {$wpdb->term_relationships} INNER JOIN {$wpdb->term_taxonomy} using( term_taxonomy_id ) WHERE term_id IN ( " . implode( ',', array_map( 'absint', $terms_to_count ) ) . ' ) ) AS include_join ON include_join.object_id = p.ID';

    // Get the count.
    $count = $wpdb->get_var( implode( ' ', $term_query ) ); // WPCS: unprepared SQL ok.

[1] [2] Next »

 View on GitHub View on Trac

Published: 8th February 2020 | Last updated: 19th April 2021

Primary Sidebar

Information

Function name: _wc_term_recount
Plugin ref: WooCommerce
Version: 5.2.2
Sourcefile: includes/wc-term-functions.php
File ref: includes/wc-term-functions.php
API type: private
Deprecated?: No
API Letters: _,R,T,W

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.7.1
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites