Skip to content

Instantly share code, notes, and snippets.

@coenjacobs
Created June 18, 2013 09:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save coenjacobs/5803977 to your computer and use it in GitHub Desktop.
Save coenjacobs/5803977 to your computer and use it in GitHub Desktop.
Adds a custom way of ordering products, in this example it's 'random' product ordering. This can also be set to be used as default via the Catalog tab in WooCommerce settings.
<?php
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'] ) ? woocommerce_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;
}
@erichopf
Copy link

erichopf commented Sep 4, 2013

I'm a php nob. Could you enlighten me as to what file this code is supposed to be added to? The php file uploaded and then use a hook in another file for wc settings? Any info on the right direction would be appreciated.

@idac73
Copy link

idac73 commented Nov 5, 2013

Thanks @coenjacobs! -- Hey @erichopf, put that code without the PHP tags in your functions.php file of your template.

@whoaloic
Copy link

The random products snippet isn’t working correctly with pagination, since it resets at every new page.

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