0

I am trying to add a reorder button to my customer's order history pages to automatically reorder items they've already purchased. This would send them back to the cart with items already prefilled and ready for checkout. Each product item has custom properties attached to it. Everything seems to work fine, except that when I add the items back into the cart, I cannot get the custom line item properties to display.

I'm using the order-to-cart.liquid snippet.

On line 18, it seems the custom properties are being called:

properties: {{ line_item.properties | json }}

I've tried to modify the code to add them:

request.send(JSON.stringify({
  'quantity':order.items[0].quantity,
  'id':order.items[0].variant_id,
  'properties':order.items[0].properties
}));

But this does not work.

Based on the comment below, I tried to loop through the item properties:

request.send(JSON.stringify({
  'quantity':order.items[0].quantity,
  'id':order.items[0].variant_id,
  'properties': {
    {% for line_item in order.line_items %}
      {% for property in line_item.properties %}
        '{{ property.first }}': '{{ property.last }}'{% unless forloop.last %},{% endunless %}
      {% endfor %}
    {% endfor %}
  }
}));

But I get syntax error:

SyntaxError: missing } after property list
note: { opened at line 403, column 26

Which seems to reference the property list of request.send

The outputted example json is:

        /* Setup the order object. Extend this as needed. */
        var order = {
          items:[

              {
                variant_id: 16320547225634,
                product_id: 1782978904098,
                properties: [["Arrangement Type",""],["Enclosed Card",""],["Occasion \u0026amp; Comments","Condolescens"],["Delivery Date","Mar 29, 2019"]],
                available: true
              },

              {
                variant_id: null,
                product_id: 1776316743714,
                properties: [["Arrangement Type",""],["Enclosed Card",""],["Occasion \u0026amp; Comments",""],["Delivery Date","Mar 24, 2019"]],
                available: null
              },

              {
                variant_id: 16319970017314,
                product_id: 1782916808738,
                properties: [["Arrangement Type","Seasonal"],["Enclosed Card","Love and best wishes"],["Occasion \u0026amp; Comments","Just Because"],["Delivery Date","Mar 25, 2019"]],
                available: true
              },

              {
                variant_id: 16311468687394,
                product_id: 1780877819938,
                properties: [["Arrangement Type","Large vase with orchids"],["Enclosed Card","Steal the warm chair right after you get up when owners are asleep, cry for no apparent reason sleeps on my head."],["Occasion \u0026amp; Comments","Birthday so make extra special!"],["Delivery Date","Apr 10, 2019"]],
                available: true
              }

          ]
        };



request.send(JSON.stringify({
            'quantity':order.items[0].quantity,
            'id':order.items[0].variant_id,
            'properties': {


                    'Arrangement Type': '',

                    'Enclosed Card': '',

                    'Occasion & Comments': 'Condolescens',

                    'Delivery Date': 'Mar 29, 2019'



                    'Arrangement Type': '',

                    'Enclosed Card': '',

                    'Occasion & Comments': '',

                    'Delivery Date': 'Mar 24, 2019'



                    'Arrangement Type': 'Seasonal',

                    'Enclosed Card': 'Love and best wishes',

                    'Occasion & Comments': 'Just Because',

                    'Delivery Date': 'Mar 25, 2019'



                    'Arrangement Type': 'Large vase with orchids',

                    'Enclosed Card': 'Steal the warm chair right after you get up when owners are asleep, cry for no apparent reason sleeps on my head.',

                    'Occasion & Comments': 'Birthday so make extra special!',

                    'Delivery Date': 'Apr 10, 2019'


              }
          }));

My cart.liquid file calls the custom properties like so:

            {% if property_size > 0 %}

                  {% for p in item.properties %}
                    {% assign first_character_in_key = p.first | truncate: 1, '' %}
                    {% unless p.last == blank or first_character_in_key == '_' %}
                      <div class="label-info">{{ p.first }}:
                        <strong class="input-info">{{ p.last }}</strong>
                      </div>
                    {% endunless %}
                  {% endfor %}

            {% endif %}

But I'm not sure if I need to modify this to accommodate any properties already created.

The custom line item properties need to show up when added back to the cart, otherwise the reorder function won't really be a time saver as it will force customers to go back to each product page to re-add info into the custom fields.

I'm not that versed in liquid so not quite sure what needs to be done to modify the snippet or cart templates. Any help you could provide would be greatly appreciated!

Many thanks, Mark

Mark Genest
  • 11
  • 1
  • 7

1 Answers1

0

The reason 'properties':order.items[0].properties does not work is because it contains an array with the name and value of the line item property.

To set this up we will need to turn the array into and object and then pass that into the request.send function.

A function I found that achieves this can be seen in this post. You can copy and paste this function into the orderItems function.

Once that is added we then create the properties variable, passing in order.items[0].properties as an argument to turn this into an object.

This variable is added along with the 'quantity' and 'id' in request.send.

Here is how the orderItems function will look once everything is added:

/* Simple function add to cart */
var orderItems = function(){
  if(!order.items.length){ return }
  if(!order.items[0].available){ 
    checkQueue();
  }

  function objectify(array) {
    return array.reduce(function(p, c) {
      p[c[0]] = c[1];
      return p;
    }, {});
  }

  var properties = objectify(order.items[0].properties);

  var request = new XMLHttpRequest();
  request.open('post', '/cart/add.js', true);
  request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  request.onload = function() {
    var resp = request.responseText;
    if (request.status >= 200 && request.status < 400) {
      checkQueue();
    } else { /* add your error handling in here */ }
  };
  request.onerror = function() {
    /* add your error handling in here */
  };
  request.send(JSON.stringify({
    'quantity':order.items[0].quantity,
    'id':order.items[0].variant_id,
    properties 
  }));

};

Hope that helps!

David Mc
  • 109
  • 1
  • 6
  • Unfortunately get a syntax error. Please see updated question for more detail. Thanks! – Mark Genest Mar 31 '19 at 20:39
  • Hey Mark! I see the liquid approach I posted initially was pulling in the properties for every line item and since there were no commas separating them it resulted in an error. I've removed the liquid and showed how this is possible with Javascript. – David Mc Apr 01 '19 at 18:17
  • That seems to have done it. Works great now. Thanks so much! – Mark Genest Apr 04 '19 at 04:43