Automattic\WooCommerce\Blocks\BlockTypes

ProductCollection::get_filter_by_taxonomies_query()privateWC 1.0

Return a query to filter products by taxonomies (product categories, product tags, etc.)

For example: User could provide "Product Categories" using "Filters" ToolsPanel available in Inspector Controls. We use this function to extract its query from $tax_query.

For example, this is how the query for product categories will look like in $tax_query array: Array

(
	[taxonomy] => product_cat
	[terms] => Array
		(
			[0] => 36
		)
)

For product tags, taxonomy would be "product_tag"

Method of the class: ProductCollection{}

No Hooks.

Return

Array. Query to filter products by taxonomies.

Usage

// private - for code of main (parent) class only
$result = $this->get_filter_by_taxonomies_query( $tax_query ): array;
$tax_query(array) (required)
Query to filter products by taxonomies.

ProductCollection::get_filter_by_taxonomies_query() code WC 9.4.2

private function get_filter_by_taxonomies_query( $tax_query ): array {
	if ( ! is_array( $tax_query ) ) {
		return array();
	}

	/**
	 * Get an array of taxonomy names associated with the "product" post type because
	 * we also want to include custom taxonomies associated with the "product" post type.
	 */
	$product_taxonomies = array_diff( get_object_taxonomies( 'product', 'names' ), array( 'product_visibility', 'product_shipping_class' ) );
	$result             = array_filter(
		$tax_query,
		function ( $item ) use ( $product_taxonomies ) {
			return isset( $item['taxonomy'] ) && in_array( $item['taxonomy'], $product_taxonomies, true );
		}
	);

	// phpcs:ignore WordPress.DB.SlowDBQuery
	return ! empty( $result ) ? array( 'tax_query' => $result ) : array();
}