gsiegel
asked 7 years ago

I have several WooCommerce products with the same price and I’m trying to change the default WooCommerce sort/orderby to sort first by Price Low-to-High AND then sort by Title (asc).
Is this possible? Many thanks in advance.

1 Answers
DominicStaff
answered 7 years ago

The correct way to force orderby by “title” can be done this way:

add_filter( 'woocommerce_products_widget_query_args','title_orderby_products_widget_query_arg', 10, 1 );
function title_orderby_products_widget_query_arg( $query_args ) {
    // set 'title' for orderby 
    $query_args['orderby'] = 'title';

    return $query_args;
}

Code goes in function.php file of your active child theme or theme.
Or you can use the following code:

add_action( 'woocommerce_cart_loaded_from_session', function() {
 global $woocommerce;
 $products_in_cart = array();
 foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
 $products_in_cart[ $key ] = $item['data']->get_title();
 }
 natsort( $products_in_cart );
 $cart_contents = array();
 foreach ( $products_in_cart as $cart_key => $product_title ) {
 $cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
 }
 $woocommerce->cart->cart_contents = $cart_contents;
}, 100 );

If you still face their issue, you can use the following plugin: https://wordpress.org/plugins/woocommerce-more-sorting/
This should work for you.

gsiegel
replied 7 years ago

Dominic-
Thanks for your reply. I’m trying to sort first by Price Low-to-High AND then sort by Title (asc), so this would be 2 different sorts (a Main sort and then a Sub-sort), It looks like your code might only sort by Title. Do you have code which would do the sorting I’m trying to do? Thanks very much.

Dominic Staff
replied 7 years ago

At the moment, you can not have 2 different sort methods active on at the same time.

Powered by DW Question & Answer Pro