0
votes

I'm currently working on a WooCommerce webstore. I have the shop by category option selected and the front page currently shows the first 3 categories.

Instead of the first three categories I would like it to show 3 random categories.

I was able to add a custom function to my function.php (code below) to increase the number of categories listed (to 10) but I'm not able to have the categories show up in a random order.

add_filter('storefront_product_categories_shortcode_args','custom_storefront_category_per_page' );

// Category Products
function custom_storefront_category_per_page( $args ) {
    $args['number'] = 10;
    return $args;
}

I have tired setting $args['orderby'] = "rand"; with no luck. Im guessing that only works for products. What function should I be changing to have the "Shop By Category" section on the front page list 3 random categories instead of 3 categories in AESC or DESC order?

1

1 Answers

1
votes

if orderby = rand is not working for your case please try below technique.

-- first you need to get the random category for products to display in the page.

-- then pass it to the shortcode.

$categories = get_terms( array(
    'taxonomy' => 'product_cat',
    'hide_empty' => true,
) );

$all_cat = array();

foreach( $categories as $cat ){

$all_cat[] = $cat->name; 

}

$random_cat // get and create random category with comma seperated. and pass it to the shortcode.
 $randomCat = "tshirt, shirt";

echo do_shortcode('[products limit="8" columns="4" category="$randomCat" cat_operator="AND"]'); ?>