Automattic\WooCommerce\Blocks\BlockTypes

ProductCategories::get_categories │ protected │ WC 1.0

Get categories (terms) from the db.

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

Хуков нет.

Возвращает

array.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_categories( $attributes );
$attributes(массив) (обязательный)
Block attributes.
По умолчанию: empty array

Код ProductCategories::get_categories() WC 11.2.1

protected function get_categories( $attributes ) {
	$hierarchical  = wc_string_to_bool( $attributes['isHierarchical'] );
	$children_only = wc_string_to_bool( $attributes['showChildrenOnly'] ) && is_product_category();

	if ( $children_only ) {
		$term_id    = get_queried_object_id();
		$categories = get_terms(
			'product_cat',
			[
				'hide_empty'   => ! $attributes['hasEmpty'],
				'pad_counts'   => true,
				'hierarchical' => true,
				'child_of'     => $term_id,
			]
		);
	} else {
		$categories = get_terms(
			'product_cat',
			[
				'hide_empty'   => ! $attributes['hasEmpty'],
				'pad_counts'   => true,
				'hierarchical' => true,
			]
		);
	}

	if ( ! is_array( $categories ) || empty( $categories ) ) {
		return [];
	}

	// This ensures that no categories with a product count of 0 is rendered.
	if ( ! $attributes['hasEmpty'] ) {
		$categories = array_filter(
			$categories,
			function( $category ) {
				return 0 !== $category->count;
			}
		);
	}

	if ( ! empty( $attributes['hasImage'] ) ) {
		$attachment_ids = array_filter( array_map( static fn( $category ) => (int) get_term_meta( $category->term_id, 'thumbnail_id', true ), $categories ) );
		if ( ! empty( $attachment_ids ) ) {
			// Prime caches to reduce future queries.
			_prime_post_caches( $attachment_ids );
		}
	}

	return $hierarchical ? $this->build_category_tree( $categories, $children_only ) : $categories;
}