Do not display empty sub-collections on the screen

Do not display empty sub-collections on the screen

Bezpallko
Tourist
9 0 2

Hello everyone. I created 6 metafields for subcollections (from 1 to 6) in Settings > Custom data > Collections > Choose List of References as the type and select Collection. In the collection metafield section I added references to subcollections that should be displayed on the screen and in the customizer I added metafields to the list of collections. But now I have a problem that the main collections have a different number of subcollections, usually from 3 to 6 and subcollections that are empty are also displayed on the screen. What is the solution to this issue so that empty subcollections are not displayed on the screen?

Reply 1 (1)

Hasan112
Shopify Partner
152 11 21

 

To solve the issue of empty subcollections being displayed on the screen, you can add a check to ensure that only non-empty subcollections are rendered. Here's how you can approach it:

  1. Check for Empty Subcollections: In your Liquid template, before displaying each subcollection, you should check whether the collection has any products or content. If the collection is empty, it won't be displayed.

  2. Modify the Liquid Code: Modify your template code that loops through the metafields to display the subcollections. You can use the empty property to check if a collection is empty. For example:

 

liquid
 
{% for metafield in collection.metafields.your_namespace %} {% assign subcollection = collections[metafield.value] %} {% if subcollection.products_count > 0 %} <!-- Render the subcollection --> <div class="subcollection"> <h3>{{ subcollection.title }}</h3> <!-- You can add other content here like products, etc. --> </div> {% endif %} {% endfor %}
 

This code checks if each subcollection has any products before displaying it. If subcollection.products_count > 0, the subcollection will be shown, otherwise, it will be skipped.

  1. Ensure Proper Metafield Configuration: Double-check that the metafield references in the main collections are set up correctly and that the subcollections are properly populated with products.
- If you need assistance with your store, feel free to contact us at zayan.1@yahoo.com or WhatsApp!
- Hire me, if you want to Design, Re-design, Develop a store or make changes to the pre-built store.
- If its helpful, please Mark as Accepted Solution, reply helpful? Click Like. Or Coffee Tip Thank you!