How to set random order for products in WooCommerce

How to set random order for products in WooCommerce

In order to set random order for products, you should perform the following:

  1. Navigate to the following directory in your cpanel or FTP: wp-content/themes/themeXXXXX/includes, locate a custom-function.php file, and click an Edit button (or edit it with the help of a text editor).
  2. Paste the following piece of code to the bottom of the file before the last ?>:
    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;
    }
  3. Save the changes and navigate to the following directory in your WooCommerce Settings: Products–>Display.
  4. Check out the Random option under Default Product Sorting directory.

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.