How do I get a list of 10 most recently purchased products in Shopify?

129 Views Asked by At

I'm trying to create a "Buy Again" list in Shopify. My coding skills are very basic, but I know enough to get by. I can loop through a customer's previous 10 orders, but that's not really what I want. An order could have 10 line items which could make the list very long. What I want is a Buy Again List limited up to the last 10 items purchased. Is there a way to do that?

Here's what I currently have:

{% assign customer_orders = customer.orders %}
{% for order in customer_orders limit:10 %}
  {% assign buy_again_list = order.line_items %}
  {% for line_item in buy_again_list %}
    {% unless line_item.product == nil %}
    <div class="buy-again-item">
      <a class="buy-again-card" href="{{ line_item.product.url }}">
        <img class="buy-again-image" src="//printsaverepeat.com/cdn/shop/{{ line_item.product.featured_image }}" width="100" height="100">
        <div class="buy-again-info">
          <div class="buy-again-title">
            {{ line_item.product.title }}
          </div>
          <div class="buy-again-sku">
            {{ line_item.variant.sku }}
          </div>
          <div class="buy-again-price">
            {{ line_item.variant.price | money }}
          </div>
        </div>
      </a>
    </div>
    {% endunless %}
  {% endfor %}
{% endfor %}
2

There are 2 best solutions below

2
YELLAMADDI NAVEEN On

Looks like you are missing the logic to break if the Product count is greater than 10 and avoiding duplicates. Try this

{% if customer and customer.orders %}
  {% assign customer_orders = customer.orders %}
  {% assign displayed_products = "" | split: "%" %} <!-- Create an array to store displayed products -->
  {% assign count = 0 %} <!-- Counter for limiting the number of displayed products -->

  {% for order in customer_orders limit:10 %}
    {% for line_item in order.line_items %}
      {% unless line_item.product == nil or displayed_products contains line_item.product.id %}
        <div class="buy-again-item">
          <a class="buy-again-card" href="{{ line_item.product.url }}">
            <img class="buy-again-image" src="{{ line_item.product.featured_image | img_url: '100x100' }}" width="100" height="100">
            <div class="buy-again-info">
              <div class="buy-again-title">
                {{ line_item.product.title }}
              </div>
              <div class="buy-again-sku">
                {{ line_item.variant.sku }}
              </div>
              <div class="buy-again-price">
                {{ line_item.variant.price | money }}
              </div>
            </div>
          </a>
        </div>
        {% assign displayed_products = displayed_products | push: line_item.product.id %} <!-- Add product to displayed products array -->
        {% assign count = count | plus: 1 %} <!-- Increment counter -->
        {% if count >= 10 %} <!-- Break if 10 products are displayed -->
          {% break %}
        {% endif %}
      {% endunless %}
    {% endfor %}
  {% endfor %}
{% endif %}

I have added the logic to avoid dup. products and also break if product count is more than 10.

1
YELLAMADDI NAVEEN On
{% if customer and customer.orders %}
{% assign displayed_products = "" | split: "," %}
{% assign count = 0 %}

{% for order in customer.orders limit:10 %}
    {% for line_item in order.line_items %}
        {% if line_item.product and displayed_products contains line_item.product.id == false %}
            <div class="buy-again-item">
                <a class="buy-again-card" href="{{ line_item.product.url }}">
                    <img class="buy-again-image" src="{{ line_item.product.featured_image | img_url: '100x100' }}" width="100" height="100">
                    <div class="buy-again-info">
                        <div class="buy-again-title">
                            {{ line_item.product.title }}
                        </div>
                        <div class="buy-again-sku">
                            {{ line_item.variant.sku }}
                        </div>
                        <div class="buy-again-price">
                            {{ line_item.variant.price | money }}
                        </div>
                    </div>
                </a>
            </div>

            {% assign displayed_products = displayed_products | push: line_item.product.id %}
            {% assign count = count | plus: 1 %}

            {% if count >= 10 %}
                {% break %}
            {% endif %}
        {% endif %}
    {% endfor %}
{% endfor %}

{% endif %}

THis should work well ,you can increase the limit as needed too.