@liquidcommerce/elements-sdk 2.6.0-beta.37 → 2.6.0-beta.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/cart.md ADDED
@@ -0,0 +1,387 @@
1
+ # Cart
2
+
3
+ The cart component provides a complete shopping cart experience as a slide-out drawer. Customers can review items, adjust quantities, apply promo codes, and proceed to checkout. The cart updates instantly when products are added from anywhere on the page.
4
+
5
+ ## Overview
6
+
7
+ The cart element handles everything shopping cart related:
8
+ - Slide-out drawer UI with item list
9
+ - Real-time quantity adjustments
10
+ - Promo code application
11
+ - Subtotal and fee calculations
12
+ - Proceed to checkout flow
13
+ - Cross-tab synchronization
14
+ - Session persistence
15
+
16
+ The cart syncs in real-time across browser tabs and persists between sessions, so customers can continue where they left off.
17
+
18
+ ---
19
+
20
+ ## Declarative Setup (HTML Attributes)
21
+
22
+ Configure the cart button with data attributes. By default, you get a floating cart button in the top-right corner.
23
+
24
+ ### Default Floating Button
25
+
26
+ No configuration needed - just include the SDK:
27
+
28
+ ```html
29
+ <script
30
+ defer
31
+ type="text/javascript"
32
+ data-liquid-commerce-elements
33
+ data-token="YOUR_API_KEY"
34
+ data-env="production"
35
+ src="https://assets-elements.liquidcommerce.us/all/elements.js"
36
+ ></script>
37
+ ```
38
+
39
+ This gives you a floating cart button with item count badge.
40
+
41
+ ### Cart Button in Specific Container
42
+
43
+ Place the cart button inside a specific element:
44
+
45
+ ```html
46
+ <nav>
47
+ <div id="header-cart"></div>
48
+ </nav>
49
+
50
+ <script
51
+ defer
52
+ type="text/javascript"
53
+ data-liquid-commerce-elements
54
+ data-token="YOUR_API_KEY"
55
+ data-env="production"
56
+ data-cart-badge-button="header-cart"
57
+ src="https://assets-elements.liquidcommerce.us/all/elements.js"
58
+ ></script>
59
+ ```
60
+
61
+ ### Position Modifiers
62
+
63
+ Control where the button appears relative to a target:
64
+
65
+ ```html
66
+ <!-- Inside the target (default) -->
67
+ <script data-cart-badge-button="header-cart" ...></script>
68
+
69
+ <!-- Above the target -->
70
+ <script data-cart-badge-button="above:.header-logo" ...></script>
71
+
72
+ <!-- Below the target -->
73
+ <script data-cart-badge-button="below:#main-nav" ...></script>
74
+
75
+ <!-- Replace the target -->
76
+ <script data-cart-badge-button="replace:.old-cart" ...></script>
77
+ ```
78
+
79
+ ### Cart Button Variants
80
+
81
+ | Attribute | Description |
82
+ |-----------|-------------|
83
+ | `data-cart-button` | Cart button without badge |
84
+ | `data-cart-badge-button` | Cart button with item count badge |
85
+
86
+ ### Mobile Cart Button
87
+
88
+ Configure different placements for desktop and mobile:
89
+
90
+ ```html
91
+ <script
92
+ defer
93
+ type="text/javascript"
94
+ data-liquid-commerce-elements
95
+ data-token="YOUR_API_KEY"
96
+ data-env="production"
97
+ data-cart-badge-button="above:.desktop-nav"
98
+ data-cart-mobile-badge-button="below:.mobile-header"
99
+ src="https://assets-elements.liquidcommerce.us/all/elements.js"
100
+ ></script>
101
+ ```
102
+
103
+ Mobile attributes support the same position modifiers (`above:`, `below:`, `replace:`, `inside:`).
104
+
105
+ ### Custom Cart Toggle
106
+
107
+ Use your own button to toggle the cart:
108
+
109
+ ```html
110
+ <button data-lce-cart-toggle-button>My Cart</button>
111
+ <span data-lce-cart-items-count>0</span>
112
+
113
+ <script
114
+ defer
115
+ type="text/javascript"
116
+ data-liquid-commerce-elements
117
+ data-token="YOUR_API_KEY"
118
+ data-env="production"
119
+ data-cart-button-hidden
120
+ src="https://assets-elements.liquidcommerce.us/all/elements.js"
121
+ ></script>
122
+ ```
123
+
124
+ | Attribute | Description |
125
+ |-----------|-------------|
126
+ | `data-lce-cart-toggle-button` | Makes any element a cart toggle |
127
+ | `data-lce-cart-items-count` | Auto-updates with item count |
128
+ | `data-cart-button-hidden` | Hides the default cart button |
129
+
130
+ Use `data-lce-cart-items-count="keep-zero"` to show count even when cart is empty.
131
+
132
+ ### Hide Cart Button Completely
133
+
134
+ For manual cart control via JavaScript:
135
+
136
+ ```html
137
+ <script
138
+ defer
139
+ type="text/javascript"
140
+ data-liquid-commerce-elements
141
+ data-token="YOUR_API_KEY"
142
+ data-env="production"
143
+ data-cart-button-hidden
144
+ src="https://assets-elements.liquidcommerce.us/all/elements.js"
145
+ ></script>
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Programmatic Setup (JavaScript API)
151
+
152
+ For full control, use the client API to inject cart components.
153
+
154
+ ### Cart Element (Full Cart View)
155
+
156
+ Inject the cart into a container for a dedicated cart page:
157
+
158
+ ```javascript
159
+ const client = await Elements('YOUR_API_KEY', { env: 'production' });
160
+
161
+ const component = await client.injectCartElement('cart-container');
162
+ ```
163
+
164
+ ### UI Helper Methods
165
+
166
+ Quick methods for common cart UI needs:
167
+
168
+ ```javascript
169
+ // Cart button in a container
170
+ client.ui.cartButton('header-cart', true); // true = show item count
171
+
172
+ // Floating cart button
173
+ client.ui.floatingCartButton(true); // true = show item count
174
+
175
+ // Display cart subtotal
176
+ client.ui.cartSubtotal('subtotal-display');
177
+
178
+ // Display item count
179
+ client.ui.cartItemsCount('count-display', {
180
+ keepZero: true // Show "0" instead of hiding when empty
181
+ });
182
+ ```
183
+
184
+ ---
185
+
186
+ ## Actions
187
+
188
+ Control the cart programmatically.
189
+
190
+ ### Open / Close / Toggle
191
+
192
+ ```javascript
193
+ // Open the cart drawer
194
+ window.elements.actions.cart.openCart();
195
+
196
+ // Close the cart drawer
197
+ window.elements.actions.cart.closeCart();
198
+
199
+ // Toggle cart drawer
200
+ window.elements.actions.cart.toggleCart();
201
+ ```
202
+
203
+ ### Add Products
204
+
205
+ Add items to cart programmatically (great for "Buy Now" buttons or bundles):
206
+
207
+ ```javascript
208
+ await window.elements.actions.cart.addProduct(
209
+ [
210
+ {
211
+ identifier: '00619947000020',
212
+ fulfillmentType: 'shipping',
213
+ quantity: 2
214
+ },
215
+ {
216
+ identifier: '00832889005513',
217
+ fulfillmentType: 'onDemand',
218
+ quantity: 1
219
+ }
220
+ ],
221
+ true // Open cart after adding (optional)
222
+ );
223
+ ```
224
+
225
+ ### Promo Codes
226
+
227
+ ```javascript
228
+ // Apply a promo code
229
+ await window.elements.actions.cart.applyPromoCode('SUMMER20');
230
+
231
+ // Remove the applied promo code
232
+ await window.elements.actions.cart.removePromoCode();
233
+ ```
234
+
235
+ ### Reset Cart
236
+
237
+ Clear all items from the cart:
238
+
239
+ ```javascript
240
+ await window.elements.actions.cart.resetCart();
241
+ ```
242
+
243
+ ### Get Cart Details
244
+
245
+ Retrieve current cart state:
246
+
247
+ ```javascript
248
+ const cart = window.elements.actions.cart.getDetails();
249
+
250
+ console.log(cart.itemCount);
251
+ console.log(cart.subtotal);
252
+ console.log(cart.items);
253
+ ```
254
+
255
+ ---
256
+
257
+ ## Events
258
+
259
+ Listen to cart interactions and state changes.
260
+
261
+ ### Subscribing to Events
262
+
263
+ ```javascript
264
+ window.addEventListener('lce:actions.cart_updated', (event) => {
265
+ const { current, previous } = event.detail.data;
266
+ console.log('Cart updated:', current.itemCount, 'items');
267
+ });
268
+ ```
269
+
270
+ ### Available Events
271
+
272
+ | Event | Description | Key Data |
273
+ |-------|-------------|----------|
274
+ | `cart_loaded` | Cart data ready | Full cart state |
275
+ | `cart_opened` | Drawer opened | boolean |
276
+ | `cart_closed` | Drawer closed | boolean |
277
+ | `cart_updated` | Items or totals changed | previous, current state |
278
+ | `cart_reset` | Cart cleared | boolean |
279
+ | `cart_failed` | Operation failed | cartId, message |
280
+ | `cart_item_added` | Item added | cartId, itemId, quantity |
281
+ | `cart_item_removed` | Item removed | cartId, itemId |
282
+ | `cart_item_quantity_increase` | Quantity increased | cartId, itemId, quantity, previousQuantity |
283
+ | `cart_item_quantity_decrease` | Quantity decreased | cartId, itemId, quantity, previousQuantity |
284
+ | `cart_item_engraving_updated` | Personalization changed | cartId, itemId, engravingLines |
285
+ | `cart_product_add_success` | Programmatic add succeeded | cartId, itemsAdded, identifiers |
286
+ | `cart_product_add_failed` | Programmatic add failed | cartId, identifiers, error |
287
+ | `cart_promo_code_applied` | Discount applied | cartId, discount, newSubtotal |
288
+ | `cart_promo_code_removed` | Discount removed | cartId, newSubtotal |
289
+ | `cart_promo_code_failed` | Discount rejected | cartId, error |
290
+
291
+ ### Example: Analytics Integration
292
+
293
+ ```javascript
294
+ // Track add to cart
295
+ window.addEventListener('lce:actions.cart_item_added', (event) => {
296
+ const { itemId, quantity } = event.detail.data;
297
+ analytics.track('Cart Item Added', { itemId, quantity });
298
+ });
299
+
300
+ // Track cart abandonment prevention
301
+ window.addEventListener('lce:actions.cart_closed', () => {
302
+ const cart = window.elements.actions.cart.getDetails();
303
+ if (cart.itemCount > 0) {
304
+ // User closed cart with items - trigger retention flow
305
+ showRetentionOffer();
306
+ }
307
+ });
308
+ ```
309
+
310
+ ---
311
+
312
+ ## Use Cases
313
+
314
+ ### Smart Cart Timing
315
+
316
+ Open the cart after the customer adds their third item:
317
+
318
+ ```javascript
319
+ let addCount = 0;
320
+
321
+ window.addEventListener('lce:actions.cart_item_added', () => {
322
+ addCount++;
323
+ if (addCount === 3) {
324
+ window.elements.actions.cart.openCart();
325
+ }
326
+ });
327
+ ```
328
+
329
+ ### Bundle Sales
330
+
331
+ Add complementary products with one click:
332
+
333
+ ```javascript
334
+ document.querySelector('#buy-bundle').addEventListener('click', async () => {
335
+ await window.elements.actions.cart.addProduct([
336
+ { identifier: 'MAIN_PRODUCT', fulfillmentType: 'shipping', quantity: 1 },
337
+ { identifier: 'ACCESSORY_1', fulfillmentType: 'shipping', quantity: 1 },
338
+ { identifier: 'ACCESSORY_2', fulfillmentType: 'shipping', quantity: 1 }
339
+ ], true);
340
+ });
341
+ ```
342
+
343
+ ### Custom Cart Button with Count
344
+
345
+ ```javascript
346
+ const cartBtn = document.querySelector('#my-cart-button');
347
+ const countBadge = document.querySelector('#cart-count');
348
+
349
+ // Update count when cart changes
350
+ window.addEventListener('lce:actions.cart_updated', (event) => {
351
+ const { current } = event.detail.data;
352
+ countBadge.textContent = current.itemCount || '';
353
+ countBadge.hidden = current.itemCount === 0;
354
+ });
355
+
356
+ // Toggle cart on click
357
+ cartBtn.addEventListener('click', () => {
358
+ window.elements.actions.cart.toggleCart();
359
+ });
360
+ ```
361
+
362
+ ---
363
+
364
+ ## Customization
365
+
366
+ Style the cart through theme configuration.
367
+
368
+ ```javascript
369
+ const client = await Elements('YOUR_API_KEY', {
370
+ customTheme: {
371
+ cart: {
372
+ // Cart-specific theme options
373
+ }
374
+ }
375
+ });
376
+ ```
377
+
378
+ See [theming.md](./theming.md) for available cart theme options.
379
+
380
+ ---
381
+
382
+ ## See Also
383
+
384
+ - [Checkout](./checkout.md) - Complete purchase flow
385
+ - [Actions Reference](./actions.md) - All available actions
386
+ - [Events Reference](./events.md) - All available events
387
+ - [Theming Guide](./theming.md) - Customization options