• Resolved Cristian

    (@mastix)


    Hello:

    I would be great to have an option to show products randomly. I am selling images and would like that there is no fixed pattern showing them most of the time. This is the filtering (option random) and also in the search engine by tags (search by tags and give random order of results). Why is this not possible. It seems to be a very much needed option.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Néstor Soriano Vilchez

    (@konamiman)

    Hi Cristian, thanks for your feedback.

    It’s possible to add an option for showing products randomly in the catalog by adding some custom code. By using a code snippets plugin (for example this one) you can try to add the following snippet (adapted from a Stack Overflow answer) to add a “Random” option to the product sort selector that appears in the shop catalog page:

    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    	if( is_search() ) {
    		return $args;
    	}
    
    	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( (string) wp_unslash( $_GET['orderby'] ) ) : wc_clean( get_query_var( 'orderby' ) );
    	if ( ! $orderby_value ) {
    		$orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );
    	}
    
    	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;
    }

    I hope that helps.

    Additionally, if you think that this is a functionality that WooCommerce should implement natively, please feel free to submit an enhancement request in the WooCommerce repository in GitHub.

    Thread Starter Cristian

    (@mastix)

    Thank you very much. I appreciate your help and will give it a try how it works out

    Kind regards

    Cristian

    You’re welcome @mastix take your time and let us know how that goes!

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Random order showing products’ is closed to new replies.