Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #1358263

    Hi

    How can I get the product category description I created to show even when there is no products found in that product category?

    See links where I have created a product category descriptions and where these don’t show because no products exist in those product categories just yet:
    https://diyjewellery.com/bails/bails-attached/bails-attached-gold/
    https://diyjewellery.com/bails/bails-glue-on/bails-glue-on-gold/

    I contacted WooCommerce support this morning and they said that the default behavior is to display that message when no products exist in the category. They said that the theme most likely controls this behavior and I should contact you for a code snippet that would allow me to display the description even though there are not products found – keeping the message on the results page to say “No products were found matching your selection.”

    Much appreciated
    Lyse

    #1358281

    Hey tremblayly,

    Thank you for the inquiry.

    You have to override the archive template by creating a copy of the woocommerce/templates/archive-product.php template in your child theme. Remove the condition around line 48.

    if ( woocommerce_product_loop() ) {
    

    And remove this hook around line 88.

    do_action( 'woocommerce_no_products_found' );
    

    Best regards,
    Ismael

    #1358421

    Hi Ismael,

    I’m not a PHP expert, so would need a bit more clarification on how to remove the condition you specify:

    <strong>if ( woocommerce_product_loop() ) {</strong>
    
    	/**
    	 * Hook: woocommerce_before_shop_loop.
    	 *
    	 * @hooked woocommerce_output_all_notices - 10
    	 * @hooked woocommerce_result_count - 20
    	 * @hooked woocommerce_catalog_ordering - 30
    	 */
    	do_action( 'woocommerce_before_shop_loop' );
    
    	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();
    
    	/**
    	 * Hook: woocommerce_after_shop_loop.
    	 *
    	 * @hooked woocommerce_pagination - 10
    	 */
    	do_action( 'woocommerce_after_shop_loop' );
    } <strong>else {
    	/**
    	 * Hook: woocommerce_no_products_found.
    	 *
    	 * @hooked wc_no_products_found - 10
    	 */
    	do_action( 'woocommerce_no_products_found' );
    }</strong>
    

    So do I delete the what’s in bold here?

    Thanks
    Lyse

    #1358436

    Hi,

    Thank you for the update.

    Try to replace the whole block with this code.

    
    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked woocommerce_output_all_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    do_action('woocommerce_before_shop_loop');
    
    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();
    
    /**
     * Hook: woocommerce_after_shop_loop.
     *
     * @hooked woocommerce_pagination - 10
     */
    do_action('woocommerce_after_shop_loop');
    

    Best regards,
    Ismael

    #1358508

    Hi Ismael,
    I replaced the code and uploaded the template file to my Child theme but nothing changed.
    see results here: https://diyjewellery.com/bails/bails-glue-on/bails-glue-on-gold/
    Thanks
    Lyse

    #1358592

    Hi,
    Thanks for your patience, as I understand your request you have added a html table to your category Description and you want it to show when there are no products in that category, I was not able to get the above solution to work on my test site, but I was able to create a shortcode that you can add to empty categories to show your custom description:
    2022-07-16_008.jpg
    Try adding this code to the end of your functions.php file in Appearance ▸ Editor:

    function archive_description_callback() {
    	$term_object = get_queried_object();
    	$archive_description = $term_object->description;
    	echo '<div class="container archive-description">'.$archive_description.'</div>';
        return $output;
    }
    add_shortcode( 'archive_description_shortcode', 'archive_description_callback' );
    function remove_archive_description_shortcode() { ?>
      <script>
    (function($){
    $('.archive-description').contents().filter(function() {
      return this.nodeType == 3
    }).each(function(){
      this.textContent = this.textContent.replace('[archive_description_shortcode]','');
    });
    })(jQuery);
    </script>
      <?php
    }
    add_action('wp_footer', 'remove_archive_description_shortcode');

    and add this shortcode to the category Description [archive_description_shortcode]
    and this is the result:
    2022-07-16_009.jpg
    Perhaps this will help.

    As a side note I had originally thought that you wanted to show the category full width banner, and I found that this shows without and modifications:
    2022-07-16_010.jpg
    the results:
    2022-07-16_011.jpg

    In the process I stumbled on to this solution for just showing the category image without the description when using the default Category Styling and not the full width banner:
    2022-07-16_012.jpg
    use this shortcode: [banner_image_shortcode]
    This code to the end of your functions.php file in Appearance ▸ Editor:

    function banner_image_callback() {
    	ob_start();
    	avia_woocommerce_overview_banner_image();
    	$banner_image = ob_get_clean();
    	echo '<div class="container">'.$banner_image.'</div>';
        return $output;
    }
    add_shortcode( 'banner_image_shortcode', 'banner_image_callback' );

    for this result:
    2022-07-16_013.jpg

    Best regards,
    Mike

    #1358609

    HI Mike,
    I have added a code snippet to my site to add the shortcode. I then added the shortcode to the following category descriptions but nothing changes even after I cleared all cache including server cache.

    https://diyjewellery.com/bails/bails-attached/bails-attached-gold/
    https://diyjewellery.com/bails/bails-glue-on/bails-glue-on-gold/

    I don’t use the fullwidth banner options as they don’t perform as expected.

    Thank you
    Lyse

    #1358616

    Hi,
    Thanks for the feedback, I tested my first solution again on my test site and achieve this:
    2022-07-16_009.jpg
    I wanted to check that I didn’t leave a step out, so please include an admin login in the Private Content area so I can examine your site closer.

    Have you removed the custom archive-product.php template in your child theme, from the original solution above, as this is not a factor of the shortcode solution?
    Also is the woocommerce/templates/archive-product.php file from the original solution above, in it’s original state, without any modifications?

    Best regards,
    Mike

    #1358617

    Hi Mike
    I don’t think you are on the right category page. You should be on the Gold Attached Bails – this link https://diyjewellery.com/bails/bails-attached/bails-attached-gold/

    Lyse

    #1358619

    Hi,
    Yes, I understand that your page is not showing the custom table at the top of your empty category page.
    The table that I copied was from a non-empty category page that is showing a table that I assume is similar to what should show on your empty category page
    https://diyjewellery.com/bails/bails-attached/bails-attached-gold/
    It is only an example, since I can not log in to your site a see the actual backend of your empty category page, I have to guess what the expected result is.
    Please include an admin login in the Private Content area so I can examine your page closer.

    Best regards,
    Mike

    #1358621

    Hi Mike,

    Below are temporary login credentials

    Thank you
    Lyse

    #1358628

    Hi,
    Thank you for the login, I’m not sure why this was not working on your site, but I did notice that your category page was different than my default version, probably due to one of your plugins.
    Anyways I adjusted my approach with a new function in your PHP snippets plugin, I left the old one disabled and you can delete it.
    So the first improvement for this one is that it checks the category page for the ID “search-fail” which is only found on empty category pages, if found it injects the shortcode that gets the category description, so you don’t need to add the shortcode on the pages and then remove it once you add products to the category later.
    But you need to add your html table without line breaks, you will see what I mean when you check the back end of the two pages I adjusted for you:
    https://diyjewellery.com/bails/bails-attached/bails-attached-gold/
    https://diyjewellery.com/bails/bails-glue-on/bails-glue-on-gold/
    Also note that for these two page you didn’t add an image url just img src="#" so no images are showing in the table right now.
    2022-07-17_006.jpg
    This is the function I added

    function archive_description_callback() {
    	$term_object = get_queried_object();
    	$archive_description = $term_object->description;
    	echo '<div class="container archive-description">'.$archive_description.'</div>';
        return $output;
    }
    add_shortcode( 'archive_description_shortcode', 'archive_description_callback' );
    function add_archive_description_shortcode() { ?>
        <script>
      (function($){	
          var short_code = '<?php echo do_shortcode("[archive_description_shortcode]"); ?>';
    	  if ($('#top.archive.tax-product_cat .entry-content-wrapper').attr("id") == "search-fail") {
          $(short_code).insertBefore( ".container_wrap_first" );
    	  }
      })(jQuery);
      </script>
        <?php
      }
      add_action('wp_footer', 'add_archive_description_shortcode');

    Best regards,
    Mike

    #1358631

    Hi Mike,
    When I clicked on the links you provided I saw the description. I added two images to the table for the Gold Attached Bails category and saved the changes. Now the description no longer works.

    I also wanted the description to show like all of the others I’m created that have products. Here’s a link to an example: https://diyjewellery.com/bails/bails-peg/

    Thanks so much for your efforts so far.
    Lyse

    #1358632

    Hi,
    Thanks for the feedback the reason you table was not working was because it had line breaks in it, I tried to explain this above:
    2022-07-17_008.jpg
    2022-07-17_009.jpg
    I also adjusted the placement
    2022-07-17_010.jpg
    please check.

    Best regards,
    Mike

    #1358667

    Hi Mike,
    When I choose to look at the visual instead of the HTML, it adds line breaks. I used <br /> to format my table content.
    It now looks exactly as I want it.

    I much appreciated all of your efforts to make this work.

    Have a great week.
    Lyse

    #1358680

    Hi,
    Glad we were able to help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 16 posts - 1 through 16 (of 16 total)
  • The topic ‘Show product category description even when there is no products’ is closed to new replies.