• Resolved Ciprian

    (@cipriandoroftei)


    Hi,

    I’m using a WooCommerce Custom Sorting Option (https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/) which makes Relevanssi fail the SQL query call.

    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    function custom_woocommerce_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 ( 'new_product_list' == $orderby_value ) {
        $args['orderby'] = 'meta_value';
        $args['order'] = 'desc';
        $args['meta_key'] = 'brainie_produs_nou';
      }
      return $args;
    }
    

    Any way to resolve this issue?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Meta field sorting doesn’t work in Relevanssi the same way it does with the default WP search. Here’s how you can do it with Relevanssi.

    Thread Starter Ciprian

    (@cipriandoroftei)

    Hi Mikko!

    The solution you pointed out works partly. The WooCommerce category pages get sorted according to the custom sort set and the result page shows results thanks to the first function from the solution.

    What I can’t make it do is sort the results. I’m not sure what $year_hits is.

    For my sort above a kinda copied the code for the $price_sort in the solution.

    Thanks

    Plugin Author Mikko Saari

    (@msaari)

    $year_hits is a relic from a year sort, hence the name. The way the price sort works is that it first loops through the posts (the first foreach loop) and sorts all posts by price to a bunch of arrays in $prices. The array key is the price and the value is another array that has all the products with that price.

    Then the $prices array is sorted by key, either normal or reverse, to have the posts sorted by price.

    Then the second foreach loop goes through the sorted $prices array and compiles all the results in the $sorted_hits array in the order of price.

    The sorting should work if you just replace $price = get_post_meta($hit->ID, '_price', true); with $price = get_post_meta($hit->ID, 'brainie_produs_nou', true); (and replace _price with brainie_produs_nou in the other function as well in order to make the sorting trigger).

    Thread Starter Ciprian

    (@cipriandoroftei)

    Figured out my mistake. I was saving the order var under a different variable instead of the new global one.

    Thank you for the help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WooCommerce Custom Sorting options’ is closed to new replies.