Automattic\WooCommerce\Blocks\BlockTypes

ProductCategories::get_categories()protectedWC 1.0

Get categories (terms) from the db.

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

Хуков нет.

Возвращает

Массив.

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

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

Код ProductCategories::get_categories() WC 8.7.0

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;
			}
		);
	}
	return $hierarchical ? $this->build_category_tree( $categories, $children_only ) : $categories;
}