Home Forums WoodMart support forum Product sorting via search

Product sorting via search

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #271624

    defjam2oo1
    Participant

    my categories are sorted by popularity and works too.
    But when I search on the search a product, they get results according to relevance. I can manually toggle it to popularity but I want it to be that way automatically.
    I have to edit the product-searchform.php but unfortunately it doesn’t work. can you help me?

    Product-searchform.php

    woodmart_search_form( array(
    ‘ajax’ => true,
    ‘post_type’ => ‘product’,
    ‘orderby’ => ‘popularity’
    ) );

    Example: I go to the Softdrinks Category. The first Product is Coca Cola 12×1,0L. This is also the most popular item.
    Now I am searching for the article Cola and first see other articles because they are sorted by relevance.

    Attachments:
    You must be logged in to view attached files.
    #271755

    Hello,

    Please add this code to the functions.php of the child.

    add_filter(
    	'woocommerce_get_catalog_ordering_args',
    	function( $args ) {
    		if ( isset( $args['orderby'] ) && 'relevance' === $args['orderby'] ) {
    			$args['orderby'] = 'popularity';
    			$args['order']   = 'DESC';
    		}
    		return $args;
    	}
    );

    Best Regards

    #271768

    defjam2oo1
    Participant

    unfortunately the snippet did not work. the search results are still sorted by Relevanz. you are welcome to look it up yourself.. I also deleted cache afterwards
    Thanks

    #272073

    Hello,

    Please remove the code provided above and add this one to the functions.php of the child theme:

    function wd_append_product_sorting_table_join( $sql ) {
    	global $wpdb;
    	if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {
    		$sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
    	}
    	return $sql;
    }
    function wd_order_by_popularity_post_clauses( $args ) {
    	$args['join']    = wd_append_product_sorting_table_join( $args['join'] );
    	$args['orderby'] = ' wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC ';
    	return $args;
    }
    add_filter(
    	'woocommerce_get_catalog_ordering_args',
    	function( $args ) {
    		if ( isset( $args['orderby'] ) && 'relevance' === $args['orderby'] ) {
    			add_filter( 'posts_clauses', 'wd_order_by_popularity_post_clauses' );
    			$args['orderby'] = 'popularity';
    			$args['order']   = 'ASC';
    		}
    		return $args;
    	}
    );

    Best Regards

    #272099

    defjam2oo1
    Participant

    It works. I love this support. many thanks

    #272108

    We are always happy to help you, write to us when you have any difficulties or issues with our theme.

    We would be grateful for 5 stars rate on http://themeforest.net/downloads in case you are satisfied with our theme and customer service

    Thank you in advance

Viewing 6 posts - 1 through 6 (of 6 total)