wordpresswoocommercefacetwp

WooCommerce: Own sort/order dropdown (FacetWP)


I want to replace the WooCommerce ordering with the FacetWP sort facet. To do so, I'm using the output hook facetwp_sort_options: https://facetwp.com/documentation/developers/output/facetwp_sort_options/

I have already replaced the order dropdown but the WooCommerce order options are missing.

For now I've only managed to add order by price:

add_filter( 'facetwp_sort_options', function( $options, $params ) {

    $options['price_asc'] = array(
        'label' => 'Price: low to high',
        'query_args' => array(
            'meta_key' => '_price',
            'orderby' => 'meta_value_num',
            'order' => 'ASC'
         )
    );

    $options['price_desc'] = array(
        'label' => 'Price: high to low',
        'query_args' => array(
            'meta_key' => '_price',
            'orderby' => 'meta_value_num',
            'order' => 'DESC'
         )
    );
    return $options;

This answer helped: https://stackoverflow.com/a/46715264/1788961

But how can I add the rest of the WooCommerce order options. Is there any list of meta fields I could use?

I need to add the following order options:

Edit: Removed the option for sale products (figured it out myself)


Solution

  • Got it from this site

    Maybe helpfull:

    add_filter( 'facetwp_sort_options', function( $options, $params ) {
    unset( $options['date_asc'] );
    unset( $options['title_asc'] );
    unset( $options['title_desc'] );
    $options['default']['label'] = 'Standaard sortering';
    $options['date_desc']['label'] = 'Sorteren op nieuwste';
    
    $options['popularity_new'] = [
        'label' => 'Sorteer op populariteit',
        'query_args' => [
            'orderby' => 'post_views',
            'order' => 'DESC',
        ]
    ];
    $options['price_asc'] = [
        'label' => 'Sorteer op prijs: laag naar hoog',
        'query_args' => [
            'orderby' => 'meta_value_num',
            'meta_key' => '_price',
            'order' => 'ASC',
        ]
    ];
    $options['price_desc'] = [
        'label' => 'Sorteer op prijs: hoog naar laag',
        'query_args' => [
            'orderby' => 'meta_value_num',
            'meta_key' => '_price',
            'order' => 'DESC',
        ]
    ];
    
    return $options;
    }, 10, 2 );