Search code examples
pluginsbuilderoctobercms

How to remove empty categories?


I have a two scenarios to solve :)

  1. Let's say I built plugin (with builder) where I have products and categories. Now on page with listed categories I have all of them, but I want to not display empty categories. How to remove empty categories form listing?

  2. Second scenario. Some of products have options and some none. Products and options have relation table. On single product page I have some like:

product description options for this product (that work well)

But in case of product doesn't have any options I want to have:

product description text "this product have no options" (or not display any text at all)

So far I have tried something like:

{{ if option in record.options == true }} display options {{ else }} "this product have no options" {{ endif }}

But this doesn't work at all.

Is there a way to check for existing options for product?

Thanks for your time.


Solution

  • For Question number 1

    Here you can put a condition on $query->has('products', '>', 0) but seems you are using the builder plugin so you can simply put a condition on the category product count.

    {% for category in categories %}
      {% if category.products|length > 0 %}
        {{category.name}}. // here you will get only category which has products.
      {% endif %}
    {% endfor %}
    

    For Question number 2

    You can do something like this

    {% if record.options|length > 0 %}
      show them
    {% else %}
      no options
    {% endif %}
    

    if any doubt please comment.