Sort price in FacetWP

£可爱£侵袭症+ 提交于 2020-07-09 06:16:02

问题


In FacetWP Plugin (for Wordpress) I want to sort my results after 'price', so I added a new custom filter, described in their documentation. Currently, the sort results are looking like this:

  1. 4.450 €
  2. 399 €
  3. 3.990 €

I think, the code doesn't recognizes the last zero value. This is my code:

add_filter( 'facetwp_sort_options', function( $options, $params ) {
    $options['price_desc'] = array(
        'label' => 'Price (Highest)',
        'query_args' => array(
            'orderby' => 'price',
            'meta_key' => 'price',
            'order' => 'DESC',
        )
    );
    return $options;
}, 10, 2 );

Already tried the "usort" function and the alternate 'price_raw_short' value (delivered by mobile.de) with no effect.


回答1:


Do you have Woocommerce? Then you need to tell it that it is a number. Also the meta_key is _price

Example for both ascending and Descending sorting:

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

$options['price-desc'] = array(
    'label' => __( 'Price: high to low', 'woocommerce' ),
    'query_args' => array(
        'orderby' => 'meta_value_num',
        'meta_key' => '_price',
        'order' => 'desc',
    )
);


来源:https://stackoverflow.com/questions/45596929/sort-price-in-facetwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!