fbpx

When developing a custom theme for woocommerce you might want to list all product category with their products. This little piece of code might help.

$args = array(
	'number' => $number,
	'orderby' => 'title',
	'order' => 'ASC',
	'hide_empty' => $hide_empty,
	'include' => $ids
);

$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);

if ( $count > 0 ){
	foreach ( $product_categories as $product_category ) {
		
		// Product name can be printed with this
		echo $product_category->name;

		$args = array(
			'posts_per_page' => -1,
			'tax_query' => array(
			'relation' => 'AND',
				array(
					'taxonomy' => 'product_cat',
					'field' => 'slug',
					'terms' => $product_category->slug
				)
		),
			'post_type' => 'product',
			'orderby' => 'title,'
		);

	$products = new WP_Query( $args );
		while ( $products->have_posts() ) {

			$products->the_post();

			// Now you print all product details

			} // @end while

	  } // @end foreach
} // @end if


If you need some Custom Development in woo-commerce we are always here to help. contact us here.