Automattic\WooCommerce\Blocks\BlockTypes
ProductCategories::get_categories()
Get categories (terms) from the db.
Method of the class: ProductCategories{}
No Hooks.
Return
Array
.
Usage
// protected - for code of main (parent) or child class $result = $this->get_categories( $attributes );
- $attributes(array) (required)
- Block attributes.
Default: empty array
ProductCategories::get_categories() ProductCategories::get categories code WC 9.4.2
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; }