Tagged: product categories
-
AuthorPosts
-
July 13, 2022 at 10:49 pm #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
LyseJuly 14, 2022 at 9:18 am #1358281Hey 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,
IsmaelJuly 15, 2022 at 3:19 am #1358421Hi 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
LyseJuly 15, 2022 at 10:05 am #1358436Hi,
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,
IsmaelJuly 15, 2022 at 4:15 pm #1358508Hi 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
LyseJuly 16, 2022 at 11:43 pm #1358592Hi,
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:
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:
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:
the results:
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:
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' );
Best regards,
MikeJuly 17, 2022 at 5:13 pm #1358609HI 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
LyseJuly 17, 2022 at 8:23 pm #1358616Hi,
Thanks for the feedback, I tested my first solution again on my test site and achieve this:
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,
MikeJuly 17, 2022 at 8:38 pm #1358617Hi 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
July 17, 2022 at 8:55 pm #1358619Hi,
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,
MikeJuly 17, 2022 at 9:01 pm #1358621Hi Mike,
Below are temporary login credentials
Thank you
LyseJuly 17, 2022 at 11:36 pm #1358628Hi,
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 justimg src="#"
so no images are showing in the table right now.
This is the function I addedfunction 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,
MikeJuly 17, 2022 at 11:59 pm #1358631Hi 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.
LyseJuly 18, 2022 at 12:45 am #1358632July 18, 2022 at 3:54 pm #1358667Hi 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.
LyseJuly 18, 2022 at 7:28 pm #1358680Hi,
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 -
AuthorPosts
- The topic ‘Show product category description even when there is no products’ is closed to new replies.