This topic has 5 replies, 2 voices, and was last updated 4 years, 7 months ago ago by Olga Barlow
Hi,
In the product catalog, I can find option of default sorting. How can I add there random sorting? like this [recent_products per_page="4" columns="4" orderby="rand" order="rand"]
Hello,
Do you want to implement random sorting for the product on the shop page? There is no such option, all the available sorting options you can find in Appearance > Customizer > WooCommerce > Product catalog http://prntscr.com/rykad1
Regards
Yeah, I do. But based on woocommerce docs I can do it, the main problem is that how to implement it via theme?
Here is the link : https://docs.woocommerce.com/document/woocommerce-shortcodes/
Hello,
Please use this code in function.php and it’ll add Random selection for your theme.
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;
}
So, it’ll make one more great feature for great theme..
Tested, and it works like a charm.
Hello,
Oh, great. We are glad to hear that you found a solution. We’ll add this to our docs to the Customization department if anybody else decides to implement this.
Regards
You must be logged in to reply to this topic.Log in/Sign up