Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - Show Random Products

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?

like image 717
Daniel Avatar asked May 03 '14 05:05

Daniel


People also ask

How do I show all products in a WooCommerce shortcode?

These two shortcodes will display your product categories on any page. [product_category] – Will display products in a specified product category. [product_categories] – Will display all your product categories.

How do I get a list of products in WooCommerce?

In the WordPress admin, go to WooCommerce > Settings > Products > Product tables. Add your license key and read through all the settings, choosing the ones that you want for your WooCommerce all products list. Now create a page where you want to list all products in a table (Pages > Add New.

How do I show more products in WooCommerce?

You can set the number of products to be displayed on a single page by setting the Select Options. You can set the default number of products per page through the Default meta box. The Position option lets you decide where you want to display the Products per page selector on your WooCommerce store.

How do I use WooCommerce shortcodes?

Go to your admin dashboard and click on Plugin > Add New on the right side. Then search for WooCommerce shortcodes, and then you just have to install and activate it. When you install a shortcode plugin for WooCommerce, you will find a new shortcode button in your page editor and post editor.


4 Answers

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

like image 92
Pec1983 Avatar answered Oct 25 '22 20:10

Pec1983


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;
}
like image 21
Frithir.com Avatar answered Oct 25 '22 19:10

Frithir.com


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);    
   }
like image 37
Ngo Quoc Vinh Avatar answered Oct 25 '22 20:10

Ngo Quoc Vinh


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();
?>
like image 4
Abel Avatar answered Oct 25 '22 18:10

Abel