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?
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.
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.
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.
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.
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();
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With