Hello,
Yes that’s why i’m trying to do, but i don’t know where to make these changes. Thanks for your help
The above snippets would go in theme functions.php files.
Is it correct this way? ( i’m nowhere in PHP, sorry… )
<?php
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’] = ‘asc’;//
$args[‘meta_key’] = ‘wccaf_date’;//`
}
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;
}
Pretty much – meta key wouldn’t be needed. RAND order is a bad idea though – it will be different random products per page and you will see duplicates as you browse.
so, is there a way to show random products, distributed over all pages. So that there wont be any duplicates?
so, is there a way to show random products, distributed over all pages. So that there wont be any duplicates?
so, is there a way to show random products, distributed over all pages. So that there wont be any duplicates?
Not really. Also a really bad idea for caching reasons.
You could gather all your product ID’s and randomize once. Then assign each an incremental number in a custom meta field, and sort by that.
Could also do manual sorting for a pseudo-random appearance using the menu order meta field: https://www.sellwithwp.com/create-woocommerce-custom-product-sorting/
this seems the solution:
session_start();
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 ) {
// Get seed from session variable if it exists
$seed = false;
if( isset( $_SESSION['seed'] ) ) {
$seed = $_SESSION['seed'];
}
// Set new seed if none exists
if ( ! $seed ) {
$seed = rand();
$_SESSION['seed'] = $seed;
}
// Update ORDER BY clause to use seed
$sortby = 'RAND(' . $seed . ')';
return $sortby;
}