Merge pull request #16593 from woocommerce/update/search-order-16565
Show sorting on search results but default to relevance
This commit is contained in:
commit
bee9a35e72
|
@ -372,16 +372,11 @@ class WC_Query {
|
|||
* @param mixed $q
|
||||
*/
|
||||
public function product_query( $q ) {
|
||||
// Ordering query vars
|
||||
if ( ! $q->is_search() ) {
|
||||
$ordering = $this->get_catalog_ordering_args();
|
||||
$q->set( 'orderby', $ordering['orderby'] );
|
||||
$q->set( 'order', $ordering['order'] );
|
||||
if ( isset( $ordering['meta_key'] ) ) {
|
||||
$q->set( 'meta_key', $ordering['meta_key'] );
|
||||
}
|
||||
} else {
|
||||
$q->set( 'orderby', 'relevance' );
|
||||
$ordering = $this->get_catalog_ordering_args();
|
||||
$q->set( 'orderby', $ordering['orderby'] );
|
||||
$q->set( 'order', $ordering['order'] );
|
||||
if ( isset( $ordering['meta_key'] ) ) {
|
||||
$q->set( 'meta_key', $ordering['meta_key'] );
|
||||
}
|
||||
|
||||
// Query vars that affect posts shown
|
||||
|
@ -445,10 +440,16 @@ class WC_Query {
|
|||
$order = strtoupper( $order );
|
||||
$args = array();
|
||||
|
||||
// default - menu_order
|
||||
$args['orderby'] = 'menu_order title';
|
||||
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
|
||||
$args['meta_key'] = '';
|
||||
// Set to default. Menu order for non-searches, relevance for searches.
|
||||
if ( ! is_search() ) {
|
||||
$args['orderby'] = 'menu_order title';
|
||||
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
|
||||
$args['meta_key'] = '';
|
||||
} else {
|
||||
$args['orderby'] = 'relevance';
|
||||
$args['order'] = 'DESC';
|
||||
$args['meta_key'] = '';
|
||||
}
|
||||
|
||||
switch ( $orderby ) {
|
||||
case 'rand' :
|
||||
|
@ -482,6 +483,10 @@ class WC_Query {
|
|||
$args['orderby'] = 'title';
|
||||
$args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
|
||||
break;
|
||||
case 'relevance' :
|
||||
$args['orderby'] = 'relevance';
|
||||
$args['order'] = 'DESC';
|
||||
break;
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
|
||||
|
|
|
@ -821,7 +821,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
|
|||
function woocommerce_catalog_ordering() {
|
||||
global $wp_query;
|
||||
|
||||
if ( 1 === (int) $wp_query->found_posts || ! woocommerce_products_will_display() || $wp_query->is_search() ) {
|
||||
if ( 1 === (int) $wp_query->found_posts || ! woocommerce_products_will_display() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -836,6 +836,11 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
|
|||
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ),
|
||||
) );
|
||||
|
||||
if ( $wp_query->is_search() ) {
|
||||
$catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options );
|
||||
unset( $catalog_orderby_options['menu_order'] );
|
||||
}
|
||||
|
||||
if ( ! $show_default_orderby ) {
|
||||
unset( $catalog_orderby_options['menu_order'] );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue