Automattic\WooCommerce\StoreApi\Utilities

ProductQuery::convert_tax_query_to_meta_query()publicWC 1.0

Convert the tax_query to a meta_query which is needed to support filtering by attributes for variations.

Method of the class: ProductQuery{}

No Hooks.

Return

Array.

Usage

$ProductQuery = new ProductQuery();
$ProductQuery->convert_tax_query_to_meta_query( $tax_query );
$tax_query(array) (required)
The tax_query to convert.

ProductQuery::convert_tax_query_to_meta_query() code WC 9.5.1

public function convert_tax_query_to_meta_query( $tax_query ) {
	$meta_query = array();

	foreach ( $tax_query as $tax_query_item ) {
		$taxonomy = $tax_query_item['taxonomy'];
		$terms    = $tax_query_item['terms'];

		$meta_key = 'attribute_' . $taxonomy;

		$meta_query[] = array(
			'key'   => $meta_key,
			'value' => $terms,
		);

		if ( isset( $tax_query_item['operator'] ) ) {
			$meta_query[0]['compare'] = $tax_query_item['operator'];
		}
	}

	return $meta_query;
}