Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce Order By Price Not Working Correctly

When slecting the option sort by price (Woocommerce dropdown) the products to sort in price but it's not working exactly; the first 6 are sorted:

  • € 1,50
  • € 11,75
  • € 1,75
  • € 1,75
  • € 2,95
  • € 4,50

Link to unfinished website: http://verduijncichlids.com/product-categorie/vissen-voorraad/west-afrika-cichliden/?orderby=price

Anyone know what is happening and how to fix this? Cheers!

like image 493
Hassan Avatar asked Oct 15 '25 09:10

Hassan


2 Answers

for anyone who may be faced with this problem

according to: https://woocommerce.wordpress.com/2019/04/01/performance-improvements-in-3-6/

if you imported your products with tools such as "WP All Import", you have to regenerate "Product lookup tables" in:

WooCommerce > Status > Tools > Product lookup tables

like image 110
Abolfazl Sharifi Avatar answered Oct 18 '25 04:10

Abolfazl Sharifi


If ordering by price is not working correctly in WooCommerce a secure solution is always to customize how WooCommerce handles the ordering by price. This works because WooCommerce price and price-desc are default ordering options. (Tested with standard theme in WordPress v5.4.1 & WooCommerce v4.1.0):

in your functions.php add:

/**
 * Customize ordering by price
 */
add_filter('woocommerce_get_catalog_ordering_args', function ($args) {
    $orderby_value = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) : apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));

    if ('price' == $orderby_value) {
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'ASC';
        $args['meta_key'] = '_price';
    }

    if ('price-desc' == $orderby_value) {
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'DESC';
        $args['meta_key'] = '_price';
    }

    return $args;
});

like @Pelmered mentioned it is important to use meta_value_num as the 'order_by' option so the ordering is done by number values and not string values. I changed the meta_key to '_price' though, because that is required in the WooCommerce version used and mentioned above.

Further reading:

WordPress Documentation WP_Query

WooCommerce Documentation Custom sorting options

like image 42
Jannick Avatar answered Oct 18 '25 06:10

Jannick