Add random sorting in woocommerce - by Whoszecki

This topic has 5 replies, 2 voices, and was last updated 4 years ago ago by Olga Barlow

  • Avatar: Whoszecki
    Whoszecki
    Participant
    April 13, 2020 at 17:18

    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"]

    4 Answers
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    April 13, 2020 at 17:32

    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

    Avatar: Whoszecki
    Whoszecki
    Participant
    April 13, 2020 at 18:01

    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/

    Avatar: Whoszecki
    Whoszecki
    Participant
    April 13, 2020 at 18:22

    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.

    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    April 14, 2020 at 10:15

    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

  • Viewing 5 results - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.