Categories and products in random order

as suggested by kaiser, my last working edit as solution:

ok, inspired by @s_ha_dum answer i manged to get a solution:

//this 1. part is out of the woocommerce-template.php and part of the woocommerce_product_subcategories()
$product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat' );
$product_category_parent = $product_cat->term_id;

$pcat_args = array(
'child_of'  => $product_category_parent,
'hide_empty'    => 1,
'hierarchical'  => 1,
'taxonomy'      => 'product_cat',
'pad_counts'    => 1
);
$product_categories = get_categories( $pcat_args  );

//grab things by post_type product
$prod_args = array(
'post_type' => array('product'),
'orderby' => rand, 
'posts_per_page' =>-1,
'post_status' => publish
);
$ng_query = get_posts($prod_args);

//combines, merge the arrays
$merge_query = array_merge( $ng_query, $product_categories );

//make the order random
shuffle($merge_query);

//iterate merged arrays
foreach( $merge_query as $mqp ) { 
setup_postdata($mqp);
if ( $mqp->taxonomy == product_cat ) {
echo $mqp->category_nicename;
echo "<br/>";
} else {
echo $mqp->post_title;
echo "<br/>";
}
}

this gives me a randomized list of categories and products!

like @s_ha_dum said it is neccessray to differ between the arrays/object-types merged together. i did that, for this simplified solution, by checking for taxonomy – not sure if that is a good way for more complex outputs.

(1st) EDIT: i now implemented it with all the specific formating and its still working well – so looking for the taxonomy »product_cat« is a good solution for my scenario!

(2nd) EDIT: i ran into the problem that the price and the add-to-cart-button wouldnt show up and the iteration would break, the solution for that was to add setup_postdata(); – i added that into the code above

Leave a Comment