How to hide the featured collection section if the collection is empty

Hi Experts

I am using the Enterprise theme and currently have a featured collection section for our sale items. However, if we do not have any items on sale I would like to hide this section. I have added a ‘custom HTML/liquid’ section with the below code.

{% if collection.products_count < 1 %}

#shopify-section-template--25939786662173__featured_collection_pVELqV {display: none;}

{% endif %}

Example is viewable here:

I know this isn’t correct, but would appreciate any assistance.

Many thanks

Nick

You need to loop over the products and check whatever the logic is that use to call a product “on sale”

{% assign has_product_on_sale = false %}
{% for product in collection %}
  {% if product.price < product.compare_at_price %}
     {% assign has_product_on_sale  = true %}
  {% endif %}
{% endfor %}

{% if has_product_on_sale %}
  #shopify-section-template--25939786662173__featured_collection_pVELqV {display: none;}
{% endif %}

Before reaching for that band-aid and living with it forever fix the real issues upstream first:

  • If the section displays even when a collection is empty contact the theme developers and make that feature request; it really is annoying how many things work like this in themes.
  • make the collection conditions be empty when there’s no sale products either by tags or pricess etc;
  • And or using an automation app like flow or mechanic to control tags
    • or make a it a manual collection populated by such a custom automations logic

Alternative:
If you have time to waste or need to get hands on with some sort of “AI” for the experience there’s also asking the sidekick tool to try and randomly generate a section:block with the wanted requirements and more.
This can be good testing/training to get used to such a workflow for one-offs or making prototypes even if they don’t fully work before bringing in experts.
https://help.shopify.com/en/manual/online-store/themes/customizing-themes/theme-editor/shopify-magic/generate-blocks

Hi @Nick_S2

Go to: Online Store → Themes → … → Edit code Open:

sections/featured-collection.liquid

(or similarly named file used by Enterprise)

Step 2: Wrap the section with a Liquid condition

At the VERY TOP of the file, add:

{% if section.settings.collection != blank and section.settings.collection.products_count > 0 %}

At the VERY BOTTOM of the file, add:

{% endif %}

@devcoders that worked perfectly! Many thanks

Nick

Hi @Nick_S2

Thank you for your response. It’s good to know that it’s worked for you. Kindly feel free to get back to me if you need any further assistance. If helpful, please like all posts. :white_check_mark:

Best regards,

Devcoder :laptop: