7

I am using cart attributes to add extra information for each product to the cart (from the product page). I am specifically using cart attributes over line item properties because the client needs to be able to edit this information in the order later on which line item properties don't allow.

Adding the information works just fine, the problem comes in when a customer decides to remove an item from the cart because although the item is removed, the cart attribute remains since it is not specifically tied to the product (other than a naming convention that we give it.)

So, is there a way to update/remove specific cart attributes?

Thanks in advance for your replies!

1 Answer 1

11

I have used cart attributes since they were introduced, and found most of the bugs associated with them along the way (which Shopify fixed along the way), giving me enough confidence to inform you on this question.

To edit them, simply provide the cart with the same key you used to create them, and change the information. If you provide an empty value, you'll remove the key. It is just a key:value store. As are line items, which by the way, are editable in my book. I managed to edit them by simply changing the value of the key, and by providing the line item index value, something maybe you overlooked?

Anyway, if you take the provided Shopify Javascript API code, and examine that small library, you'll see the functions used to create attributes. With a little minor change, you can introduce your own cart attribute CRUD functionality to suit your needs.

Here is an old example showing off updating the cart attributes, data is a string ie) "attributes[fizz]='buzz' (and note that you can thus store JSON):

updateCartAttributes: function(data, callback) {
  var params = {
    type: 'POST',
    url: '/cart/update.js',
    data: data,
    dataType: 'json',
    success: function(cart) {
      if ((typeof callback) === 'function') {
        callback(cart);
      }
      else {
        Shopify.api.onCartUpdate(cart);
      }
    },
    error: function(XMLHttpRequest, textStatus) {
      Shopify.api.onError(XMLHttpRequest, textStatus);
    }
  }; 
  $.ajax(params);
}
5
  • Hi David, I was just revisiting this answer for a different project but I noticed that you said "As are line items, which by the way, are editable in my book." Are you referring to anytime before the checkout process or can the Store owner change them later in the admin?
    – ChrisC
    Commented Mar 2, 2013 at 19:43
  • 1
    Line item properties are only editable before checkout. After checkout they are read-only. There is no access to change them using the API. Commented Mar 2, 2013 at 21:52
  • Tiny update needed here - remove the apostrophes around buzz. The string attributes[fizz]=buzz just needs to be wrapped in double quotes like this: "attributes[fizz]=buzz" Commented Apr 30, 2015 at 9:48
  • @DavidLazar - is there any solution for customizable subscriptions that need modifiable post-checkout line properties that doesn't involve a brand new subscription since they aren't editable post checkout? Commented Aug 25, 2015 at 14:55
  • 1
    @DavidLazar "If you provide an empty value, you'll remove the key." I have not found this to be the case - it leaves the line item property attached, just with a blank value. This is via /cart/change.js.
    – iamkeir
    Commented Sep 21, 2019 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.