Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

QueryBuilder::add_price_sorting_posts_clauses()publicWC 1.0

Add the posts_clauses to add price-based sorting

Method of the class: QueryBuilder{}

No Hooks.

Return

Array. Modified list of clauses.

Usage

$QueryBuilder = new QueryBuilder();
$QueryBuilder->add_price_sorting_posts_clauses( $clauses, $query );
$clauses(array) (required)
The list of clauses for the query.
$query(WP_Query) (required)
The WP_Query instance.

QueryBuilder::add_price_sorting_posts_clauses() code WC 9.6.0

public function add_price_sorting_posts_clauses( $clauses, $query ) {
	$query_vars                  = $query->query_vars;
	$is_product_collection_block = $query_vars['isProductCollection'] ?? false;

	if ( ! $is_product_collection_block ) {
		return $clauses;
	}

	$orderby = $query_vars['orderby'] ?? null;
	if ( 'price' !== $orderby ) {
		return $clauses;
	}

	$clauses['join']    = $this->append_product_sorting_table_join( $clauses['join'] );
	$is_ascending_order = 'asc' === strtolower( $query_vars['order'] ?? 'desc' );

	$clauses['orderby'] = $is_ascending_order ?
		'wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC' :
		'wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC';

	return $clauses;
}