0
votes

I have seen this question and answer. That does not work.


Setup

I am running:

  • WordPress 5.4.1
  • WooCommerce 4.1.1

I have a custom theme that overrides some of the WooCommerce templates by placing my own templates in: themes/my_theme/woocommerce/template-name.php

I have established that the shop page (homepage) uses the template archive-product.php. I have copied the this from plugins/woocommerce/templates/archive-product.php into my theme and made some minor HTML changes which work perfectly. There are no functional changes in my theme's copy, just some HTML.

The problem

I want the homepage to only show the store categories, as thumbnails. There is an option to set the shop page to show categories instead of products:

Appearance > Customize > WooCommerce > Catalogue

Shop page display has been set to Show Categories

However the shop homepage seems to ignore this setting entirely, and it still shows the products. It seems surprising that WooCommerce's own template does not honour this setting!

How do I find this setting in the template and then show the categories (as thumbnails) on the shop homepage?

Is there an equivalent to woocommerce_product_loop() for looping categories?


As a side note, the Storefront theme does honour the setting but Storefront does not have the template archive-product.php. Storefront seems to be highly abstracted and after much debugging of it / trying to take it apart I have so far not worked out which template file it is using for the shop page.

My theme is already in production and I just want to make an update so that the homepage shows the categories instead of going directly into the products list.

1

1 Answers

0
votes

I found a workable solution. The WooCommerce default templates don't support the setting to show categories on the Shop page.

However using a shortcode with do_shortcode(), and a condition this can be achieved as follows:

if (is_shop()) {

  echo do_shortcode('[product_categories hide_empty="0"]');

} else {

  woocommerce_product_loop_start();

  if ( wc_get_loop_prop( 'total' ) ) {
    while ( have_posts() ) {
      the_post();

      /**
      * Hook: woocommerce_shop_loop.
      */
      do_action( 'woocommerce_shop_loop' );

      wc_get_template_part( 'content', 'product' );
    }

  }

  woocommerce_product_loop_end();
}

Still:

  • I would like to know how to pick up the 'show categories' customisation setting shown in the question, so my theme responds to that.
  • Is there a better way than using do_shortcode(), this feels like a bit of a hack