Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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.

@thrash4life

This comment has been minimized.

Copy link

thrash4life commented Nov 5, 2013

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

@whoaloic

This comment has been minimized.

Copy link

whoaloic commented Nov 24, 2016

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