Skip to content

Instantly share code, notes, and snippets.

@chrisjhoughton
Last active September 9, 2023 07:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chrisjhoughton/893f536698e2c27cb7f5 to your computer and use it in GitHub Desktop.
Save chrisjhoughton/893f536698e2c27cb7f5 to your computer and use it in GitHub Desktop.
Remove a Shopify cart attribute
{% if cart.attributes.yourCartAttribute %}
<script>
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: 'attributes[yourCartAttribute]=',
dataType: 'json'
});
</script>
{% endif %}
@cjauvin
Copy link

cjauvin commented Oct 15, 2017

This is incredibly useful (and incredibly undocumented), many thanks!

@alpriest
Copy link

Alternatively (and equally undocumented!), you could use the Shopify API directly.

{% if cart.attributes.yourCartAttribute %}
  <script>
    Shopify.updateCartAttributes([{
      key: 'yourCartAttribute',
      value: ''
    }], function(onError) {});
  </script>
{% endif %}

@emcmanus
Copy link

Provide null as the value wherever you normally send the request (e.g. XHR, fetch, et al), and Shopify will remove the key from its attributes hash.

In the original example, you probably want to remove the dataType param, since json isn't a valid MIME type and the payload isn't JSON (obj[key]=value is form urlencoded).

@skillmatic-co
Copy link

@alpriest @emcmanus by setting the value to null, it doesn't remove the key for me from the order. It just sets the key's value to null. Anyway to actually remove the entire note attribute via the API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment