0

I have the following code which inserts data to the bottom of a product category. It works perfectly however it only displays when a product is actually in the category, id like it to display in empty categories too. What am I doing wrong ?

    add_action( 'woocommerce_after_shop_loop', 'wpm_product_cat_archive_add_meta' );
function wpm_product_cat_archive_add_meta() {
  $t_id = get_queried_object()->term_id;
  $term_meta = get_option( "taxonomy_$t_id" );
  $term_meta_content = $term_meta['custom_term_meta'];
  if ( $term_meta_content != '' ) {
    echo '<h2 style="text-align:center; font-weight:bold;">Local info</h2><div class="term-description"><p>';
      echo apply_filters( 'the_content', $term_meta_content );
    echo '</p></div>';
  }
}
5
  • 1
    An action hook woocommerce_after_shop_loop is used only to display breadcrumbs in latest woocommerce so if there breadcrumb is not needed then your data will also not displayed so try to improve your code by selecting another action hook. Can you please tell whether the page is product page or category page? It would be great if you share screenshot. Commented Mar 6, 2017 at 12:08
  • Hi Faisal , The page is a category and we use Yoast breadcrumbs not woocommerce. The code above is nothing more than a short description of the category which goes under the products so the after_shop_loop would be ideal placement , i did try the woocommerce_after_main_content hook but it done the same , it worked on categories with products but if there were no products it doesnt show.
    – Randomer11
    Commented Mar 6, 2017 at 12:18
  • You must have to use woocommerce_after_main_content. Please see my answer below for more detail. Commented Mar 6, 2017 at 12:27
  • If I use the woocommerce_after_main_content hook the content doesnt display at all, even the categories with products.
    – Randomer11
    Commented Mar 6, 2017 at 12:39
  • You can add condition to your custom function (see my updated ans below) so that data will display only if the page is category. Like: if (is_product_category()) { /*code*/ } Commented Mar 6, 2017 at 12:55

1 Answer 1

1

You have used woocommerce_after_shop_loop action hook which is within the following condition:

<?php if ( have_posts() ) : ?>

You should use woocommerce_after_main_content in order to display your data. Because it is outside of this condition. Your data will display even there are no products in the category.

UPDATE:

You can add condition to your custom function so that data will display only if the page is category.

Read this on how to use conditions for WooCommerce:

Some from there:

is_product_category()

Returns true when viewing a product category archive.

is_product_category( 'shirts' )

When the product category page for the ‘shirts’ category is being displayed.

is_product_category( array( 'shirts', 'games' ) )

When the product category page for the ‘shirts’ or ‘games’ category is being displayed.

So you can update your code as follows:

if ( $term_meta_content != '' && is_product_category()) {
    echo '<h2 style="text-align:center; font-weight:bold;">Local info</h2><div class="term-description"><p>';
      echo apply_filters( 'the_content', $term_meta_content );
    echo '</p></div>';
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.