Display four products per category on the shop page in WooCommerce on child sites.
Snippet Type
Execute on Child Sites
Snippet
add_action( 'woocommerce_no_products_found', 'wc_show_4_products_per_category' );
function wc_show_4_products_per_category() {
$args = array(
'parent' => 0,
'hide_empty' => true,
'taxonomy' => 'product_cat',
'fields' => 'slugs',
);
$categories = get_categories( $args );
foreach ( $categories as $category_slug ) {
$term_object = get_term_by( 'slug', $category_slug , 'product_cat' );
echo '<hr><h2>' . $term_object->name . '</h2>';
echo do_shortcode( '[products limit="4" columns="4" category="' . $category_slug . '"]' );
echo '<p><a href="' . get_term_link( $category_slug, 'product_cat' ) . '">View all ' . $term_object->name . ' products →</a>';
}
}