foreach ( $terms as $term ) { $term_id = $term->term_id; $parent_id = $term->parent; $temp_parents[ $term_id ] = $parent_id; if ( ! isset( $temp_children[ $parent_id ] ) ) { $temp_children[ $parent_id ] = array(); } $temp_children[ $parent_id ][] = $term_id; $temp_terms[ $term_id ] = array( 'slug' => $term->slug, 'name' => $term->name, 'parent' => $parent_id, 'term_id' => $term->term_id, ); } // Pre-compute descendants and ancestors. foreach ( array_keys( $temp_parents ) as $term_id ) { $map['descendants'][ $term_id ] = $this->compute_descendants( $term_id, $temp_children ); $map['ancestors'][ $term_id ] = $this->compute_ancestors( $term_id, $temp_parents ); } foreach ( $temp_children[0] as $term_id ) { $this->build_term_tree( $map['tree'], $term_id, $temp_children, $temp_terms ); } return $map; } /** * Recursively build hierarchical term tree with depth and parent. * * @param array $tree Reference to tree array being built. * @param int $term_id Current term ID. * @param array $children Children relationships map (parent_id => [child_ids]). * @param array $temp_terms Term data indexed by term_id. * @param int $depth Current depth level in hierarchy. */ private function build_term_tree( &$tree, $term_id, $children, $temp_terms, $depth = 0 ) { $tree[ $term_id ] = $temp_terms[ $term_id ]; $tree[ $term_id ]['depth'] = $depth; if ( ! empty( $children[ $term_id ] ) ) { foreach ( $children[ $term_id ] as $child_id ) { $this->build_term_tree( $tree[ $term_id ]['children'], $child_id, $children, $temp_terms, $depth + 1 ); } } } /** * Compute all descendants of a term. * * @param int $term_id The term ID. * @param array $children Children relationships map. * @return array Array of descendant term IDs. */ private function compute_descendants( int $term_id, array $children ): array { $descendants = array(); if ( ! isset( $children[ $term_id ] ) ) { return $descendants; } foreach ( $children[ $term_id ] as $child_id ) { $descendants[] = $child_id; $descendants = array_merge( $descendants, $this->compute_descendants( $child_id, $children ) ); } return array_unique( $descendants ); } /** * Compute ancestor chain for a term. * * @param int $term_id The term ID. * @param array $parent_lookup Parent relationships. * @return array Array of ancestor term IDs (bottom-up). */ private function compute_ancestors( int $term_id, array $parent_lookup ): array { $ancestors = array(); $current_id = $term_id; while ( isset( $parent_lookup[ $current_id ] ) && $parent_lookup[ $current_id ] > 0 ) { $parent_id = $parent_lookup[ $current_id ]; $ancestors[] = $parent_id; $current_id = $parent_id; } return $ancestors; } }
Fatal error: Uncaught Automattic\WooCommerce\Internal\DependencyManagement\ContainerException: Attempt to get an instance of class 'Automattic\WooCommerce\Internal\ProductFilters\TaxonomyHierarchyData', which doesn't exist. in /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php:102 Stack trace: #0 /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(182): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get_core('Automattic\\WooC...', Array) #1 [internal function]: Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->Automattic\WooCommerce\Internal\DependencyManagement\{closure}(Object(ReflectionParameter)) #2 /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(170): array_map(Object(Closure), Array) #3 /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(116): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->instantiate_class_using_reflection('Automattic\\WooC...', Array) #4 /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(75): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get_core('Automattic\\WooC...', Array) #5 /htdocs/wp-content/plugins/woocommerce/src/Container.php(67): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get('Automattic\\WooC...') #6 /htdocs/wp-content/plugins/woocommerce/includes/class-woocommerce.php(391): Automattic\WooCommerce\Container->get('Automattic\\WooC...') #7 /htdocs/wp-content/plugins/woocommerce/includes/class-woocommerce.php(255): WooCommerce->init_hooks() #8 /htdocs/wp-content/plugins/woocommerce/includes/class-woocommerce.php(162): WooCommerce->__construct() #9 /htdocs/wp-content/plugins/woocommerce/woocommerce.php(47): WooCommerce::instance() #10 /htdocs/wp-content/plugins/woocommerce/woocommerce.php(62): WC() #11 /htdocs/wp-settings.php(560): include_once('/htdocs/wp-cont...') #12 /htdocs/wp-config.php(128): require_once('/htdocs/wp-sett...') #13 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #14 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #15 /htdocs/index.php(17): require('/htdocs/wp-blog...') #16 {main} thrown in /htdocs/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php on line 102