I need my woocommerce products to display randomly. My products are actually people and the random element is so different people get seen on the first page so no-one is favoured.

So, I have Woocommerce installed and am using a filter plugin called WOOF (Woo Filter - just to complicate things)

WOOF uses shortcode like this: [woof is_ajax=1 taxonomies=product_cat:196]

[woof_products taxonomies=product_cat:196 orderby=random_order per_page=24]

So, displays category 196 using the random order and has 24 products per page.

However, if products run over pagination, the randomness is re-generated on the next page meaning products can be duplicated across page 1, 2, 3 etc.

So, I found a plugin that sets a random seed with a 1 hour time limit. This avoids the products being repeated. However, the 1 hour limit to the rand seed doesn't seem to be working.

Here is the random seed plugin code. I've contacted the plugin guy but not had any reply:

    //add "Random" setting to product sorting menu
    function do2_addRandomProductOrderSetting($sortby){
        $sortby['random_order'] = 'Random';
        return $sortby;
    }
    add_filter('woocommerce_default_catalog_orderby_options','do2_addRandomProductOrderSetting');
    add_filter('woocommerce_catalog_orderby','do2_addRandomProductOrderSetting');

    //randomize products when setting is used
    function do2_randomizeProductWhenSet($args){
        $orderbySetting = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) : apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));
        if('random_order' == $orderbySetting){
            if(false===($seed = get_transient('do2_randSeed'))){
                $seed = rand();
                set_transient('do2_randSeed', $seed, 3600 );
            }
            $args['orderby'] = 'RAND('.$seed.')';
            $args['order'] = '';
            $args['meta_key'] = '';
        }
        return $args;
    }
    add_filter('woocommerce_get_catalog_ordering_args','do2_randomizeProductWhenSet');
}
} else {
if(!function_exists('do2_WooCommerceAdminNotice')){
    //warn on missing WooCommerce
    function do2_WooCommerceAdminNotice() {
    ?>
        <div class="notice error is-dismissible" >
            <p><?php _e('Your site must be running WooCommerce to benefit from the WooCommerce Random Product Sorting with Pagination plugin.'); ?></p>
        </div>
    <?php
    }
    add_action('admin_notices', 'do2_WooCommerceAdminNotice');
}
}

Here also is what I've added to the index of the WOOF filter plugin index.php within it's 'orderby' section ( just this - case 'random_order': break; but in context):

  public function get_catalog_orderby($orderby = '', $order = 'ASC') {
    if (empty($orderby) OR $orderby == 'no') {
        $orderby = get_option('woocommerce_default_catalog_orderby');
    }
    //echo $orderby;exit;
    //D:\myprojects\woocommerce-filter\wp-content\plugins\woocommerce\includes\class-wc-query.php#588
    //$orderby_array = array('menu_order', 'popularity', 'rating',
    //'date', 'price', 'price-desc','rand');
    $meta_key = '';
    global $wpdb;
    switch ($orderby) {
        case 'random_order':
        $orderby = "random_order";
        break;
        case 'price-desc':
            $orderby = "meta_value_num {$wpdb->posts}.ID";
            $order = 'DESC';
            $meta_key = '_price';
            break;
        case 'price':
            $orderby = "meta_value_num {$wpdb->posts}.ID";
            $order = 'ASC';
            $meta_key = '_price';
break;

I've set the Woo sorting to be by 'Random' via the WP customizer but think the WOOF shortcode over-rides this in the shortcode i mentioned earlier.

I just need the random seed to change every hour so pagination works but the random order is reset after an hour so the order of the products changes for the next hour.

I just don't understand what's not working. When i set the order to 'random_order' in the WOOF shortcode, the order just defaults to date-desc - newest post at the top and not random at all.

Can anyone see a fault in the code? Thanks very much in advance

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.