Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit 98b4e13

Browse files
authored
@shopify/theme-cart: v1.0.0 (#45)
* Refactor theme-cart to remove jQuery and add tests * Resolve with the cart state object for updateItem and setNote * setNote() -> updateNote() * setAttributes() -> updateAttributes()
1 parent 48d08ab commit 98b4e13

14 files changed

+1576
-151
lines changed

packages/theme-cart/README.md

+186
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# @shopify/theme-cart
22

3+
Theme Cart is a tiny library (<1kb min+gzip) that facilitates requests to [Shopify's Cart API](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api) and makes it easier to manage cart state.
4+
35
## Getting Started
46

57
Theme Scripts can be used in any theme project. To take advantage of semantic versioning and easy updates, we recommend using NPM or Yarn to include them in your project:
@@ -13,3 +15,187 @@ and then import the functions you wish to use through ES6 imports:
1315
```
1416
import * as cart from '@shopify/theme-cart`;
1517
```
18+
19+
If you prefer not to use a package manager, you can download the latest version of Theme Cart and include it in your project manually from the following links:
20+
21+
- [theme-cart.js](http://unpkg.com/@shopify/theme-cart@latest/dist/theme-cart.js)
22+
- [theme-cart.min.js](http://unpkg.com/@shopify/theme-cart@latest/dist/theme-cart.min.js)
23+
24+
These files make Theme cart accessible via the `Shopify.theme.cart` global variable.
25+
26+
## Browser Support
27+
28+
Theme Cart uses two APIs not available to legacy browsers, Fetch and Promise. If you wish to support legacy browsers, make sure you add the following dependencies to your project:
29+
30+
```
31+
yarn add unfetch es6-promise
32+
```
33+
34+
and then import them before you import Theme Cart:
35+
36+
```js
37+
// Only need to import these once
38+
import 'unfetch/polyfill';
39+
import 'es6-promise/auto';
40+
41+
// Import @shopify/theme-cart anywhere you need it
42+
import * as cart from '@shopify/theme-cart';
43+
```
44+
45+
## Methods
46+
47+
### getState()
48+
49+
Returns a promise which fulfills with the [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
50+
51+
```js
52+
cart.getState().then(state => {
53+
console.log(`Cart State: ${state}`);
54+
});
55+
```
56+
57+
### getItemIndex([key](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-key))
58+
59+
Returns a promise which fulfills with the line item number.
60+
61+
> ⚠️ Line item indexes are base 1, so the first item in your cart has an index value of 1.
62+
63+
```js
64+
cart.getItemIndex(key).then(index => {
65+
console.log(`Line item with ${key} has an index of ${index}`);
66+
});
67+
```
68+
69+
### getItem([key](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-key))
70+
71+
Returns a promise which fulfills with the matching [line item](https://help.shopify.com/en/themes/liquid/objects/line_item).
72+
73+
```js
74+
cart.getItem(key).then(item => {
75+
if (item === null) {
76+
console.log(`Line item does not exist with key '${key}'`);
77+
} else {
78+
console.log(`Found a line item with key '${key}':`, item);
79+
}
80+
});
81+
```
82+
83+
### addItem([id](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-variant_id), { [quantity](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-quantity), [properties](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-properties) })
84+
85+
Adds an item to your cart. If no quantity is specified, 1 item is added. Returns a promise which fulfills with the matching [line item](https://help.shopify.com/en/themes/liquid/objects/line_item).
86+
87+
> ⚠️ If the quantity specified is more then what is available, the promise will reject and the cart state will remain unchanged
88+
89+
```js
90+
cart.addItem(id, { quantity, properties }).then(item => {
91+
console.log(
92+
`An item with a quantity of ${quantity} was added to your cart:`,
93+
item
94+
);
95+
});
96+
```
97+
98+
### updateItem([key](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-key), { [quantity](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-quantity), [properties](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-properties) })
99+
100+
Changes the quantity and/or properties of an existing line item. Returns a promise which fulfills with the [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
101+
102+
> ⚠️ If the quantity specified is more then what is available, the promise will still be fullfilled and the maximum number of available items is added to the cart.
103+
104+
> ⚠️ When you modify properties, the key value of the item changes. Make sure you record the new key value from the item returned by the resolved promise.
105+
106+
```js
107+
cart.updateItem(key, { quantity }).then(state => {
108+
var item = state.items.find(item => item.key === key);
109+
console.log(`The item with key '${key}' now has quantity ${item.quantity}`);
110+
});
111+
```
112+
113+
### removeItem([key](https://help.shopify.com/en/themes/liquid/objects/line_item#line_item-key))
114+
115+
Removes an item from the cart. Returns a promise which fulfills with the updated [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
116+
117+
```js
118+
cart.removeItem(key).then(state => {
119+
console.log(`There is now ${state.items.length} item(s) in your cart`);
120+
});
121+
```
122+
123+
### clearItems()
124+
125+
Removes all items from the cart. Returns a promise which fulfills with the updated [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
126+
127+
```js
128+
cart.clearItems().then(state => {
129+
console.log(`Your cart is now empty.`);
130+
});
131+
```
132+
133+
### getAttributes()
134+
135+
Returns a promise which fulfills with the [cart attributes](https://help.shopify.com/en/themes/customization/cart/get-more-information-with-cart-attributes).
136+
137+
```js
138+
cart.getAttributes().then(attributes => {
139+
if (attributes.giftWrapped) {
140+
console.log('The customer wants their order gift wrapped.');
141+
}
142+
});
143+
```
144+
145+
### updateAttributes()
146+
147+
Sets the value of the [cart attributes](https://help.shopify.com/en/themes/customization/cart/get-more-information-with-cart-attributes). Returns a promise which fulfills with the [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
148+
149+
> ⚠️ This method will only add or overwrite attribute values. Passing an empty object will result in no change.
150+
151+
```js
152+
cart.getAttributes({ giftWrapped: false }).then(state => {
153+
if (state.attributes.giftWrapped) {
154+
console.log('The customer wants their order gift wrapped.');
155+
}
156+
});
157+
```
158+
159+
### clearAttributes()
160+
161+
Clears all values from the [cart attributes](https://help.shopify.com/en/themes/customization/cart/get-more-information-with-cart-attributes) object. Returns a promise which fulfills with the [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
162+
163+
### getNote()
164+
165+
Returns a promise which fulfills with the value of the cart note.
166+
167+
```js
168+
cart.getNote().then(note => {
169+
console.log('The customer has written the following note:', note);
170+
});
171+
```
172+
173+
### updateNote()
174+
175+
Sets the value of the cart note. Returns a promise which fulfills with the [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
176+
177+
```js
178+
cart.updateNote().then(state => {
179+
console.log('The customer has written the following note:', state.note);
180+
});
181+
```
182+
183+
### clearNote()
184+
185+
Clears the value of the cart note. Returns a promise which fulfills with the [cart state](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-cart).
186+
187+
```js
188+
cart.clearNote().then(() => {
189+
console.log('The cart note has been cleared');
190+
});
191+
```
192+
193+
### getShippingRates()
194+
195+
Returns a promise which fulfills with an array of [shipping rate objects](https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#get-shipping-rates).
196+
197+
```js
198+
cart.getShippingRates().then(rates => {
199+
console.log('Got shipping rates:', rates);
200+
});
201+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"token": "7d10e2cf893223c1830b507f024c56f8",
3+
"note": null,
4+
"attributes": {},
5+
"original_total_price": 0,
6+
"total_price": 0,
7+
"total_discount": 0,
8+
"total_weight": 0,
9+
"item_count": 0,
10+
"items": [],
11+
"requires_shipping": false,
12+
"currency": "USD"
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"token": "6475a6c35b7fd07fe832782fb341a83b",
3+
"note": null,
4+
"attributes": {},
5+
"original_total_price": 58000,
6+
"total_price": 58000,
7+
"total_discount": 0,
8+
"total_weight": 0,
9+
"item_count": 1,
10+
"items": [
11+
{
12+
"id": 38558517710,
13+
"properties": null,
14+
"quantity": 1,
15+
"variant_id": 38558517710,
16+
"key": "38558517710:fe217df576fefe94bff27eff2010a09a",
17+
"title": "Ratio Eight - Nickel / 110 Volts",
18+
"price": 58000,
19+
"original_price": 58000,
20+
"discounted_price": 58000,
21+
"line_price": 58000,
22+
"original_line_price": 58000,
23+
"total_discount": 0,
24+
"discounts": [],
25+
"sku": "",
26+
"grams": 0,
27+
"vendor": "Ratio",
28+
"taxable": true,
29+
"product_id": 10270178382,
30+
"gift_card": false,
31+
"url": "/products/ratio-eight?variant=38558517710",
32+
"image":
33+
"https://cdn.shopify.com/s/files/1/1906/1399/products/InSitu-5_8fd16703-2540-4b02-bc89-b950053d533f.png?v=1492799854",
34+
"handle": "ratio-eight",
35+
"requires_shipping": true,
36+
"product_type": "",
37+
"product_title": "Ratio Eight",
38+
"product_description":
39+
"\nThis is a demonstration store. You can purchase products like this from the real Ratio online store.\nTechnically speaking.\nModel: Eight Warranty: 10 years Capacity: Brews up to 40oz (eight 5oz cups) Dimensions: 13.5\" deep x 9\" wide x 14\" tall\nRatio Eight changes everything with precision brewing technique, top-quality construction, and a design that will elevate any environment for years to come. Each machine is hand assembled and tested in Portland, Oregon. That Eight has a powerful 1600 watt heating element in 110 (US &amp; Canada) or 220 volt. Eight's carafe holds Chemex paper filters or Able Kone stainless steel filter.\nExpected to ship: 2-3 weeksFree shipping | 10 years warranty",
40+
"variant_title": "Nickel / 110 Volts",
41+
"variant_options": ["Nickel", "110 Volts"]
42+
}
43+
],
44+
"requires_shipping": true,
45+
"currency": "CAD"
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"shipping_rates": [
3+
{
4+
"name": "Ground Shipping",
5+
"price": "8.00",
6+
"delivery_date": null,
7+
"source": "shopify"
8+
},
9+
{
10+
"name": "Expedited Shipping",
11+
"price": "15.00",
12+
"delivery_date": null,
13+
"source": "shopify"
14+
},
15+
{
16+
"name": "Express Shipping",
17+
"price": "30.00",
18+
"delivery_date": null,
19+
"source": "shopify"
20+
}
21+
]
22+
}

0 commit comments

Comments
 (0)