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
Answers
Looks like you are missing the logic to break if the Product count is greater than 10 and avoiding duplicates. Try this
I have added the logic to avoid dup. products and also break if product count is more than 10.
{% endif %}
THis should work well ,you can increase the limit as needed too.