Created
June 18, 2013 09:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Thanks @coenjacobs! -- Hey @erichopf, put that code without the PHP tags in your functions.php file of your template.
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
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.