0
votes

not a dev, just trying to hack something together. In our Shopify site, have some logic that tags new blogs articles in a specific blog with a an order.id. So this blog has posts with one order.id per post.

On customer record (account), I made a grid where I want to show all articles where article.tags matches customer.orders.

I made an array for all the customer's order ids, and trying to compare this array with the article.tags array and show only the articles where there are matches in the two arrays.

Please help!

This is on customers/account.liquid:

     <div class="table-wrap">
        <table class="full table--responsive">
          <thead>
            <tr>
              <th>POST NAME</th>
            </tr>
          </thead>
          <tbody>

             {% assign myorders = '' %}
        {% for order in customer.orders  %}
           {% capture myorders %}
              {{ myorders }} {{ order.id }}
           {% endcapture %}
            {% endfor %}

             {% for article in blogs.my-posts.articles  %}
         {% if article.tags contains myorders %}

                   <!--SHOW THE MATCHING ARTICLES HERE-->

                   <tr>
                     <td class="underline"><strong><a href="{{ article.url }}">{{ article.title | capitalize }}</a></strong></td>
                   <tr>

                 {% else %}

                     You have no posts.

                 {% endif %}
         {% endfor %}
          </tbody>
        </table>              
      </div>
1

1 Answers

0
votes

One thing. Using capture will continuously overwrite your last capture, so putting that in a for loop is problematic. You'll only ever have the last order in customer orders as your result.

So for a start, you might want to work on building up a string that will continuously grow to contain the order ids. Once you have that string, you can perhaps use it. You would then go through articles. Note that tags are best dealt with in an array. So you'd split that string of tags on comma, and for each resulting tag in the array you'd loop over, you'd want to see if the tag was in the string of order ids you previously built.

None of this is too hard, but it is not obvious how to build up to success. My suggestion is to establish each need in a small step of code, and then dump the code out in the source of the page Shopify renders. Use the view source to find your results, and check that they are correct. If you just whip up a whole recipe like your current one, and it fails at any point to produce, like it does, you have no clue where the error is. Often it is easy enough to just use comments in the course and then ctrl+f to find the clue, like maybe the word "testeefizzle":

<!-- testeefizzle {{ dump crap here to see it }} -->

Once you achieve glory on one small piece, built on that, with more code.