سؤال

I'm looking for a way to show random WooCommerce products on a page. This is nothing to do with "featured products" just random from any category.

I've been looking but cant seem to find any plugin or script to do this? Does anyone have an idea how to do this?

هل كانت مفيدة؟

المحلول

Alright folks here is a bit of code I am using for mine its for recent products but is doing the job. Just add to page you want to show them on.

[recent_products per_page="4" columns="4" orderby="rand" order="rand"]

نصائح أخرى

Try this. Paste code into functions.php Go to wp-admin/ Woocommerce > Settings > Products > Display View settings drop down order by random will be a new option. *note: it will be the the last option.

// Shop random order. View settings drop down order by Woocommerce > Settings > Products > Display
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;
}

You can try it. let post it in function.php

 add_filter('woocommerce_get_catalog_ordering_args', 'set_sort_order');
   function set_sort_order($args) {
     $args['orderby'] = 'rand';
     return ($args);    
   }

This works for me:

<?php

    global $post; // setup_postdata will not work without this being set (outside of the foreach loop)

    $args = array(
        'posts_per_page'   => 1,
        'orderby'          => 'rand',
        'post_type'        => 'product' ); 

    $random_products = get_posts( $args );

    foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();
?>

This was the code I used [featured_products per_page="8" columns="4" orderby="rand"]

I just used this [products limit="8" columns="4" orderby="rand" order="rand" visibility="visible"].and it works as expected

If you would like to use code to make the shop homepage sort by random, then use the following code. All you need to do is install a plugin called "code snippets" that allows you to make changes in WordPress without changing any core files.

add_action( 'pre_get_posts', 'shop_default_orderby_rand' );
function shop_default_orderby_rand( $query ) {
    if ( is_shop() && ( ! isset($_GET['orderby']) || 'menu_order' === $_GET['orderby'] ) ) {
        $query->set( 'orderby', 'rand' );
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top