Automattic\WooCommerce\Blocks\BlockTypes

ProductCategories::build_category_tree()protectedWC 1.0

Build hierarchical tree of categories.

Метод класса: ProductCategories{}

Хуков нет.

Возвращает

Массив.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->build_category_tree( $categories );
$categories(массив) (обязательный)
List of terms.

Код ProductCategories::build_category_tree() WC 7.5.1

protected function build_category_tree( $categories ) {
	$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;
	}

	$tree = $categories_by_parent['cat-0'];
	unset( $categories_by_parent['cat-0'] );

	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;
}