Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Product catalog sorting price and title together

$
0
0

Replies: 0

I’m trying to insert a snippet into functions.php to sort the product catalog by price and title together, but to no success. The following snippets are what I’ve tried so far.

add_filter( 'woocommerce_get_catalog_ordering_args', 'wc_custom_get_catalog_ordering_args' );
function wc_custom_get_catalog_ordering_args( $args ) {
	$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

	if ( $orderby_value == 'price' ) {
		$args['orderby'] = array( 'meta_value_num' => 'ASC', 'title' => 'ASC' );
		$args['meta_key'] = '_price';
    }

	if ( $orderby_value == 'price-desc' ) {
		$args['orderby'] = array( 'meta_value_num' => 'DESC', 'title' => 'ASC' );
		$args['meta_key'] = '_price';
    }

    return $args;
}

and

add_action( 'woocommerce_product_query', 'wc_custom_product_query' );
function wc_custom_product_query( $q ) {
	if ( ! $q->is_main_query() || is_admin() )   return;
	
	$orderby_value = $q->get( 'orderby' );
	if ( $orderby_value == 'price' || $orderby_value == 'price-desc' ) {
		$q->set( 'orderby', array(
			'price' => $orderby_value == 'price' ? 'ASC' : 'DESC',
			'title' => 'ASC'
		) );
	}	
}

What I’m trying to achieve is to have the product titles always sorted in an ascending order even when they are price sorted in any order.

Any help will be greatly appreciated. Thank you in advance.


Viewing all articles
Browse latest Browse all 59525

Trending Articles