I need to make my WooCommerce shop page have a different type of randomizing. Basically I want it to randomize by category and display that method going forward. So one category of items then another category of items but the products in those categories need to be randomized as well so it looks mixed up. Could anyone assist with this ?
CodePudding user response:
Once you have added the code above to your active theme’s "functions" file, head to Appearance > Customise to configure your Woo Commerce product catalogue sorting options.
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woo_catalog_ordering_args' );
function custom_woo_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', 'woocommerce_catalog_random_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'woocommerce_catalog_random_orderby' );
function woocommerce_catalog_random_orderby( $sortby ) {
$sortby['random_list'] = 'Random';
return $sortby;
}