Support » Plugin: WOOF - Products Filter for WooCommerce » Random order (orderby=rand) with pagination

  • Resolved Morphim

    (@morphim)


    Hi there.
    I have orderby=rand set within my woof_products shortcode but I also have per_page=24 which then produces pagination.

    So, when I go to the next paginated page, the random order is once again generated which leads to duplication of some products from previous pages. Is there a way to stop this?

    Thanks in advance

    • This topic was modified 11 months, 4 weeks ago by  Morphim.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support pavloborysenko

    (@pavloborysenko)

    Hello

    Unfortunately not.

    this behavior is a sort feature of rand.

    Please drop me exact link to the issue

    Hi. Thanks for the reply.

    Here is a non pagination page (quite long and takes a while to load) but uses orderby=rand
    http://sohovo-test-com.stackstaging.com/uk-voices/

    And here is a page with orderby=rand & per_page=24
    Therefore this has pagination. It’s not too easy to spot but some artists are duplicated on other pages.
    http://sohovo-test-com.stackstaging.com/international-voices/

    I tried YITH infinite scroll too but it does the same as i guess it replicates the pagination pages.

    There is a plugin here:
    https://wordpress.org/plugins/woo-random-product-sorting-with-pagination/
    that seems to overcome this problem by introducing a 1 hour cache to the random seed but this is for WC and not WOOF.
    Maybe it’s possible to add something from this to a function in WOOF? Or edit this plugin to hook into WOOF rather then WC?
    Here’s the code of that plugin:

    <?php
    if(!defined('ABSPATH')){
    	exit; //Exit if accessed directly.
    }
    if(in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){
    	if(!function_exists('do2_addRandomProductOrderSetting')&&!function_exists('do2_randomizeProductWhenSet')){
    		
    		//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');
    	}
    }

    Thanks again in advance

    • This reply was modified 11 months, 4 weeks ago by  Morphim.
    Plugin Support pavloborysenko

    (@pavloborysenko)

    Hello

    You need add properties “random_order” in file – \plugins\woocommerce-products-filter\index.php here – https://c2n.me/3XFlTSl.png

    Hey. Thanks very much for this. I really appreciate it.
    Initial tests seem good : )

    To be used in conjunction with this plugin:
    https://wordpress.org/plugins/woo-random-product-sorting-with-pagination/
    Which provides a random sorting order to products in WooCommerce and eliminates duplicate products in pagination. It does this by caching the random seed for one hour. This obviously doesn’t carry over to WOOF, which has it’s own random order. So …

    For anyone else, add

    case 'random_order':
    break;

    to the index.php file of WOOF plugin (**remember this will be overwritten in any update)
    Around line 1882, like this:

     switch ($orderby) {
    			case 'random_order':
    			break;
                case 'price-desc':
                    $orderby = "meta_value_num {$wpdb->posts}.ID";
                    $order = 'DESC';
                    $meta_key = '_price';
                    break;

    Then, within the WOOF shortcode on your page, add something like this:
    [woof is_ajax=1 taxonomies=product_cat:96]
    [woof_products taxonomies=product_cat:96 orderby=random_order per_page=24 ]

    This will only display category 96 (or whatever cat number(s) you specify.
    Then the woof_products shortcode uses the random order set by the other plugin and only displays 24 products per page. Then, the same random seed is used for any subsequent paginated pages. After an hour, the randomness with change on page refresh for another hour.

    Thanks for an excellent plugin and great help. I appreciate your time. Cheers

    • This reply was modified 11 months, 3 weeks ago by  Morphim.
    Plugin Support pavloborysenko

    (@pavloborysenko)

    Hello

    Welcome;)

    Hi again. Unfortunately, this works to randomise the products and stops duplications on paginated pages but the seed isn’t refreshing after an hour (or after any time)
    I’m guessing this is something to do with the random pagination plugin or it’s function with WOOF.

    Is it possible to add the random seed time limit to WOOF at all?
    I guess it’s this code (there might be more?) I’m no coder.

    if(false===($seed = get_transient('do2_randSeed'))){
    					$seed = rand();
    					set_transient('do2_randSeed', $seed, 3600 );
    				}

    Thanks again inadvance

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Random order (orderby=rand) with pagination’ is closed to new replies.