Automattic\WooCommerce\Blocks\BlockTypes
ProductCategories::build_category_tree()
Build hierarchical tree of categories.
Метод класса: ProductCategories{}
Хуков нет.
Возвращает
Массив
.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->build_category_tree( $categories, $children_only );
- $categories(массив) (обязательный)
- List of terms.
- $children_only(true|false) (обязательный)
- Is the block rendering only the children of the current category.
Код ProductCategories::build_category_tree() ProductCategories::build category tree WC 8.7.0
protected function build_category_tree( $categories, $children_only ) { $categories_by_parent = []; foreach ( $categories as $category ) { if ( ! isset( $categories_by_parent[ 'cat-' . $category->parent ] ) ) { $categories_by_parent[ 'cat-' . $category->parent ] = []; } $categories_by_parent[ 'cat-' . $category->parent ][] = $category; } $parent_id = $children_only ? get_queried_object_id() : 0; $tree = $categories_by_parent[ 'cat-' . $parent_id ]; // these are top level categories. So all parents. unset( $categories_by_parent[ 'cat-' . $parent_id ] ); foreach ( $tree as $category ) { if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); } } return $tree; }