skip to Main Content

I have a page on my site that lists all the vendors in my shop like so:

<div class="vendor-list" id="designers-a-to-z">
    <ul>
        {% assign current = "" %}
        {% capture alphabet %}
            -A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
        {% endcapture %}
        {% assign letters = alphabet | split: '-' %}
        {% assign its_a_letter = false %}
        {% for vendor in shop.vendors %}
            {% assign vendor_first_letter = vendor | strip | upcase | slice : 0 %}
            {% for letter in letters %}
                {% if vendor_first_letter == letter %}
                    {% assign its_a_letter = true %}
                    {% break %}
                {% endif %}
            {% endfor %}
            {% if its_a_letter %}
                {% unless vendor_first_letter == current %}
                    <h3><span class="anchor" id="designers-{{ vendor_first_letter }}"></span>{{ vendor_first_letter }}</h3>
                {% endunless %}
                <li class="vendor-list-item">
                    <a href="/collections/{{ vendor | handleize }}/in-stock">{{ vendor }}</a>
                </li>
            {% else %}
                {% assign vendor_first_letter = "#" %}
                {% unless vendor_first_letter == current %}
                    <h3><span class="anchor" id="designers-{{ vendor_first_letter }}"></span>{{ vendor_first_letter }}</h3>
                {% endunless %}
                <li class="vendor-list-item">
                    <a href="/collections/{{ vendor | handleize }}/in-stock">{{ vendor }}</a>
                </li>
            {% endif %}
            {% assign current = vendor_first_letter %}
        {% endfor %}
    </ul>
</div>

Some of these vendors don’t currently have any items in stock so it’s pointless to have them show up here. Is it possible to display only the vendors that have items in stock within their collection?

I currently have 2 collection tags in my store for ‘in-stock’ and ‘sold-out’ to help filter them with our filter menu and that is appended to the url so we only show customers in stock items.

2

Answers


  1. Chosen as BEST ANSWER

    I was able to get this working by writing it like this:

    {% for vendor in shop.vendors %}
        {% for collection in collections %}
            {% if collection.title == vendor %}
                {% if collection.all_tags contains 'in-stock' %}
                    then the conditions for checking the first letter and displaying the vendor, etc. nothing new
    

    It only worked, however, on my dev site that I was testing it on and when I pushed it to production I got liquid error: memory limits exceeded. My dev site is definitely lacking in collections and products compared to my live site so this may work for people who have smaller sites.


  2. Since a product has a vendor, and a product has inventory, you could just check that way. It would be insanely slow and obnoxious but hey! That is what a hosted platform is for, to turn crazy Liquid into the right tight HTML.

    Note the Liquid for product.available

    Returns true if a product is available for purchase. Returns false if all of the products variants' inventory_quantity values are zero or less, and their inventory_policy is not set to "Allow users to purchase this item, even if it is no longer in stock."
    

    So while you loop through your vendor list, loop through all the products, checking the product vendor for a match, and the availability. If they don’t suit you… skip the vendor.

    Would be interesting to see how slow this is… but you never know till you try…

    Another approach, perhaps smarter, is to iterate all your in-stock products once. Build your vendor list out of that, instead of shop.vendors.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search