How to *remove* line item property (not set to blank) - possible API bug/lacking documentation?

Solved
Shopify Partner
192 0 27

Hey folks,

 

When using /cart/change.js, is it possible to remove a line item property?

 

Setting a property to '', false, nil, null or blank results in the line item property being set to blank rather than being removed. My use case need requires the property to be removed completely.

 

I cannot find any documentation on this - would this be considered a bug? Or simply that the API is lacking in the ability to fully remove properties?

 

For reference:

$.ajax({
  type: 'POST',
  url: '/cart/change.js',
  dataType: 'json',
  data: {
    id: lineID,
    properties: {
      propertyID: null // or '', false, nil, blank...
    }
  }
});

 

 

http://iamkeir.com
0 Likes
Highlighted
Shopify Partner
192 0 27

So, I thought I would try a blank object for the properties. This does not work either - leaves the existing properties unaffected.

 

$.ajax({
  type: 'POST',
  url: '/cart/change.js',
  dataType: 'json',
  data: {
    id: lineID,
    properties: {}
  }
});

However, if you specify some new, unrelated property, this replaces all the previous properties... but this isn't so much a 'remove property', as 'replace all properties'.

 

$.ajax({
  type: 'POST',
  url: '/cart/change.js',
  dataType: 'json',
  data: {
    id: lineID,
    properties: {
      'someNewProperty': 'whatever'
    }
  }
}); 

Is that perhaps the best we can get in the ever-quirky world of Shopify?

http://iamkeir.com
0 Likes

Success.

Shopify Expert
3994 14 321

Just to clarify your example, there is no such thing in Javascript as nil, like var x = nil; Instead, if you want some variable or object to point at nothing you'd use null. But you know that I am sure ;)

 

As for setting a property to false or an empty string, that makes sense. If something was equal to say 'norseman' and you replaced it with '', or false, nothing in Javascript world would object.

 

Just as an exercise you can remove the key that offends you from your properties object, straight up Javascript there (removing keys from a hash), and then assign the new property object using the cart change call for that line item. Since Shopify replaces the object, you get the behaviour you need without anything quirky going on.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Shopify Partner
192 0 27

Thanks @HunkyBill, always appreciate your input.

 

Yeah, when I tried 'nil/blank' etc. it was passed as a string aimed at Shopify rather than JS. Useful for anyone else reading this thread though.

 

It was really just to find out if I was missing something or if the only way was indeed to replace the property object with a new, updated version - which seems to be the case.

http://iamkeir.com
0 Likes