Exclude items in cart from product listing

Shopify Partner
5 0 1

I am trying to show recommended products on my cart template. It appears the built-in recommended functionality from Shopify only works on product pages, so I am using a manual collection to output products. I want to exclude any products from this list that are already in the cart though. This is what I have currently:

 

<div class="cart__recommendations">
  {% capture cart_items %}
    {% for item in cart.items %}
      {{item.product.id}}
      {% if forloop.last == false %},{% endif%}
    {% endfor %}
  {% endcapture %}
  {% assign allCartProducts = cart_items | split:',' %}
  {{cart_items}}

  <h5>Other Products You Make Like</h5>
  <ul class="listing--products">
    {% for product in collections.cart-recommendations.products limit:4 %}
      {% if product.id != cart_items %}
      <li class="list-item--product__container">
        {% include 'product-card-grid', max_height: 250 %}
      </li>
      {% endif %}
    {% endfor %}
  </ul>
</div>

I know the problem is the below line, but not sure how I am supposed to create an array of items in the cart and exclude those from another array.

{% if product.id != cart_items %}
0 Likes
Highlighted
Shopify Partner
5 0 1

Using this article (https://community.shopify.com/c/Shopify-Design/problem-when-comparing-product-id-with-value-inside-a...) I was able to figure out a different way to compare the array and essentially swap what I had. This is what worked for me:

 

        <div class="cart__recommendations">
          {% for item in cart.items %}
            {% capture cart_items %}
              {{cart_items}} {{item.product.id}}
            {% endcapture %}
          {% endfor %}

          <h5>Other Products You Make Like</h5>
          <ul class="listing--products">
            {% for product in collections.cart-recommendations.products limit:4 %}
              {% if cart_items contains product.id %}
              {% else %}
              <li class="list-item--product__container">
                {% include 'product-card-grid', max_height: 250 %}
              </li>
              {% endif %}
            {% endfor %}
          </ul>
        </div>
0 Likes