ui.shipaid.com 0.3.22 → 0.3.24

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/README.md CHANGED
@@ -1,200 +1,200 @@
1
- # ShipAid Shopify Widget
2
-
3
- This is the repository for the ShipAid Shopify (and possibly others in future) widget. It uses the [Lit](https://lit.dev/) web components library, so it is recommended to become familiar with it before contributing to this repository.
4
-
5
- Using Lit provides a framework that allows us to build a reactive UI, using JSX-like syntax - no need to use JQuery etc. And it can easily be installed in a page by using the custom web component name:
6
- ```html
7
- <shipaid-widget>Fallback Content</shipaid-widget>
8
- ```
9
-
10
- ## Overview
11
-
12
- This widget provides an interface where a user can choose to add or remove ShipAid protection - this is actually a product in their store that can be added to cart. When the component is loaded, we immediately run a request to fetch the ShipAid protection product details from our API, as well as the customers current cart from the [Shopify AJAX API](https://shopify.dev/api/ajax/reference/cart).
13
- Once we have this data, we can check whether the customer currently has the ShipAid product in their cart, and show either the add or remove product buttons based on this.
14
-
15
- We also emit various [custom events](https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events#adding_custom_data_%E2%80%93_customevent) when we add or remove the ShipAid product from the cart, so other developers can listen to these events, and update AJAX carts.
16
-
17
- ### Installation
18
-
19
- Add the script tag to the theme - if the theme has an ajax cart, you'll likely want to add this to the `theme.liquid` file, otherwise if the store has only a cart page (`/cart`), then you can add it to just that page, to save it being unecessarily loaded when it isn't needed.
20
- As we don't want to affect a stores speed at all, you should add it to the bottom of the page just before the ending body tag (`</body>`), rather than inside the `<head>` block.
21
-
22
- ```html
23
- <!-- ShipAid Widget -->
24
- <script src="https://unpkg.com/ui.shipaid.com/dist/widget.es.js" type="module"></script>
25
- ```
26
- Then add the widget element where needed:
27
-
28
- ```html
29
- <shipaid-widget></shipaid-widget>
30
-
31
- <!-- Disable polling example -->
32
- <shipaid-widget disablePolling></shipaid-widget>
33
-
34
- <!-- With customised text -->
35
- <shipaid-widget>
36
- <p slot="loading">Loading ShipAid Protection</p>
37
- </shipaid-widget>
38
- ```
39
-
40
- #### Test Mode
41
-
42
- Sometimes, a store won't have activated their subscription before you install the widget - in this case, the widget does not display (you will notice a message in the console reflecting this). So to force the widget to show while you are installing and testing it, you can add this query param to the page URL: `shipaid-test`.
43
- For example: `https://some-store.myshopify.com/cart?shipaid-text`
44
-
45
- ### Slots
46
-
47
- Slots, with the syntax `slot="<slot name>"`, can be used to customise the widgets content - for example, a merchant may want to add a custom subtitle, which can be done like so:
48
- ```html
49
- <shipaid-widget>
50
- <p slot="subtitle">Shipping protection is required to be able to return or refunds items.</p>
51
- </shipaid-widget>
52
- ```
53
-
54
- The default content will be replaced by any slot content. You can also add inline styles to the slots, if you need to change the font size/weight for example - but color changes should be handled by CSS variables:
55
- ```html
56
- <span slot="title" style="font-weight: 500;">Package Protection</span>
57
- ```
58
-
59
- | Name | Description |
60
- |--------|-------------|
61
- | `loading` | Replaces the text that is shown whilst the widget is loading - I.e. fetching content from the Shopify or ShipAid API's. |
62
- | `title` | Replaces the default title. |
63
- | `subtitle` | Replaces the default subtitle. |
64
-
65
- ### Props
66
-
67
- This is a list of props that can be used to configure the widget:
68
-
69
- | Prop | Description | Value/Type |
70
- |--------|-------------|---------|
71
- | `disablePolling` | Sets whether the cart should disable polling (enabled by default) - should be disabled if integrating manually with an ajax cart. | `boolean` |
72
- | `pollingInterval` | If polling is enabled, it sets the interval (in ms) between API updates. | `number` in milliseconds |
73
- | `disableRefresh ` | Sets whether the store cart should be updated when the protection item is added/removed. Add if you want to initially hide the protection product from the cart, even if it has just been added. | `boolean` |
74
- | `customerId ` | Passes the information of the customer to the widget. Add if merchant wants to disable auto opt-in for some customers based on the customer tags. | `boolean` |
75
- | `lang ` | Sets the widget language (see the translations section below). This value can be any supported [ISO 639-1 code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). | `string` defaults to `en` |
76
- | `refreshCart` | Refresh the page if shipaid product quantity is greater than one to sync product qty at checkout | `boolean` |
77
- | `persistPopup` | Use local storage to show popup so that popup don't re-render on script refresh | `boolean` |
78
-
79
- ### Events
80
-
81
- This is a list of the events emitted by the widget. You can listen to these events like so:
82
- ```js
83
- document.addEventListener('shipaid-protection-status', ({ detail }) => {
84
- console.log(detail.cart) // ShopifyCart
85
- })
86
- ```
87
-
88
- | Event | Description | Payload |
89
- |-------|-------------|---------|
90
- | `shipaid-loaded` | Dispatched when the widget has finished fetching data from the API, and has successfully rendered. | Contains the data from the ShipAid API: [`ShipAidStore`](/types/shipaid.ts) |
91
- | `shipaid-protection-status` | Dispatched when a user either adds or removes the protection product from their cart. | `{ protection: boolean, cart: ShopifyCart, lineItem: ShopifyCartItem }` |
92
-
93
- ### Methods
94
-
95
- This is a list of public methods available on the widget HTMLElement that can be used to change protection settings. These can be used by querying the element:
96
-
97
- ```js
98
- const shipAidEl = document.querySelector('shipaid-widget')
99
- if (shipAidEl) await shipAidEl.updateCart()
100
- ```
101
-
102
- | Method | Description | Payload |
103
- |--------|-------------|---------|
104
- | `hasProtection` | Returns a boolean which indicates whether the protection item is currently in the cart. | |
105
- | `updateCart` | Updates the internal cart, and triggers any protection product updates - use this method with with ajax carts, to update protection state without needing to reload the page. | Optional - the cart object from the ajax API. |
106
- | `addProtection` | Adds the relevant protection item to cart. | |
107
- | `removeProtection` | Removes the protection item from the cart. | |
108
-
109
- ### Styles
110
-
111
- If you need to change any of the widget colors to suit a specific theme, there are various CSS variables you can use to do so. These should be added to the style attribute on the widget element:
112
-
113
- ```html
114
- <shipaid-widget
115
- style="
116
- --shipaid-text: #fff;
117
- --shipaid-prompt-actions-price-color: #fff;
118
- "
119
- >
120
- </shipaid-widget>
121
- ```
122
-
123
- | Variable | Description | Default |
124
- |----------|-------------|---------|
125
- | `--shipaid-text` | Changes the global text color. | `#000000` |
126
- | `--shipaid-text-muted` | Changes the global muted text color. | `#aaaaaa` |
127
- | `--shipaid-prompt-margin` | Changes the margins of the main container element. | `2rem 0px 4rem auto` |
128
- | `--shipaid-prompt-width` | Changes the width of the main container element. | `400px` |
129
- | `--shipaid-prompt-width-mobile` | Changes the mobile width of the main container element. | `100%` |
130
- | `--shipaid-prompt-actions-price-color` | Changes the color of the price element. | `var(--shipaid-text-muted)` |
131
- | `--shipaid-prompt-actions-button-color` | Changes the color of the add/remove button element. | `var(--shipaid-primary)` |
132
- | `--shipaid-prompt-badge-background-color` | Changes the background color of the learn more button element. | `var(--shipaid-light-grey)` |
133
- | `--shipaid-prompt-badge-text-color` | Changes the color of the learn more button element. | `var(--shipaid-text)` |
134
- | `--shipaid-logo-height` | Changes the height of ShipAid logo. | `var(--shipaid-logo-height, 35px)` |
135
- | `--shipaid-logo-max-height` | Changes the max height of ShipAid logo. | `var(--shipaid-logo-max-height, 35px)` |
136
- | `--shipaid-logo-width` | Changes the width of ShipAid logo. | `var(--shipaid-logo-width, auto)` |
137
- | `--shipaid-logo-max-width` | Changes the max width of ShipAid logo. | `var(--shipaid-logo-max-width, 50px)` |
138
- | `--shipaid-prompt-footer-button-size` | Changes the height of info button in footer. | `var(--shipaid-prompt-footer-button-size, 10px)` |
139
- | `--shipaid-prompt-badge-border-radius` | Changes border radius of footer | `var(--shipaid-prompt-badge-border-radius, 30px)` |
140
- | `--shipaid-footer-badge-logo-height` | Changes the height of logo in footer. | `var(--shipaid-footer-badge-logo-height, 9px)` |
141
- | `--shipaid-prompt-footer-location` | Changes the position of footer badge | `var(--shipaid-prompt-footer-location, flex-start)` |
142
- | `--shipaid-prompt-product-actions-content` | Changes the spaces between price and add/remove button | `var(--shipaid-prompt-product-actions-content, space-between)` |
143
- | `--shipaid-prompt-footer-topMargin` | Changes margin between header and badge footer | `var(--shipaid-prompt-footer-topMargin, 0px)` |
144
- | `-shipaid-prompt-footer-display` | Changes the display option for footer | `var(-shipaid-prompt-footer-display, flex)` |
145
-
146
- Other variables can be found [here](/widget/src/assets/styles.ts) (`/widget/src/assets/styles.ts`).
147
-
148
- ### Translations
149
-
150
- This widget also supports multiple languages, using the `lit-translate` plugin. The widget language can be configured using an attribute and would usally be static, but supports reactively swapping the language if a theme needs - i.e. if a user switches the language using a select option in the theme, then the widget language could be updated at the same time by selecting the widget element, and setting the `lang` attribute.
151
-
152
- To create lang files, you should copy the default `/lang/en.json` file, and rename it to an [ISO 639-1 code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) - for example `cp /src/lang/en.json /src/lang/fs.json`. You can then go through each key, and translate the existing text to the relevant language. This should be all you need to do - the widget automatically (and lazily, to save bundle size) imports the relevant lang file when it is specified. It will also fallback to the default `en.json` lang file if a certain language doesn't exist yet.
153
-
154
-
155
- ## Contributing
156
-
157
- ### Requirements
158
-
159
- - Node v16+
160
- - PNPM
161
- - Development Shopify store (with the ShipAid app installed)
162
- - ShipAid API development/staging instance
163
-
164
- ### Development
165
-
166
- You will need to make sure your development store has the ShipAid app installed, so the store and its protection product is added to the DB. You will also need to ensure the Shopify app you are testing this with has an app proxy added, and pointed towards an API instance.
167
-
168
- ```sh
169
- pnpm install
170
- pnpm run develop
171
- ```
172
-
173
- Once the project is running, add the below to your development Shopify store `theme.liquid`:
174
-
175
- ```html
176
- <!-- Dev -->
177
- <script src="http://localhost:3000/src/shipaid-widget.ts"type="module" ></script>
178
-
179
- <!-- Prod -->
180
- <!-- ShipAid Widget -->
181
- <script src="https://unpkg.com/ui.shipaid.com/dist/widget.es.js" type="module"></script>
182
- ```
183
- And add the widget element where needed:
184
-
185
- ```html
186
- <shipaid-widget>
187
- <p>Loading ShipAid Protection</p>
188
- </shipaid-widget>
189
- ```
190
-
191
- ### Build
192
-
193
- Once the project has been built, you can publish the project to NPM, and users can add the script to their store using a package CDN (I.e. [Unpkg](https://unpkg.com/)).
194
-
195
- ```sh
196
- pnpm install
197
- pnpm run lint
198
- pnpm run build
199
- pnpm publish
200
- ```
1
+ # ShipAid Shopify Widget
2
+
3
+ This is the repository for the ShipAid Shopify (and possibly others in future) widget. It uses the [Lit](https://lit.dev/) web components library, so it is recommended to become familiar with it before contributing to this repository.
4
+
5
+ Using Lit provides a framework that allows us to build a reactive UI, using JSX-like syntax - no need to use JQuery etc. And it can easily be installed in a page by using the custom web component name:
6
+ ```html
7
+ <shipaid-widget>Fallback Content</shipaid-widget>
8
+ ```
9
+
10
+ ## Overview
11
+
12
+ This widget provides an interface where a user can choose to add or remove ShipAid protection - this is actually a product in their store that can be added to cart. When the component is loaded, we immediately run a request to fetch the ShipAid protection product details from our API, as well as the customers current cart from the [Shopify AJAX API](https://shopify.dev/api/ajax/reference/cart).
13
+ Once we have this data, we can check whether the customer currently has the ShipAid product in their cart, and show either the add or remove product buttons based on this.
14
+
15
+ We also emit various [custom events](https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events#adding_custom_data_%E2%80%93_customevent) when we add or remove the ShipAid product from the cart, so other developers can listen to these events, and update AJAX carts.
16
+
17
+ ### Installation
18
+
19
+ Add the script tag to the theme - if the theme has an ajax cart, you'll likely want to add this to the `theme.liquid` file, otherwise if the store has only a cart page (`/cart`), then you can add it to just that page, to save it being unecessarily loaded when it isn't needed.
20
+ As we don't want to affect a stores speed at all, you should add it to the bottom of the page just before the ending body tag (`</body>`), rather than inside the `<head>` block.
21
+
22
+ ```html
23
+ <!-- ShipAid Widget -->
24
+ <script src="https://unpkg.com/ui.shipaid.com/dist/widget.es.js" type="module"></script>
25
+ ```
26
+ Then add the widget element where needed:
27
+
28
+ ```html
29
+ <shipaid-widget></shipaid-widget>
30
+
31
+ <!-- Disable polling example -->
32
+ <shipaid-widget disablePolling></shipaid-widget>
33
+
34
+ <!-- With customised text -->
35
+ <shipaid-widget>
36
+ <p slot="loading">Loading ShipAid Protection</p>
37
+ </shipaid-widget>
38
+ ```
39
+
40
+ #### Test Mode
41
+
42
+ Sometimes, a store won't have activated their subscription before you install the widget - in this case, the widget does not display (you will notice a message in the console reflecting this). So to force the widget to show while you are installing and testing it, you can add this query param to the page URL: `shipaid-test`.
43
+ For example: `https://some-store.myshopify.com/cart?shipaid-text`
44
+
45
+ ### Slots
46
+
47
+ Slots, with the syntax `slot="<slot name>"`, can be used to customise the widgets content - for example, a merchant may want to add a custom subtitle, which can be done like so:
48
+ ```html
49
+ <shipaid-widget>
50
+ <p slot="subtitle">Shipping protection is required to be able to return or refunds items.</p>
51
+ </shipaid-widget>
52
+ ```
53
+
54
+ The default content will be replaced by any slot content. You can also add inline styles to the slots, if you need to change the font size/weight for example - but color changes should be handled by CSS variables:
55
+ ```html
56
+ <span slot="title" style="font-weight: 500;">Package Protection</span>
57
+ ```
58
+
59
+ | Name | Description |
60
+ |--------|-------------|
61
+ | `loading` | Replaces the text that is shown whilst the widget is loading - I.e. fetching content from the Shopify or ShipAid API's. |
62
+ | `title` | Replaces the default title. |
63
+ | `subtitle` | Replaces the default subtitle. |
64
+
65
+ ### Props
66
+
67
+ This is a list of props that can be used to configure the widget:
68
+
69
+ | Prop | Description | Value/Type |
70
+ |--------|-------------|---------|
71
+ | `disablePolling` | Sets whether the cart should disable polling (enabled by default) - should be disabled if integrating manually with an ajax cart. | `boolean` |
72
+ | `pollingInterval` | If polling is enabled, it sets the interval (in ms) between API updates. | `number` in milliseconds |
73
+ | `disableRefresh ` | Sets whether the store cart should be updated when the protection item is added/removed. Add if you want to initially hide the protection product from the cart, even if it has just been added. | `boolean` |
74
+ | `customerId ` | Passes the information of the customer to the widget. Add if merchant wants to disable auto opt-in for some customers based on the customer tags. | `boolean` |
75
+ | `lang ` | Sets the widget language (see the translations section below). This value can be any supported [ISO 639-1 code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). | `string` defaults to `en` |
76
+ | `refreshCart` | Refresh the page if shipaid product quantity is greater than one to sync product qty at checkout | `boolean` |
77
+ | `persistPopup` | Use local storage to show popup so that popup don't re-render on script refresh | `boolean` |
78
+
79
+ ### Events
80
+
81
+ This is a list of the events emitted by the widget. You can listen to these events like so:
82
+ ```js
83
+ document.addEventListener('shipaid-protection-status', ({ detail }) => {
84
+ console.log(detail.cart) // ShopifyCart
85
+ })
86
+ ```
87
+
88
+ | Event | Description | Payload |
89
+ |-------|-------------|---------|
90
+ | `shipaid-loaded` | Dispatched when the widget has finished fetching data from the API, and has successfully rendered. | Contains the data from the ShipAid API: [`ShipAidStore`](/types/shipaid.ts) |
91
+ | `shipaid-protection-status` | Dispatched when a user either adds or removes the protection product from their cart. | `{ protection: boolean, cart: ShopifyCart, lineItem: ShopifyCartItem }` |
92
+
93
+ ### Methods
94
+
95
+ This is a list of public methods available on the widget HTMLElement that can be used to change protection settings. These can be used by querying the element:
96
+
97
+ ```js
98
+ const shipAidEl = document.querySelector('shipaid-widget')
99
+ if (shipAidEl) await shipAidEl.updateCart()
100
+ ```
101
+
102
+ | Method | Description | Payload |
103
+ |--------|-------------|---------|
104
+ | `hasProtection` | Returns a boolean which indicates whether the protection item is currently in the cart. | |
105
+ | `updateCart` | Updates the internal cart, and triggers any protection product updates - use this method with with ajax carts, to update protection state without needing to reload the page. | Optional - the cart object from the ajax API. |
106
+ | `addProtection` | Adds the relevant protection item to cart. | |
107
+ | `removeProtection` | Removes the protection item from the cart. | |
108
+
109
+ ### Styles
110
+
111
+ If you need to change any of the widget colors to suit a specific theme, there are various CSS variables you can use to do so. These should be added to the style attribute on the widget element:
112
+
113
+ ```html
114
+ <shipaid-widget
115
+ style="
116
+ --shipaid-text: #fff;
117
+ --shipaid-prompt-actions-price-color: #fff;
118
+ "
119
+ >
120
+ </shipaid-widget>
121
+ ```
122
+
123
+ | Variable | Description | Default |
124
+ |----------|-------------|---------|
125
+ | `--shipaid-text` | Changes the global text color. | `#000000` |
126
+ | `--shipaid-text-muted` | Changes the global muted text color. | `#aaaaaa` |
127
+ | `--shipaid-prompt-margin` | Changes the margins of the main container element. | `2rem 0px 4rem auto` |
128
+ | `--shipaid-prompt-width` | Changes the width of the main container element. | `400px` |
129
+ | `--shipaid-prompt-width-mobile` | Changes the mobile width of the main container element. | `100%` |
130
+ | `--shipaid-prompt-actions-price-color` | Changes the color of the price element. | `var(--shipaid-text-muted)` |
131
+ | `--shipaid-prompt-actions-button-color` | Changes the color of the add/remove button element. | `var(--shipaid-primary)` |
132
+ | `--shipaid-prompt-badge-background-color` | Changes the background color of the learn more button element. | `var(--shipaid-light-grey)` |
133
+ | `--shipaid-prompt-badge-text-color` | Changes the color of the learn more button element. | `var(--shipaid-text)` |
134
+ | `--shipaid-logo-height` | Changes the height of ShipAid logo. | `var(--shipaid-logo-height, 35px)` |
135
+ | `--shipaid-logo-max-height` | Changes the max height of ShipAid logo. | `var(--shipaid-logo-max-height, 35px)` |
136
+ | `--shipaid-logo-width` | Changes the width of ShipAid logo. | `var(--shipaid-logo-width, auto)` |
137
+ | `--shipaid-logo-max-width` | Changes the max width of ShipAid logo. | `var(--shipaid-logo-max-width, 50px)` |
138
+ | `--shipaid-prompt-footer-button-size` | Changes the height of info button in footer. | `var(--shipaid-prompt-footer-button-size, 10px)` |
139
+ | `--shipaid-prompt-badge-border-radius` | Changes border radius of footer | `var(--shipaid-prompt-badge-border-radius, 30px)` |
140
+ | `--shipaid-footer-badge-logo-height` | Changes the height of logo in footer. | `var(--shipaid-footer-badge-logo-height, 9px)` |
141
+ | `--shipaid-prompt-footer-location` | Changes the position of footer badge | `var(--shipaid-prompt-footer-location, flex-start)` |
142
+ | `--shipaid-prompt-product-actions-content` | Changes the spaces between price and add/remove button | `var(--shipaid-prompt-product-actions-content, space-between)` |
143
+ | `--shipaid-prompt-footer-topMargin` | Changes margin between header and badge footer | `var(--shipaid-prompt-footer-topMargin, 0px)` |
144
+ | `-shipaid-prompt-footer-display` | Changes the display option for footer | `var(-shipaid-prompt-footer-display, flex)` |
145
+
146
+ Other variables can be found [here](/widget/src/assets/styles.ts) (`/widget/src/assets/styles.ts`).
147
+
148
+ ### Translations
149
+
150
+ This widget also supports multiple languages, using the `lit-translate` plugin. The widget language can be configured using an attribute and would usally be static, but supports reactively swapping the language if a theme needs - i.e. if a user switches the language using a select option in the theme, then the widget language could be updated at the same time by selecting the widget element, and setting the `lang` attribute.
151
+
152
+ To create lang files, you should copy the default `/lang/en.json` file, and rename it to an [ISO 639-1 code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) - for example `cp /src/lang/en.json /src/lang/fs.json`. You can then go through each key, and translate the existing text to the relevant language. This should be all you need to do - the widget automatically (and lazily, to save bundle size) imports the relevant lang file when it is specified. It will also fallback to the default `en.json` lang file if a certain language doesn't exist yet.
153
+
154
+
155
+ ## Contributing
156
+
157
+ ### Requirements
158
+
159
+ - Node v16+
160
+ - PNPM
161
+ - Development Shopify store (with the ShipAid app installed)
162
+ - ShipAid API development/staging instance
163
+
164
+ ### Development
165
+
166
+ You will need to make sure your development store has the ShipAid app installed, so the store and its protection product is added to the DB. You will also need to ensure the Shopify app you are testing this with has an app proxy added, and pointed towards an API instance.
167
+
168
+ ```sh
169
+ pnpm install
170
+ pnpm run develop
171
+ ```
172
+
173
+ Once the project is running, add the below to your development Shopify store `theme.liquid`:
174
+
175
+ ```html
176
+ <!-- Dev -->
177
+ <script src="http://localhost:3000/src/shipaid-widget.ts"type="module" ></script>
178
+
179
+ <!-- Prod -->
180
+ <!-- ShipAid Widget -->
181
+ <script src="https://unpkg.com/ui.shipaid.com/dist/widget.es.js" type="module"></script>
182
+ ```
183
+ And add the widget element where needed:
184
+
185
+ ```html
186
+ <shipaid-widget>
187
+ <p>Loading ShipAid Protection</p>
188
+ </shipaid-widget>
189
+ ```
190
+
191
+ ### Build
192
+
193
+ Once the project has been built, you can publish the project to NPM, and users can add the script to their store using a package CDN (I.e. [Unpkg](https://unpkg.com/)).
194
+
195
+ ```sh
196
+ pnpm install
197
+ pnpm run lint
198
+ pnpm run build
199
+ pnpm publish
200
+ ```
package/dist/widget.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  function calculateProtectionTotal(store, protectionProduct, cart) {
2
- var _a, _b, _c;
2
+ var _a, _b;
3
3
  if (!store)
4
4
  throw new Error("Missing store settings.");
5
5
  if (!protectionProduct)
@@ -10,23 +10,34 @@ function calculateProtectionTotal(store, protectionProduct, cart) {
10
10
  if (!settings) {
11
11
  throw new Error("Tried to find protection variant, but protection settings for this store are missing.");
12
12
  }
13
- const excludedProductSkus = Array.isArray(store == null ? void 0 : store.excludedProductSkus) ? store == null ? void 0 : store.excludedProductSkus.map((sku) => sku.trim()) : [];
14
- const itemTotal = ((_a = cart.items) == null ? void 0 : _a.reduce((total, item) => {
15
- if (!item.sku)
16
- return total;
17
- const itemIsExcluded = excludedProductSkus.some((sku) => sku === item.sku.trim());
18
- return itemIsExcluded ? total - item.final_line_price : total;
19
- }, cart.total_price)) ?? cart.total_price;
20
- const protectionVariantsInCart = ((_b = cart.items) == null ? void 0 : _b.filter((item) => {
13
+ const excludedProductSkus = Array.isArray(store == null ? void 0 : store.excludedProductSkus) ? store.excludedProductSkus.map((sku) => sku.trim()) : [];
14
+ const excludedProductIds = Array.isArray(store == null ? void 0 : store.excludedProductsVariantsId) ? store.excludedProductsVariantsId.map((id) => {
15
+ var _a2;
16
+ return parseInt(((_a2 = id.match(/\d+/)) == null ? void 0 : _a2[0]) || "", 10);
17
+ }) : [];
18
+ const isItemExcluded = (item) => {
19
+ if (item.sku && excludedProductSkus.includes(item.sku.trim())) {
20
+ return true;
21
+ } else if (item.variant_id && excludedProductIds.includes(item.variant_id)) {
22
+ return true;
23
+ }
24
+ return false;
25
+ };
26
+ const itemTotal = (cart.items || []).reduce((total, item) => {
27
+ return isItemExcluded(item) ? total - item.final_line_price : total;
28
+ }, cart.total_price || 0);
29
+ const protectionVariantsInCart = ((_a = cart.items) == null ? void 0 : _a.filter((item) => {
21
30
  var _a2;
22
31
  return (_a2 = protectionProduct == null ? void 0 : protectionProduct.variants) == null ? void 0 : _a2.some((variant) => variant.id === item.variant_id);
23
32
  })) ?? [];
24
33
  const protectionVariantsInCartTotal = protectionVariantsInCart.reduce((total, item) => total + item.final_line_price, 0);
25
34
  const cartTotal = itemTotal - protectionVariantsInCartTotal;
35
+ if (cartTotal === 0)
36
+ return cartTotal;
26
37
  if (settings.protectionType === "FIXED") {
27
38
  if (typeof settings.defaultFee !== "number")
28
39
  throw new Error("Missing default fee amount.");
29
- if (!((_c = settings.rules) == null ? void 0 : _c.length))
40
+ if (!((_b = settings.rules) == null ? void 0 : _b.length))
30
41
  return settings.defaultFee;
31
42
  const formattedCartTotal = cartTotal / 100;
32
43
  const sortedRules = settings.rules.sort((a2, b) => {
@@ -1854,7 +1865,7 @@ let ShipAidWidget = class extends s$1 {
1854
1865
  this._apiEndpoint = "/apps/shipaid";
1855
1866
  this._storeDomain = ((_a = window.Shopify) == null ? void 0 : _a.shop) ?? ((_c = (_b = window.Shopify) == null ? void 0 : _b.Checkout) == null ? void 0 : _c.apiHost);
1856
1867
  this._hasFinishedSetup = false;
1857
- this._shouldShowWidget = true;
1868
+ this._shouldShowWidget = false;
1858
1869
  this._hasProtectionInCart = false;
1859
1870
  this.hasLoadedStrings = false;
1860
1871
  this.intervalId = 0;
@@ -2070,10 +2081,9 @@ let ShipAidWidget = class extends s$1 {
2070
2081
  } catch (err) {
2071
2082
  const error = err;
2072
2083
  logger.error(error.message);
2073
- this._setState(
2074
- "error",
2075
- "Failed to add protection to cart - please try again, or contact us for help."
2076
- );
2084
+ } finally {
2085
+ this._cart = await this._fetchCart();
2086
+ this._setState("success");
2077
2087
  }
2078
2088
  }
2079
2089
  /** Remove ShipAid shipping protection. */
@@ -2099,10 +2109,9 @@ let ShipAidWidget = class extends s$1 {
2099
2109
  } catch (err) {
2100
2110
  const error = err;
2101
2111
  logger.error(error.message);
2102
- this._setState(
2103
- "error",
2104
- "Failed to add protection to cart - please try again, or contact us for help."
2105
- );
2112
+ } finally {
2113
+ this._cart = await this._fetchCart();
2114
+ this._setState("success");
2106
2115
  }
2107
2116
  }
2108
2117
  /** Try adding ShipAid shipping protection during polling if applicable */
@@ -2224,7 +2233,7 @@ let ShipAidWidget = class extends s$1 {
2224
2233
  !this.disableActions,
2225
2234
  () => x`
2226
2235
  <label class="shipaid-toggle">
2227
- ${this._hasProtectionInCart ? x`<input type="checkbox" checked @click=${this._updateProtection} ?disabled=${this._state.loading}>` : x`<input type="checkbox" @click=${this._updateProtection} ?disabled=${this._state.loading}>`}
2236
+ ${this._hasProtectionInCart || !this._hasFinishedSetup ? x`<input type="checkbox" checked @click=${this._updateProtection} ?disabled=${this._state.loading}>` : x`<input type="checkbox" @click=${this._updateProtection} ?disabled=${this._state.loading}>`}
2228
2237
  <span class="shipaid-slider"></span>
2229
2238
  </label>
2230
2239
  `
@@ -2316,7 +2325,6 @@ let ShipAidWidget = class extends s$1 {
2316
2325
  return;
2317
2326
  }
2318
2327
  this._hasFinishedSetup = true;
2319
- this._shouldShowWidget = true;
2320
2328
  this._dispatchEvent(Events.LOADED, this._store);
2321
2329
  setTimeout(async () => {
2322
2330
  var _a2, _b2, _c2, _d2;
@@ -2370,7 +2378,7 @@ let ShipAidWidget = class extends s$1 {
2370
2378
  useEffect(
2371
2379
  this,
2372
2380
  async () => {
2373
- var _a, _b, _c;
2381
+ var _a, _b, _c, _d;
2374
2382
  this._cartLastUpdated = /* @__PURE__ */ new Date();
2375
2383
  if (!((_a = this._cart) == null ? void 0 : _a.items))
2376
2384
  return;
@@ -2382,7 +2390,10 @@ let ShipAidWidget = class extends s$1 {
2382
2390
  });
2383
2391
  const protectionCartItem = (_c = this._cart) == null ? void 0 : _c.items[protectionCartItemIndex];
2384
2392
  this._hasProtectionInCart = !!protectionCartItem;
2385
- if (this._cart.item_count === 1 && !!protectionCartItem) {
2393
+ if (!this._store)
2394
+ return;
2395
+ const protectionFee = await this.calculateProtectionTotal(this._cart);
2396
+ if (this._cart.item_count > 0 && !!protectionCartItem && (this._cart.total_price === protectionCartItem.final_line_price || !protectionFee)) {
2386
2397
  const removePayload = {
2387
2398
  id: protectionCartItem.key,
2388
2399
  quantity: 0
@@ -2394,16 +2405,25 @@ let ShipAidWidget = class extends s$1 {
2394
2405
  sessionStorage.removeItem(LOCAL_STORAGE_KEY);
2395
2406
  return await this._handleRefresh(cart2);
2396
2407
  }
2397
- if (!this._store)
2398
- return;
2399
- const protectionFee = await this.calculateProtectionTotal(this._cart);
2400
2408
  const protectionVariant = this._findProtectionVariant(protectionFee);
2401
- this._protectionVariant = protectionVariant;
2409
+ if (!protectionFee) {
2410
+ this._protectionVariant = {
2411
+ id: 0,
2412
+ price: "0"
2413
+ };
2414
+ } else {
2415
+ this._protectionVariant = protectionVariant;
2416
+ this._shouldShowWidget = true;
2417
+ }
2402
2418
  if (!(protectionVariant == null ? void 0 : protectionVariant.id)) {
2403
2419
  this._shouldShowWidget = false;
2404
2420
  logger.error("No matching protection variant found.");
2405
2421
  return;
2406
2422
  }
2423
+ if (!((_d = this._protectionVariant) == null ? void 0 : _d.id)) {
2424
+ this._shouldShowWidget = false;
2425
+ return;
2426
+ }
2407
2427
  if (!protectionCartItem)
2408
2428
  return;
2409
2429
  if (protectionVariant.id === protectionCartItem.variant_id) {