Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Frontend Snippets] Custom sorting options (asc/desc)
/**
* Add custom sorting options (asc/desc)
*/
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'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'random_list' == $orderby_value ) {
$args['orderby'] = 'rand';
$args['order'] = '';
$args['meta_key'] = '';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['random_list'] = 'Random';
return $sortby;
}
@EdithAllison

This comment has been minimized.

Copy link

@EdithAllison EdithAllison commented Sep 25, 2019

This code disables orderby and order in shortcodes. Let's say you want to show the latest products on a page like so:

[products limit="60" columns="6" orderby="id" order="DESC" visibility="visible"]

This now won't work as the code above will over-ride it.

To fix, amend the code as follows:

function custom_woocommerce_get_catalog_ordering_args( $args ) {

  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

	// Since a shortcode can be used on a non-WC page, we won't have $_GET['orderby'] --
	// grab it from the passed in sorting args instead for non-WC pages.
	// Don't use this on WC archives so we don't break the default option
	if ( ! is_post_type_archive( 'product' ) && ! is_shop() && isset( $sort_args['orderby'] ) ) {
		$orderby_value = $sort_args['orderby'];
	}  

	if ( 'random_list' == $orderby_value ) {
		$args['orderby'] = 'rand';
		$args['order'] = '';
		$args['meta_key'] = '';
	}
	return $args;
}

This fix is courtesy of SkyVerge, the discussion and link to fix is here: skyverge/woocommerce-extra-product-sorting-options#17

@jimmymalmqvist

This comment has been minimized.

Copy link

@jimmymalmqvist jimmymalmqvist commented Feb 15, 2021

@EdithAllison shouldn't it be this?

function custom_woocommerce_get_catalog_ordering_args( $args ) {

  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

	// Since a shortcode can be used on a non-WC page, we won't have $_GET['orderby'] --
	// grab it from the passed in sorting args instead for non-WC pages.
	// Don't use this on WC archives so we don't break the default option
	if ( ! is_post_type_archive( 'product' ) && ! is_shop() && isset( $args['orderby'] ) ) {
		$orderby_value = $args['orderby'];
	}  

	if ( 'random_list' == $orderby_value ) {
		$args['orderby'] = 'rand';
		$args['order'] = '';
		$args['meta_key'] = '';
	}
	return $args;
}
@EdithAllison

This comment has been minimized.

Copy link

@EdithAllison EdithAllison commented Feb 22, 2021

ah yes, got the variable names mixed up, $args is correct.

@van087

This comment has been minimized.

Copy link

@van087 van087 commented Sep 21, 2021

I would like to sort products based on a custom attribute.
I started with this random sorting script, but not being a programmer I haven't been able to get it to work yet.
The latest proof is this:

`
/**
Add custom sorting options (asc/desc)
*/

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'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'product_attribute_taxonomies' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = 'pa_kj';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['product_attribute_taxonomies'] = 'Order by Kj';
return $sortby;
}

`
but it does not work: "No products were found matching your selection."

My attribute name is "pa_kj" Its value is alphanumeric

I thank everyone in advance for any suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment