├── .gitignore ├── .tool-versions ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── Gruntfile.coffee ├── LICENSE ├── README.md ├── bower.json ├── dist ├── cart.js ├── cart.min.js ├── rivets-cart.js └── rivets-cart.min.js ├── docs ├── built-with │ └── built-with.jade ├── docs.json ├── guide │ ├── guide.jade │ └── sections │ │ ├── core-api.md │ │ ├── core-api │ │ ├── adding-items.md │ │ ├── attributes.md │ │ ├── callbacks.md │ │ ├── cart-state.md │ │ ├── configuration.md │ │ ├── removing-items.md │ │ └── updating-items.md │ │ ├── data-api.md │ │ ├── data-api │ │ ├── adding-items.md │ │ ├── removing-items.md │ │ ├── submitting-forms.md │ │ └── toggling-items.md │ │ ├── dom-binding.md │ │ ├── events.md │ │ ├── getting-started.md │ │ ├── getting-started │ │ ├── browser-support.md │ │ ├── installation.md │ │ ├── module-loaders.md │ │ ├── next-steps.md │ │ └── setup.md │ │ ├── introduction.md │ │ └── introduction │ │ └── what.md ├── images │ ├── icon-500x500.png │ └── icon-64x64.png ├── index │ ├── index.jade │ └── sections │ │ ├── can-it-do-more.md │ │ ├── demo-advanced-code.md │ │ ├── demo-advanced.jade │ │ ├── demo-simple-code.md │ │ ├── demo-simple.jade │ │ ├── further-questions.md │ │ └── what-is-cart-js.md ├── less │ ├── bootstrap │ │ ├── alerts.less │ │ ├── badges.less │ │ ├── bootstrap.less │ │ ├── breadcrumbs.less │ │ ├── button-groups.less │ │ ├── buttons.less │ │ ├── carousel.less │ │ ├── close.less │ │ ├── code.less │ │ ├── component-animations.less │ │ ├── dropdowns.less │ │ ├── forms.less │ │ ├── glyphicons.less │ │ ├── grid.less │ │ ├── input-groups.less │ │ ├── jumbotron.less │ │ ├── labels.less │ │ ├── list-group.less │ │ ├── media.less │ │ ├── mixins.less │ │ ├── mixins │ │ │ ├── alerts.less │ │ │ ├── background-variant.less │ │ │ ├── border-radius.less │ │ │ ├── buttons.less │ │ │ ├── center-block.less │ │ │ ├── clearfix.less │ │ │ ├── forms.less │ │ │ ├── gradients.less │ │ │ ├── grid-framework.less │ │ │ ├── grid.less │ │ │ ├── hide-text.less │ │ │ ├── image.less │ │ │ ├── labels.less │ │ │ ├── list-group.less │ │ │ ├── nav-divider.less │ │ │ ├── nav-vertical-align.less │ │ │ ├── opacity.less │ │ │ ├── pagination.less │ │ │ ├── panels.less │ │ │ ├── progress-bar.less │ │ │ ├── reset-filter.less │ │ │ ├── resize.less │ │ │ ├── responsive-visibility.less │ │ │ ├── size.less │ │ │ ├── tab-focus.less │ │ │ ├── table-row.less │ │ │ ├── text-emphasis.less │ │ │ ├── text-overflow.less │ │ │ └── vendor-prefixes.less │ │ ├── modals.less │ │ ├── navbar.less │ │ ├── navs.less │ │ ├── normalize.less │ │ ├── pager.less │ │ ├── pagination.less │ │ ├── panels.less │ │ ├── popovers.less │ │ ├── print.less │ │ ├── progress-bars.less │ │ ├── responsive-embed.less │ │ ├── responsive-utilities.less │ │ ├── scaffolding.less │ │ ├── tables.less │ │ ├── theme.less │ │ ├── thumbnails.less │ │ ├── tooltip.less │ │ ├── type.less │ │ ├── utilities.less │ │ ├── variables.less │ │ └── wells.less │ ├── cartjs.less │ └── cartjs │ │ ├── buttons.less │ │ ├── callouts.less │ │ ├── cartjs.less │ │ ├── code.less │ │ ├── examples.less │ │ ├── images.less │ │ ├── jumbotrons.less │ │ ├── layout.less │ │ ├── navbar.less │ │ ├── navs.less │ │ ├── tables.less │ │ ├── thumbnails.less │ │ ├── type.less │ │ └── variables.less ├── reference │ ├── reference.jade │ └── sections │ │ ├── core-api.md │ │ ├── core-api │ │ ├── add-item.md │ │ ├── add-items.md │ │ ├── clear-attributes.md │ │ ├── clear.md │ │ ├── get-attribute.md │ │ ├── get-attributes.md │ │ ├── get-note.md │ │ ├── remove-item-by-id.md │ │ ├── remove-item.md │ │ ├── set-attribute.md │ │ ├── set-attributes.md │ │ ├── set-note.md │ │ ├── update-item-by-id.md │ │ ├── update-item-quantities-by-id.md │ │ └── update-item.md │ │ ├── data-api.md │ │ ├── data-api │ │ ├── data-cart-add.md │ │ ├── data-cart-remove-id.md │ │ ├── data-cart-remove.md │ │ ├── data-cart-submit.md │ │ ├── data-cart-toggle-attribute.md │ │ ├── data-cart-toggle.md │ │ ├── data-cart-update-id.md │ │ └── data-cart-update.md │ │ ├── dom-binding.md │ │ ├── events.md │ │ ├── events │ │ ├── ready.md │ │ ├── request-complete.md │ │ └── request-started.md │ │ ├── options.md │ │ └── options │ │ ├── data-api.md │ │ ├── debug.md │ │ ├── money-format.md │ │ ├── money-with-currency-format.md │ │ ├── request-body-class.md │ │ ├── rivets-models.md │ │ ├── weight-precision.md │ │ └── weight-unit.md ├── snippets │ └── variables.jade └── theme │ ├── assets │ ├── atlascoffeeclub.jpg │ ├── author-disco.png │ ├── author-gavin.png │ ├── bevybar.jpg │ ├── bootstrap.min.js │ ├── budweiser.jpg │ ├── carbon.jpg │ ├── cartjs.min.css │ ├── cartjs.zip │ ├── core-api-cart-state.png │ ├── dualitas.jpg │ ├── favicon.ico │ ├── focal-theme.jpg │ ├── glyphicons-halflings-regular.eot │ ├── glyphicons-halflings-regular.svg │ ├── glyphicons-halflings-regular.ttf │ ├── glyphicons-halflings-regular.woff │ ├── go-defense.jpg │ ├── greater-goods.jpg │ ├── intributeto.jpg │ ├── ippodotea.jpg │ ├── kagami-theme.jpg │ ├── keetsa.jpg │ ├── loader.gif │ ├── mast-brothers.jpg │ ├── moderncitizen.jpg │ ├── netaya.jpg │ ├── olsatools.jpg │ ├── pure-health-delivered.jpg │ ├── rallier.jpg │ ├── rivets-cart-compatibility.min.js │ ├── rivets-cart.js │ ├── rivets-cart.min.js │ ├── sargossa.jpg │ ├── shopify.i18n.min.js │ ├── susi-studio.jpg │ ├── tattly.jpg │ ├── the-foodbank-project.jpg │ ├── trytheworld.jpg │ ├── usabaseball.jpg │ ├── writeyboards.jpg │ └── yeezy.jpg │ ├── config │ ├── settings.html │ └── settings_data.json │ ├── layout │ └── theme.liquid │ ├── snippets │ ├── footer.liquid │ ├── navbar.liquid │ └── variables.liquid │ └── templates │ ├── 404.liquid │ ├── article.liquid │ ├── blog.liquid │ ├── cart.liquid │ ├── collection.liquid │ ├── customers │ ├── account.liquid │ ├── activate_account.liquid │ ├── addresses.liquid │ ├── login.liquid │ ├── order.liquid │ ├── register.liquid │ └── reset_password.liquid │ ├── index.liquid │ ├── page.built-with.liquid │ ├── page.contact.liquid │ ├── page.guide.liquid │ ├── page.liquid │ ├── page.reference.liquid │ ├── product.liquid │ └── search.liquid ├── npm-shrinkwrap.json ├── package.json ├── spec ├── cartjs │ └── formatters.js ├── fixtures │ └── carts.js ├── runner.html └── shopify │ ├── currencies.js │ └── option_selection.js └── src ├── cart.coffee ├── cartjs.coffee ├── core.coffee ├── data.coffee ├── export.coffee ├── item.coffee ├── queue.coffee ├── rivets.coffee └── utils.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | /docs/theme/config.yml 4 | /node_modules 5 | /bower_components 6 | /cartjs.zip 7 | -------------------------------------------------------------------------------- /.tool-versions: -------------------------------------------------------------------------------- 1 | python 2.7.13 2 | nodejs 5.12.0 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | before_script: 5 | - npm install -g grunt-cli 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | 4 | ## 1.1.0 - 2020-11-14 5 | ### Added 6 | - Explicitly pass `cache: false` in Ajax calls by default. 7 | - Add support for `selling_plan` property to `CartJS.addItem` method. 8 | 9 | ### Changed 10 | - Fix for non-Rivets build triggering reference errors. 11 | - Include hyphen and underscore as valid separators in properties for form submission. 12 | 13 | ## 1.0.2 - 2020-08-14 14 | ### Added 15 | - Cache-busting `v` parameter when fetching cart contents. 16 | 17 | ## 1.0.1 - 2020-05-17 18 | ### Changed 19 | - Minor documentation changes. 20 | 21 | ## 1.0.0 - 2020-05-16 22 | ### Added 23 | - Support for adding multiple line items with new `CartJS.addItems()` method. 24 | 25 | ### Changed 26 | - Updated some documentation. v1.0.0 - only took 6 years! 27 | 28 | ## 0.4.3 - 2019-08-20 29 | ### Added 30 | - Warning when trying to use money formatters without Shopify library support. 31 | 32 | ### Changed 33 | - Fixed bug preventing spaces in line item property names in `data-cart-submit` forms. 34 | 35 | ## 0.4.2 - 2019-08-20 36 | ### Added 37 | - Additional "built with" sites, dependency locks. 38 | 39 | ## 0.4.1 - 2016-08-16 40 | ### Added 41 | - New Rivets formatters: `times`, `divided_by`, `modulo` 42 | 43 | ### Changed 44 | - Fix issue with unnecessary line item properties being added with form submit 45 | 46 | ## 0.4.0 - 2016-02-11 47 | ### Added 48 | - New Rivets formatter: `array_element`, `array_first`, `array_last` 49 | - CONTRIBUTING.md 50 | - Simple Mocha-based test framework 51 | - `pluralize` formatter 52 | - `slice` formatter 53 | 54 | ### Changed 55 | - Fixed jQuery dependency from load-time to run-time 56 | 57 | ### Removed 58 | - IE8 Compatibility mode 59 | 60 | ## 0.3.9 - 2015-11-11 61 | ### Added 62 | - Two new Rivets formatters: `includes` and `match` 63 | 64 | ## 0.3.8 - 2015-10-27 65 | ### Added 66 | - `cart.ready` event, fired after initialisation finishes. 67 | 68 | ## 0.3.7 - 2015-10-26 69 | ### Changed 70 | - `getImageSizedUrl` filter now handles empty product images. 71 | 72 | ## 0.3.6 - 2015-10-24 73 | ### Added 74 | - New weight formatters `weight` and `weight_with_unit`, with corresponding 75 | settings `weightUnit` and `weightPrecision`. 76 | 77 | ## 0.3.5 - 2015-08-04 78 | ### Added 79 | - Include non-minified version of `rivets-cart.js` in `dist` 80 | 81 | ## 0.3.4 - 2015-07-16 82 | ### Added 83 | - Minor `console.log` compatibility fix for IE8 and IE9 browser support 84 | 85 | ## 0.3.3 - 2015-07-04 86 | ### Added 87 | - Compatible version of `rivets-cart.min.js` for non-ES5 browser support 88 | 89 | ## 0.3.2 - 2015-07-03 90 | ### Added 91 | - Made `getCart` a public method to allow force cart refreshes 92 | 93 | ### Changed 94 | - Fix for missing `removeItemById` public method 95 | 96 | ## 0.3.1 - 2015-06-28 97 | ### Changed 98 | - Fix for undefined Currency issue if not using Currency plugin. 99 | 100 | ## 0.3.0 - 2015-06-27 101 | ### Added 102 | - Support for dynamically rendering currency changes through money filters 103 | 104 | ### Changed 105 | - Upgraded Rivets.js library to v0.8.1 106 | 107 | ## 0.2.7 - 2015-05-18 108 | ### Added 109 | - Add `debug` setting for outputting of useful information to the console 110 | 111 | ## 0.2.6 - 2015-04-02 112 | ### Changed 113 | - Fixed incorrect method call when un-checking a checkbox using `data-cart-toggle` 114 | 115 | ## 0.2.5 - 2015-04-01 116 | ### Added 117 | - Add `prepend` and `append` formatters to Rivets 118 | 119 | ## 0.2.4 - 2015-03-13 120 | ### Added 121 | - Ability to specify callbacks through an `options` hash argument in Core API 122 | 123 | ## 0.2.3 - 2015-02-25 124 | ### Changed 125 | - Fixed potential issue using .data() instead of .attr() to access `data-` attributes 126 | - Added quantity adjustment example to advanced code example 127 | 128 | ## 0.2.2 - 2015-01-25 129 | ### Added 130 | - New `updateItemQuantitiesById` Core API method 131 | 132 | ## 0.2.1 - 2014-11-27 133 | ### Added 134 | - This new-format CHANGELOG, based on http://keepachangelog.com 135 | 136 | ### Changed 137 | - Further improvements to documentation, including better code examples 138 | 139 | ## 0.2.0 - 2014-10-11 140 | ### Added 141 | - New Data API methods for updating and clearing items 142 | - New `data-cart-render` methods for simple DOM updating 143 | 144 | ### Changed 145 | - Major improvements to documentation 146 | 147 | ### Removed 148 | - `getCart()` method no longer publicly exported 149 | 150 | ## 0.1.0 - 2014-10-09 151 | ### Added 152 | - Initial implementation; first "official" release 153 | - Core API 154 | - Data API 155 | - Rivets.js DOM bindings 156 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Cart.js 2 | We love to accept pull requests from anyone interested in helping out with the 3 | Cart.js project. Note that by participating in this project, you agree to both: 4 | 5 | - abide by Disco's [Open Source Code of Conduct]; 6 | - license your contribution under the [MIT License]. 7 | 8 | [Open Source Code of Conduct]: https://www.discolabs.com/open-source-code-of-conduct/ 9 | [MIT License]: LICENSE 10 | 11 | 12 | ## Submitting a Pull Request 13 | Fork, then clone the repo: 14 | 15 | git clone git@github.com:your-username/cartjs.git 16 | 17 | Install all dependencies for building and running tests: 18 | 19 | npm install 20 | 21 | Make sure the tests pass: 22 | 23 | npm test 24 | 25 | Make your change. Add tests for your change. Make the tests pass: 26 | 27 | npm test 28 | 29 | Push to your fork and [submit a pull request], based off the `develop` branch. 30 | 31 | [submit a pull request]: https://github.com/discolabs/cartjs/compare/ 32 | 33 | **Please ask first** before embarking on any significant pull request to avoid 34 | spending lots of time working on something we may not wish to merge. 35 | 36 | 37 | ## Cart.js Development Philosophy 38 | The overarching goal of Cart.js is to make common dynamic Javascript 39 | functionality as easy as possible for Shopify developers to implement. To help 40 | achieve that goal, we try to stick to the following philosophical guidelines: 41 | 42 | - **User-Friendly**: A junior web developer new to Shopify development should be 43 | able to pick up and find the library useful within a couple of hours. There 44 | shouldn't be any complex configuration or code required to use Cart.js. Docs 45 | should be as comprehensive as possible and easy to follow. 46 | - **Real-World Code**: Features should only be added when they've proven useful 47 | on real-world Shopify stores. Issues and feature requests made in a vacuum 48 | should be rejected until a concrete case can be made for them. 49 | - **Opinionated**: Pick a sensible way of doing things that works for 90% of use 50 | cases and make that the default, instead of allowing many different 51 | configurations. 52 | -------------------------------------------------------------------------------- /Gruntfile.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (grunt) -> 2 | 3 | # Initialise 4 | grunt.initConfig 5 | pkg: grunt.file.readJSON('package.json') 6 | 7 | meta: 8 | banner: 9 | '// Cart.js\n' + 10 | '// version: <%= pkg.version %>\n' + 11 | '// author: <%= pkg.author %>\n' + 12 | '// license: <%= pkg.licenses[0].type %>\n' 13 | 14 | coffee: 15 | build: 16 | options: 17 | join: true 18 | files: 19 | 'dist/cart.js': [ 20 | 'src/cart.coffee' 21 | 'src/item.coffee' 22 | 'src/cartjs.coffee' 23 | 'src/utils.coffee' 24 | 'src/queue.coffee' 25 | 'src/core.coffee' 26 | 'src/data.coffee' 27 | 'src/rivets.coffee' 28 | 'src/export.coffee' 29 | ] 30 | 31 | concat: 32 | build: 33 | options: 34 | banner: '<%= meta.banner %>' 35 | files: 36 | 'dist/rivets-cart.js': [ 37 | 'node_modules/sightglass/index.js', 38 | 'node_modules/rivets/dist/rivets.js', 39 | 'dist/cart.js' 40 | ] 41 | 42 | uglify: 43 | build: 44 | options: 45 | banner: '<%= meta.banner %>' 46 | report: 'gzip' 47 | files: 48 | 'dist/cart.min.js': 'dist/cart.js' 49 | 'dist/rivets-cart.min.js': 'dist/rivets-cart.js' 50 | 51 | clean: 52 | build: 53 | src: [] 54 | 55 | terraform: 56 | docs: 57 | options: 58 | data: { 59 | docs: grunt.file.readJSON('docs/docs.json'), 60 | version: '<%= pkg.version %>' 61 | } 62 | files: 63 | 'docs/theme/snippets/variables.liquid': 'docs/snippets/variables.jade' 64 | 'docs/theme/templates/index.liquid': 'docs/index/index.jade' 65 | 'docs/theme/templates/page.guide.liquid': 'docs/guide/guide.jade' 66 | 'docs/theme/templates/page.reference.liquid': 'docs/reference/reference.jade' 67 | 'docs/theme/templates/page.built-with.liquid': 'docs/built-with/built-with.jade' 68 | 69 | compress: 70 | docs: 71 | options: 72 | archive: 'cartjs.zip' 73 | files: [ 74 | flatten: true 75 | expand: true 76 | cwd: 'dist/' 77 | src: '*.js' 78 | ] 79 | 80 | copy: 81 | docs: 82 | files: [ 83 | src: 'dist/rivets-cart.min.js' 84 | dest: 'docs/theme/assets/rivets-cart.min.js' 85 | , 86 | src: 'cartjs.zip' 87 | dest: 'docs/theme/assets/cartjs.zip' 88 | , 89 | src: 'node_modules/shopify.i18n.js/dist/shopify.i18n.min.js' 90 | dest: 'docs/theme/assets/shopify.i18n.min.js' 91 | ] 92 | 93 | less: 94 | docs: 95 | options: 96 | compress: true 97 | files: 98 | 'docs/theme/assets/cartjs.min.css': 'docs/less/cartjs.less' 99 | 100 | watch: 101 | build: 102 | files: 'src/*.coffee' 103 | tasks: ['build'] 104 | docs: 105 | files: 'docs/**/*.less' 106 | tasks: ['less:docs'] 107 | 108 | mocha_phantomjs: 109 | test: ['spec/**/*.html'] 110 | 111 | grunt.loadNpmTasks 'grunt-contrib-clean' 112 | grunt.loadNpmTasks 'grunt-contrib-compress' 113 | grunt.loadNpmTasks 'grunt-contrib-concat' 114 | grunt.loadNpmTasks 'grunt-contrib-copy' 115 | grunt.loadNpmTasks 'grunt-contrib-coffee' 116 | grunt.loadNpmTasks 'grunt-contrib-less' 117 | grunt.loadNpmTasks 'grunt-contrib-uglify' 118 | grunt.loadNpmTasks 'grunt-contrib-watch' 119 | grunt.loadNpmTasks 'grunt-terraform' 120 | grunt.loadNpmTasks 'grunt-mocha-phantomjs' 121 | 122 | grunt.registerTask 'default', ['watch'] 123 | grunt.registerTask 'build', ['coffee:build', 'concat:build', 'uglify:build', 'clean:build'] 124 | grunt.registerTask 'test', ['build', 'mocha_phantomjs'] 125 | grunt.registerTask 'docs', ['build', 'terraform:docs', 'compress:docs', 'copy:docs', 'less:docs'] 126 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2016 Disco 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://badge.fury.io/js/shopify-cartjs) 2 | [](https://badge.fury.io/bo/shopify-cartjs) 3 | 4 | # Cart.js 5 | Cart.js is a Javascript library that makes it easy to add dynamic cart features 6 | to Shopify themes. 7 | 8 | It abstracts away the difficult parts of working with Shopify's AJAX API and 9 | provides a simple, consistent interface to manipulating a customer's cart. 10 | Without having to worry about the quirks in Shopify's endpoints or about making 11 | all of your requests synchronous, you can just write code like this: 12 | 13 | ```js 14 | function example() { 15 | // Clear the existing cart. 16 | CartJS.clear(); 17 | 18 | // Add 3x "12345678" items, with a custom "size" property of "XL". 19 | CartJS.addItem(12345678, 3, { 20 | "size": "XL" 21 | }); 22 | 23 | // Add multiple items in a single call. 24 | CartJS.addItems([ 25 | { 26 | id: 12345678, 27 | quantity: 3, 28 | properties: { 29 | "size": "XL" 30 | } 31 | }, 32 | { 33 | id: 87654321, 34 | quantity: 2 35 | } 36 | ]); 37 | 38 | // Set a custom cart note. 39 | CartJS.setNote('This is a custom cart note.'); 40 | } 41 | ``` 42 | 43 | Neat, huh? But that's just the beginning. 44 | 45 | Cart.js bundles two powerful optional modules that make it even easier to build 46 | dynamic carts into your themes: 47 | 48 | 49 | ### Data API Module 50 | Lets you use simple `data-*` markup attributes to hook in to Cart.js methods, 51 | without having to write *any* Javascript yourself. 52 | 53 | For example, to create a button that added a particular product to your cart, 54 | all that's required is: 55 | 56 | ```html 57 | 58 | ``` 59 | 60 | 61 | ### DOM Binding Module 62 | In combination with [Rivets.js][], the DOM Binding module lets you write HTML 63 | templates in your `.liquid` files that are re-rendered dynamically when the 64 | contents of your cart change, like this: 65 | 66 | ```html 67 |
67 | If you're adding multiple line items at once, you can use the `CartJS.addItems()` method, rather than multiple calls to `CartJS.addItem()`. 68 |
69 | 70 |71 | This method leverages new functionality added to the Shopify Ajax API in January 2020 that supports multiple items being added at once. 72 |
73 |79 | Shopify will collate multiple line items for the same variant into one — for example, if we clicked "Add Five Widgets" in the example above again, we'd end up with one line item with `"quantity": 10` instead of two line item with `"quantity: 5"`. 80 |
81 | 82 |83 | However, this *doesn't* apply when you add the same variant with custom line item properties that differ — if we changed the value of the `added_by` property and clicked the button, we'd end up with separate line items. 84 |
85 |15 | You should only ever *read* values from `CartJS.cart`, and avoid altering the object directly. 16 |
17 | 18 |19 | Assigning a value with code like `CartJS.cart.items[0].quantity = 5;` will make the change locally in the browser, but won't save it to the server. 20 | This means that the changes will be lost when the customer refreshes or navigates to a new page, or when Cart.js fetches an updated version of the cart from Shopify. 21 |
22 |20 | One important thing to note is that the `updateItem()` method takes the ***line number*** (the "index") of the item in the cart you'd like to update, not the variant ID. 21 | This is because it's possible (and quite common) to have multiple items in the cart with the same variant ID but with different properties. 22 |
23 | 24 |25 | Shopify uses a 1-based index for line items, so the index of the first line item in a cart is `1`, not `0` as is common in many programming languages. 26 |
27 | 28 |29 | If you'd like to update an item using just the variant ID, you can use `updateItemById()`, which operates the same way as `updateItem()` but takes the variant ID as the first parameter. 30 |
31 |25 | Because Ajax requests don't support `POST` requests with `enctype="multipart/form-data"`, Cart.js won't be able to submit your form if it contains `` elements. 26 |
27 |28 | This is something that we hope to auto-detect in future, but for now you should avoid using `data-cart-submit` on forms that need to upload files. 29 |
30 |35 | Be careful not to add the `data-cart-submit` attribute to forms that target `action="/checkout"`, as Cart.js will intercept the event and customers won't be redirected to the checkout. 36 |
37 |21 | Non-ES5 browsers (namely, IE8) don't support EcmaScript 5's 22 | `Object.defineProperty()` method, which Rivets.js uses to observe 23 | changes on data models and trigger DOM updates. 24 |
25 | 26 |27 | Earlier versions of Cart.js provided a workaround for this by bundling 28 | a number of ES5 shims and polyfills into the library, as well as 29 | forcibly binding and re-binding Rivets views whenever a charge to the 30 | cart occurred. This "compatibility mode" was dropped once Shopify 31 | changed their theme guidelines to only require support for Internet 32 | Explorer 9+. 33 |
34 |11 | The Core API is covered first in the documentation below, as it provides the foundation for the Data API. 12 |
13 | 14 |15 | However, if you just want to get stuck in to using Cart.js, feel free to go straight to the coverage of the Data API. 16 |
17 |5 | `cart.js` is the unminified source code, containing the Core and Data APIs. 6 |
7 | 8 |9 | `cart.min.js` is a minified version of the library, and also contains the Core and Data APIs. 10 |
11 | 12 |13 | `rivets-cart.min.js` is a minified version of Cart.js that also bundles the Rivets.js library. 14 | Together, they provide support for the DOM Binding functionality. 15 |
16 |