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?
Topic summary
Main issue: Empty subcollections (Shopify collections referenced via metafields) are being displayed, even when they contain no products.
Context: The store uses 6 collection metafields (List of References) per main collection to show 3–6 subcollections. Because some referenced subcollections are empty, they still render in the UI.
Proposed solution: Add a Liquid check to only render subcollections that have products. Iterate over the collection’s metafields, resolve each reference to a collection, and conditionally display it when subcollection.products_count > 0.
Implementation details: Example Liquid logic is provided—loop through collection.metafields in the chosen namespace, assign subcollection = collections[metafield.value], and wrap the rendering block in an if subcollection.products_count > 0 condition.
Notes and prerequisites: Verify metafield configuration and that referenced subcollections are properly populated. Terms: “metafields” are Shopify custom data fields; “Liquid” is Shopify’s templating language; “subcollections” are collections referenced via metafields.
Status: No confirmation of success yet; the thread remains open without a final resolution.
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:
-
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.
-
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 %}
{{ subcollection.title }}
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.
- 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.