@wix/headless-stores 0.0.10 → 0.0.12
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/cjs/dist/enums/index.d.ts +2 -0
- package/cjs/dist/enums/index.js +18 -0
- package/cjs/dist/enums/social-platform-enums.d.ts +25 -0
- package/cjs/dist/enums/social-platform-enums.js +30 -0
- package/cjs/dist/enums/sort-enums.d.ts +17 -0
- package/cjs/dist/enums/sort-enums.js +21 -0
- package/cjs/dist/react/BuyNow.d.ts +2 -4
- package/cjs/dist/react/Category.d.ts +9 -11
- package/cjs/dist/react/Category.js +8 -23
- package/cjs/dist/react/Collection.d.ts +13 -3
- package/cjs/dist/react/Collection.js +30 -19
- package/cjs/dist/react/FilteredCollection.d.ts +46 -15
- package/cjs/dist/react/FilteredCollection.js +45 -12
- package/cjs/dist/react/PayNow.d.ts +2 -4
- package/cjs/dist/react/Product.d.ts +8 -35
- package/cjs/dist/react/Product.js +10 -31
- package/cjs/dist/react/ProductActions.d.ts +42 -0
- package/cjs/dist/react/ProductActions.js +83 -0
- package/cjs/dist/react/ProductModifiers.d.ts +16 -8
- package/cjs/dist/react/ProductModifiers.js +19 -10
- package/cjs/dist/react/ProductVariantSelector.d.ts +52 -68
- package/cjs/dist/react/ProductVariantSelector.js +58 -86
- package/cjs/dist/react/RelatedProducts.d.ts +8 -4
- package/cjs/dist/react/RelatedProducts.js +12 -7
- package/cjs/dist/react/SelectedVariant.d.ts +66 -0
- package/cjs/dist/react/SelectedVariant.js +52 -0
- package/cjs/dist/react/SocialSharing.d.ts +24 -30
- package/cjs/dist/react/SocialSharing.js +8 -2
- package/cjs/dist/react/Sort.d.ts +12 -15
- package/cjs/dist/react/Sort.js +11 -34
- package/cjs/dist/react/index.d.ts +11 -10
- package/cjs/dist/react/index.js +3 -2
- package/cjs/dist/services/buy-now-service.js +2 -2
- package/cjs/dist/services/catalog-options-service.d.ts +2 -2
- package/cjs/dist/services/catalog-options-service.js +39 -45
- package/cjs/dist/services/catalog-price-range-service.d.ts +2 -2
- package/cjs/dist/services/catalog-price-range-service.js +13 -11
- package/cjs/dist/services/category-service.d.ts +7 -6
- package/cjs/dist/services/category-service.js +15 -11
- package/cjs/dist/services/collection-service.d.ts +8 -8
- package/cjs/dist/services/collection-service.js +182 -67
- package/cjs/dist/services/filter-service.d.ts +1 -1
- package/cjs/dist/services/filter-service.js +15 -23
- package/cjs/dist/services/product-media-gallery-service.d.ts +3 -3
- package/cjs/dist/services/product-modifiers-service.d.ts +6 -8
- package/cjs/dist/services/product-modifiers-service.js +14 -7
- package/cjs/dist/services/product-service.d.ts +8 -7
- package/cjs/dist/services/product-service.js +36 -19
- package/cjs/dist/services/related-products-service.d.ts +4 -4
- package/cjs/dist/services/related-products-service.js +4 -4
- package/cjs/dist/services/selected-variant-service.d.ts +24 -16
- package/cjs/dist/services/selected-variant-service.js +271 -126
- package/cjs/dist/services/social-sharing-service.d.ts +2 -2
- package/cjs/dist/services/social-sharing-service.js +47 -63
- package/cjs/dist/services/sort-service.d.ts +3 -2
- package/cjs/dist/services/sort-service.js +8 -13
- package/dist/enums/index.d.ts +2 -0
- package/dist/enums/index.js +2 -0
- package/dist/enums/social-platform-enums.d.ts +25 -0
- package/dist/enums/social-platform-enums.js +27 -0
- package/dist/enums/sort-enums.d.ts +17 -0
- package/dist/enums/sort-enums.js +18 -0
- package/dist/react/BuyNow.d.ts +2 -4
- package/dist/react/Category.d.ts +9 -11
- package/dist/react/Category.js +10 -23
- package/dist/react/Collection.d.ts +13 -3
- package/dist/react/Collection.js +32 -21
- package/dist/react/FilteredCollection.d.ts +46 -15
- package/dist/react/FilteredCollection.js +49 -16
- package/dist/react/PayNow.d.ts +2 -4
- package/dist/react/Product.d.ts +8 -35
- package/dist/react/Product.js +11 -31
- package/dist/react/ProductActions.d.ts +42 -0
- package/dist/react/ProductActions.js +79 -0
- package/dist/react/ProductModifiers.d.ts +16 -8
- package/dist/react/ProductModifiers.js +22 -13
- package/dist/react/ProductVariantSelector.d.ts +52 -68
- package/dist/react/ProductVariantSelector.js +57 -84
- package/dist/react/RelatedProducts.d.ts +8 -4
- package/dist/react/RelatedProducts.js +15 -10
- package/dist/react/SelectedVariant.d.ts +66 -0
- package/dist/react/SelectedVariant.js +46 -0
- package/dist/react/SocialSharing.d.ts +24 -30
- package/dist/react/SocialSharing.js +11 -5
- package/dist/react/Sort.d.ts +12 -15
- package/dist/react/Sort.js +13 -34
- package/dist/react/index.d.ts +11 -10
- package/dist/react/index.js +11 -10
- package/dist/services/buy-now-service.js +2 -2
- package/dist/services/catalog-options-service.d.ts +2 -2
- package/dist/services/catalog-options-service.js +41 -47
- package/dist/services/catalog-price-range-service.d.ts +2 -2
- package/dist/services/catalog-price-range-service.js +15 -13
- package/dist/services/category-service.d.ts +7 -6
- package/dist/services/category-service.js +17 -13
- package/dist/services/collection-service.d.ts +8 -8
- package/dist/services/collection-service.js +188 -73
- package/dist/services/filter-service.d.ts +1 -1
- package/dist/services/filter-service.js +20 -28
- package/dist/services/product-media-gallery-service.d.ts +3 -3
- package/dist/services/product-modifiers-service.d.ts +6 -8
- package/dist/services/product-modifiers-service.js +16 -9
- package/dist/services/product-service.d.ts +8 -7
- package/dist/services/product-service.js +38 -21
- package/dist/services/related-products-service.d.ts +4 -4
- package/dist/services/related-products-service.js +6 -6
- package/dist/services/selected-variant-service.d.ts +24 -16
- package/dist/services/selected-variant-service.js +273 -127
- package/dist/services/social-sharing-service.d.ts +2 -2
- package/dist/services/social-sharing-service.js +49 -65
- package/dist/services/sort-service.d.ts +3 -2
- package/dist/services/sort-service.js +11 -16
- package/package.json +7 -1
- package/cjs/dist/react/ProductMediaGallery.d.ts +0 -128
- package/cjs/dist/react/ProductMediaGallery.js +0 -100
- package/dist/astro/BuyNowServiceContext.d.ts +0 -2
- package/dist/astro/BuyNowServiceContext.js +0 -6
- package/dist/astro/ManagerProviderContext.d.ts +0 -2
- package/dist/astro/ManagerProviderContext.js +0 -7
- package/dist/astro/withBuyButtonService.d.ts +0 -2
- package/dist/astro/withBuyButtonService.js +0 -16
- package/dist/react/CurrentCartServiceProvider.d.ts +0 -5
- package/dist/react/CurrentCartServiceProvider.js +0 -12
- package/dist/react/ProductMediaGallery.d.ts +0 -128
- package/dist/react/ProductMediaGallery.js +0 -92
- package/dist/react/VariantSelectorServiceProvider.d.ts +0 -7
- package/dist/react/VariantSelectorServiceProvider.js +0 -22
- package/dist/react/hookim/index.d.ts +0 -5
- package/dist/react/hookim/index.js +0 -22
- package/dist/services/CurrentCartService.d.ts +0 -18
- package/dist/services/CurrentCartService.js +0 -9
- package/dist/services/VariantSelectorServices.d.ts +0 -8
- package/dist/services/VariantSelectorServices.js +0 -20
|
@@ -1,15 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SelectedVariantService = exports.SelectedVariantServiceDefinition = void 0;
|
|
4
|
-
exports.loadSelectedVariantServiceConfig = loadSelectedVariantServiceConfig;
|
|
5
4
|
const services_definitions_1 = require("@wix/services-definitions");
|
|
6
5
|
const signals_1 = require("@wix/services-definitions/core-services/signals");
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const auto_sdk_stores_products_v_3_1 = require("@wix/auto_sdk_stores_products-v-3");
|
|
7
|
+
const auto_sdk_stores_inventory_items_v_3_1 = require("@wix/auto_sdk_stores_inventory-items-v-3");
|
|
8
|
+
// import { CurrentCartServiceDefinition } from '../../ecom/services/current-cart-service';
|
|
9
|
+
const product_service_1 = require("./product-service");
|
|
10
|
+
exports.SelectedVariantServiceDefinition = (0, services_definitions_1.defineService)('selectedVariant');
|
|
11
|
+
exports.SelectedVariantService = services_definitions_1.implementService.withConfig()(exports.SelectedVariantServiceDefinition, ({ getService }) => {
|
|
10
12
|
const signalsService = getService(signals_1.SignalsServiceDefinition);
|
|
11
13
|
// const cartService = getService(CurrentCartServiceDefinition);
|
|
12
|
-
const
|
|
14
|
+
const productService = getService(product_service_1.ProductServiceDefinition);
|
|
15
|
+
// const mediaService = getService(MediaGalleryServiceDefinition);
|
|
16
|
+
const selectedChoices = signalsService.signal({});
|
|
17
|
+
const preOrderMessage = signalsService.signal(null);
|
|
18
|
+
const initialProduct = productService.product.get();
|
|
19
|
+
signalsService.effect(() => {
|
|
20
|
+
const product = productService.product.get();
|
|
21
|
+
const selectedChoicesValue = selectedChoices.get() || {};
|
|
22
|
+
let mediaToDisplay = [];
|
|
23
|
+
const productItemsImages = product?.media?.itemsInfo?.items?.map(item => item).filter(Boolean) ??
|
|
24
|
+
[];
|
|
25
|
+
if (productItemsImages.length) {
|
|
26
|
+
mediaToDisplay = productItemsImages;
|
|
27
|
+
}
|
|
28
|
+
else if (product?.media?.main) {
|
|
29
|
+
mediaToDisplay = [product.media.main];
|
|
30
|
+
}
|
|
31
|
+
// Get images based on selected choices if available
|
|
32
|
+
let selectedChoicesImages = [];
|
|
33
|
+
Object.keys(selectedChoicesValue).forEach(choiceKey => {
|
|
34
|
+
const productOption = product?.options
|
|
35
|
+
?.find((option) => option.name === choiceKey)
|
|
36
|
+
?.choicesSettings?.choices?.find((choice) => choice.name === selectedChoicesValue[choiceKey]);
|
|
37
|
+
if (productOption) {
|
|
38
|
+
selectedChoicesImages.push(...(productOption.linkedMedia ?? []));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (selectedChoicesImages?.length) {
|
|
42
|
+
mediaToDisplay = selectedChoicesImages;
|
|
43
|
+
}
|
|
44
|
+
console.log({ mediaToDisplay });
|
|
45
|
+
// mediaService.setMediaToDisplay(mediaToDisplay ?? []);
|
|
46
|
+
});
|
|
13
47
|
const parsePrice = (amount) => {
|
|
14
48
|
if (!amount)
|
|
15
49
|
return 0;
|
|
@@ -30,28 +64,67 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
30
64
|
return choices;
|
|
31
65
|
};
|
|
32
66
|
const findVariantByChoices = (variants, selectedChoices) => {
|
|
33
|
-
return (variants.find(
|
|
67
|
+
return (variants.find(variant => {
|
|
34
68
|
const variantChoices = processVariantChoices(variant);
|
|
35
69
|
const choiceKeys = Object.keys(selectedChoices);
|
|
36
|
-
return choiceKeys.every(
|
|
70
|
+
return choiceKeys.every(key => variantChoices[key] === selectedChoices[key]);
|
|
37
71
|
}) || null);
|
|
38
72
|
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
73
|
+
const findVariantWithMinPrice = () => {
|
|
74
|
+
const variantsList = variants.get();
|
|
75
|
+
const variantPrices = variantsList.map(x => Number(x.price?.actualPrice?.formattedAmount) ||
|
|
76
|
+
Number(x.price?.actualPrice?.amount));
|
|
77
|
+
const minPrice = String(Math.min(...variantPrices));
|
|
78
|
+
return variantsList.find(x => x.price?.actualPrice?.formattedAmount === minPrice ||
|
|
79
|
+
x.price?.actualPrice?.amount === minPrice);
|
|
80
|
+
};
|
|
81
|
+
const updateInventoryItemData = async (variantId, inStock, preOrderEnabled) => {
|
|
82
|
+
if (!variantId) {
|
|
83
|
+
preOrderMessage.set(null);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
// Use the correct Wix inventoryItemsV3.queryInventoryItems() API
|
|
88
|
+
const queryResult = await (0, auto_sdk_stores_inventory_items_v_3_1.queryInventoryItems)()
|
|
89
|
+
.eq('variantId', variantId)
|
|
90
|
+
.find();
|
|
91
|
+
const inventoryItem = queryResult.items?.[0];
|
|
92
|
+
const isTrackingQuantity = inventoryItem?.trackQuantity ?? false;
|
|
93
|
+
trackQuantity.set(isTrackingQuantity);
|
|
94
|
+
preOrderMessage.set(inventoryItem?.preorderInfo?.message || null);
|
|
95
|
+
if (!inventoryItem || !isTrackingQuantity) {
|
|
96
|
+
quantityAvailable.set(null);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (inStock && inventoryItem?.quantity) {
|
|
100
|
+
quantityAvailable.set(inventoryItem.quantity);
|
|
101
|
+
}
|
|
102
|
+
else if (preOrderEnabled && inventoryItem.preorderInfo?.limit) {
|
|
103
|
+
quantityAvailable.set(inventoryItem.preorderInfo.limit);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
quantityAvailable.set(null);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
console.error('Failed to fetch inventory quantity:', error);
|
|
111
|
+
// Fallback on error
|
|
112
|
+
quantityAvailable.set(null);
|
|
113
|
+
trackQuantity.set(false);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const updateDataFromVariant = (variant) => {
|
|
44
117
|
if (variant) {
|
|
45
118
|
const inStock = variant.inventoryStatus?.inStock ?? true;
|
|
46
119
|
const preOrderEnabled = variant.inventoryStatus?.preorderEnabled ?? false;
|
|
47
|
-
//
|
|
48
|
-
|
|
120
|
+
// update the quantity available, tracking indication and pre-order message from the inventory API
|
|
121
|
+
updateInventoryItemData(variant._id, inStock, preOrderEnabled);
|
|
49
122
|
}
|
|
50
123
|
else {
|
|
51
124
|
quantityAvailable.set(0);
|
|
125
|
+
trackQuantity.set(false);
|
|
52
126
|
}
|
|
53
127
|
};
|
|
54
|
-
const selectedChoices = signalsService.signal({});
|
|
55
128
|
const isLoading = signalsService.signal(false);
|
|
56
129
|
const error = signalsService.signal(null);
|
|
57
130
|
const variants = signalsService.signal([]);
|
|
@@ -59,60 +132,70 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
59
132
|
const basePrice = signalsService.signal(0);
|
|
60
133
|
const discountPrice = signalsService.signal(null);
|
|
61
134
|
const isOnSale = signalsService.signal(null);
|
|
62
|
-
const quantityAvailable = signalsService.signal(
|
|
63
|
-
const
|
|
64
|
-
const
|
|
135
|
+
const quantityAvailable = signalsService.signal(null);
|
|
136
|
+
const trackQuantity = signalsService.signal(false);
|
|
137
|
+
const selectedQuantity = signalsService.signal(1);
|
|
138
|
+
const productId = signalsService.signal('');
|
|
65
139
|
const ribbonLabel = signalsService.signal(null);
|
|
66
|
-
const v3Product = signalsService.signal(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
140
|
+
const v3Product = signalsService.signal(initialProduct);
|
|
141
|
+
const init = (currentProduct) => {
|
|
142
|
+
if (currentProduct) {
|
|
143
|
+
v3Product.set(currentProduct);
|
|
144
|
+
productId.set(currentProduct._id || '');
|
|
145
|
+
ribbonLabel.set(currentProduct.ribbon?.name || null);
|
|
146
|
+
const actualPrice = currentProduct.actualPriceRange?.minValue?.amount;
|
|
147
|
+
const compareAtPrice = currentProduct.compareAtPriceRange?.minValue?.amount;
|
|
148
|
+
basePrice.set(parsePrice(actualPrice));
|
|
149
|
+
discountPrice.set(compareAtPrice ? parsePrice(compareAtPrice) : null);
|
|
150
|
+
isOnSale.set(!!compareAtPrice &&
|
|
151
|
+
parsePrice(compareAtPrice) > parsePrice(actualPrice));
|
|
152
|
+
if (currentProduct.options) {
|
|
153
|
+
const optionsMap = {};
|
|
154
|
+
currentProduct.options.forEach((option) => {
|
|
155
|
+
if (option.name && option.choicesSettings?.choices) {
|
|
156
|
+
optionsMap[option.name] = option.choicesSettings.choices.map((choice) => choice.name || '');
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
options.set(optionsMap);
|
|
160
|
+
}
|
|
161
|
+
if (currentProduct.variantSummary.variantCount > 1) {
|
|
162
|
+
variants.set(currentProduct.variantsInfo?.variants || []);
|
|
163
|
+
if (currentProduct.variantsInfo?.variants?.length) {
|
|
164
|
+
// updateDataFromVariant(currentProduct.variantsInfo?.variants[0]);
|
|
80
165
|
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
const singleVariant = {
|
|
169
|
+
_id: 'default',
|
|
170
|
+
visible: true,
|
|
171
|
+
choices: [],
|
|
172
|
+
price: {
|
|
173
|
+
actualPrice: currentProduct.actualPriceRange?.minValue,
|
|
174
|
+
compareAtPrice: currentProduct.compareAtPriceRange?.minValue,
|
|
175
|
+
},
|
|
176
|
+
inventoryStatus: {
|
|
177
|
+
inStock: currentProduct.inventory?.availabilityStatus ===
|
|
178
|
+
auto_sdk_stores_products_v_3_1.InventoryAvailabilityStatus.IN_STOCK ||
|
|
179
|
+
currentProduct.inventory?.availabilityStatus ===
|
|
180
|
+
auto_sdk_stores_products_v_3_1.InventoryAvailabilityStatus.PARTIALLY_OUT_OF_STOCK,
|
|
181
|
+
preorderEnabled: currentProduct.inventory?.preorderStatus === 'ENABLED',
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
variants.set([singleVariant]);
|
|
185
|
+
updateDataFromVariant(singleVariant);
|
|
88
186
|
}
|
|
89
187
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
price: {
|
|
96
|
-
actualPrice: configProduct.actualPriceRange?.minValue,
|
|
97
|
-
compareAtPrice: configProduct.compareAtPriceRange?.minValue,
|
|
98
|
-
},
|
|
99
|
-
inventoryStatus: {
|
|
100
|
-
inStock: configProduct.inventory?.availabilityStatus === "IN_STOCK" ||
|
|
101
|
-
configProduct.inventory?.availabilityStatus ===
|
|
102
|
-
"PARTIALLY_OUT_OF_STOCK",
|
|
103
|
-
preorderEnabled: configProduct.inventory?.preorderStatus === "ENABLED",
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
variants.set([singleVariant]);
|
|
107
|
-
updateQuantityFromVariant(singleVariant);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
188
|
+
};
|
|
189
|
+
signalsService.effect(() => {
|
|
190
|
+
const currentProduct = productService.product.get();
|
|
191
|
+
init(currentProduct);
|
|
192
|
+
});
|
|
110
193
|
const currentVariant = signalsService.computed((() => {
|
|
111
|
-
const prod = v3Product.get();
|
|
112
194
|
const choices = selectedChoices.get();
|
|
113
|
-
|
|
195
|
+
const variantsList = variants.get();
|
|
196
|
+
if (!variantsList || variantsList.length === 0)
|
|
114
197
|
return null;
|
|
115
|
-
return (
|
|
198
|
+
return (variantsList.find((variant) => {
|
|
116
199
|
const variantChoices = processVariantChoices(variant);
|
|
117
200
|
if (Object.keys(choices).length !== Object.keys(variantChoices).length)
|
|
118
201
|
return false;
|
|
@@ -125,8 +208,19 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
125
208
|
const variant = currentVariant.get();
|
|
126
209
|
return variant?._id || null;
|
|
127
210
|
});
|
|
211
|
+
const variantWithMinPrice = findVariantWithMinPrice();
|
|
128
212
|
const currentPrice = signalsService.computed(() => {
|
|
129
213
|
const variant = currentVariant.get();
|
|
214
|
+
if (!variant && variantWithMinPrice) {
|
|
215
|
+
const formattedAmount = variantWithMinPrice?.price?.actualPrice?.formattedAmount;
|
|
216
|
+
if (formattedAmount) {
|
|
217
|
+
return formattedAmount;
|
|
218
|
+
}
|
|
219
|
+
const amount = variantWithMinPrice?.price?.actualPrice?.amount;
|
|
220
|
+
if (amount) {
|
|
221
|
+
return `$${amount}`;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
130
224
|
const prod = v3Product.get();
|
|
131
225
|
// Try to get formatted amount first (if fields worked)
|
|
132
226
|
if (variant?.price?.actualPrice?.formattedAmount) {
|
|
@@ -143,11 +237,21 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
143
237
|
else if (prod?.actualPriceRange?.minValue?.amount) {
|
|
144
238
|
rawAmount = prod.actualPriceRange.minValue.amount;
|
|
145
239
|
}
|
|
146
|
-
return rawAmount ? `$${rawAmount}` :
|
|
240
|
+
return rawAmount ? `$${rawAmount}` : '';
|
|
147
241
|
});
|
|
148
242
|
const currentCompareAtPrice = signalsService.computed(() => {
|
|
149
243
|
const variant = currentVariant.get();
|
|
150
244
|
const prod = v3Product.get();
|
|
245
|
+
if (!variant && variantWithMinPrice) {
|
|
246
|
+
const formattedAmount = variantWithMinPrice?.price?.compareAtPrice?.formattedAmount;
|
|
247
|
+
if (formattedAmount) {
|
|
248
|
+
return formattedAmount;
|
|
249
|
+
}
|
|
250
|
+
const amount = variantWithMinPrice?.price?.compareAtPrice?.amount;
|
|
251
|
+
if (amount) {
|
|
252
|
+
return `$${amount}`;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
151
255
|
// Try to get formatted compare-at price first
|
|
152
256
|
if (variant?.price?.compareAtPrice?.formattedAmount) {
|
|
153
257
|
return variant.price.compareAtPrice.formattedAmount;
|
|
@@ -185,41 +289,48 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
185
289
|
});
|
|
186
290
|
const currency = signalsService.computed(() => {
|
|
187
291
|
const prod = v3Product.get();
|
|
188
|
-
return prod?.currency ||
|
|
292
|
+
return prod?.currency || 'USD';
|
|
189
293
|
});
|
|
190
294
|
const selectedVariant = () => {
|
|
191
295
|
const variantId = selectedVariantId.get();
|
|
192
296
|
const variantsList = variants.get();
|
|
193
|
-
return variantsList.find(
|
|
297
|
+
return variantsList.find(v => v._id === variantId) || null;
|
|
194
298
|
};
|
|
195
299
|
const finalPrice = () => {
|
|
196
300
|
const discount = discountPrice.get();
|
|
197
301
|
const base = basePrice.get();
|
|
198
302
|
return discount !== null ? discount : base;
|
|
199
303
|
};
|
|
200
|
-
|
|
201
|
-
|
|
304
|
+
const isLowStock = () => {
|
|
305
|
+
// Note: Currently always returns false as inventory quantity tracking
|
|
306
|
+
// is handled separately by the inventory service
|
|
202
307
|
return false;
|
|
203
308
|
};
|
|
204
309
|
const setSelectedChoices = (choices) => {
|
|
205
310
|
selectedChoices.set(choices);
|
|
311
|
+
selectedQuantity.set(1); // Reset quantity when choices change
|
|
206
312
|
const matchingVariant = findVariantByChoices(variants.get(), choices);
|
|
207
|
-
|
|
313
|
+
updateDataFromVariant(matchingVariant);
|
|
208
314
|
};
|
|
209
|
-
const addToCart = async (
|
|
315
|
+
const addToCart = async (_quantity = 1, modifiers) => {
|
|
210
316
|
try {
|
|
211
317
|
isLoading.set(true);
|
|
212
318
|
error.set(null);
|
|
213
319
|
const prod = v3Product.get();
|
|
214
320
|
const variant = currentVariant.get();
|
|
215
321
|
if (!prod?._id) {
|
|
216
|
-
throw new Error(
|
|
322
|
+
throw new Error('Product not found');
|
|
217
323
|
}
|
|
218
324
|
// Build catalog reference with modifiers if provided
|
|
219
325
|
const catalogReference = {
|
|
220
326
|
catalogItemId: prod._id,
|
|
221
|
-
appId:
|
|
222
|
-
options: variant?._id
|
|
327
|
+
appId: '215238eb-22a5-4c36-9e7b-e7c08025e04e',
|
|
328
|
+
options: variant?._id && variant._id !== 'default'
|
|
329
|
+
? {
|
|
330
|
+
variantId: variant._id,
|
|
331
|
+
preOrderRequested: !!variant?.inventoryStatus?.preorderEnabled,
|
|
332
|
+
}
|
|
333
|
+
: undefined,
|
|
223
334
|
};
|
|
224
335
|
// Transform and add modifiers to catalog reference if they exist
|
|
225
336
|
if (modifiers && Object.keys(modifiers).length > 0) {
|
|
@@ -229,19 +340,19 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
229
340
|
const productModifiers = prod.modifiers || [];
|
|
230
341
|
Object.values(modifiers).forEach((modifierValue) => {
|
|
231
342
|
const modifierName = modifierValue.modifierName;
|
|
232
|
-
const productModifier = productModifiers.find(
|
|
343
|
+
const productModifier = productModifiers.find(m => m.name === modifierName);
|
|
233
344
|
if (!productModifier)
|
|
234
345
|
return;
|
|
235
346
|
const renderType = productModifier.modifierRenderType;
|
|
236
|
-
if (renderType ===
|
|
237
|
-
renderType ===
|
|
347
|
+
if (renderType === auto_sdk_stores_products_v_3_1.ModifierRenderType.TEXT_CHOICES ||
|
|
348
|
+
renderType === auto_sdk_stores_products_v_3_1.ModifierRenderType.SWATCH_CHOICES) {
|
|
238
349
|
// For choice modifiers, use the modifier key and choice value
|
|
239
350
|
const modifierKey = productModifier.key || modifierName;
|
|
240
351
|
if (modifierValue.choiceValue) {
|
|
241
352
|
options[modifierKey] = modifierValue.choiceValue;
|
|
242
353
|
}
|
|
243
354
|
}
|
|
244
|
-
else if (renderType ===
|
|
355
|
+
else if (renderType === auto_sdk_stores_products_v_3_1.ModifierRenderType.FREE_TEXT) {
|
|
245
356
|
// For free text modifiers, use the freeTextSettings key
|
|
246
357
|
const freeTextKey = productModifier.freeTextSettings?.key || modifierName;
|
|
247
358
|
if (modifierValue.freeTextValue) {
|
|
@@ -263,17 +374,16 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
263
374
|
};
|
|
264
375
|
}
|
|
265
376
|
}
|
|
266
|
-
//
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
];
|
|
377
|
+
// const lineItems = [
|
|
378
|
+
// {
|
|
379
|
+
// catalogReference,
|
|
380
|
+
// quantity,
|
|
381
|
+
// },
|
|
382
|
+
// ];
|
|
273
383
|
// await cartService.addToCart(lineItems);
|
|
274
384
|
}
|
|
275
385
|
catch (err) {
|
|
276
|
-
error.set(err instanceof Error ? err.message :
|
|
386
|
+
error.set(err instanceof Error ? err.message : 'Failed to add to cart');
|
|
277
387
|
}
|
|
278
388
|
finally {
|
|
279
389
|
isLoading.set(false);
|
|
@@ -286,21 +396,33 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
286
396
|
};
|
|
287
397
|
const selectVariantById = (id) => {
|
|
288
398
|
const variantsList = variants.get();
|
|
289
|
-
const variant = variantsList.find(
|
|
399
|
+
const variant = variantsList.find(v => v._id === id);
|
|
290
400
|
if (variant) {
|
|
291
401
|
const variantChoices = processVariantChoices(variant);
|
|
292
402
|
selectedChoices.set(variantChoices);
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
const loadProductVariants = (data) => {
|
|
297
|
-
variants.set(data);
|
|
298
|
-
if (data.length > 0) {
|
|
299
|
-
updateQuantityFromVariant(data[0] || null);
|
|
403
|
+
updateDataFromVariant(variant);
|
|
300
404
|
}
|
|
301
405
|
};
|
|
302
406
|
const resetSelections = () => {
|
|
303
407
|
selectedChoices.set({});
|
|
408
|
+
selectedQuantity.set(1); // Reset quantity when resetting selections
|
|
409
|
+
};
|
|
410
|
+
// Quantity management methods
|
|
411
|
+
const setSelectedQuantity = (quantity) => {
|
|
412
|
+
const maxQuantity = quantityAvailable.get();
|
|
413
|
+
const validQuantity = Math.max(1, Math.min(quantity, maxQuantity || 999));
|
|
414
|
+
selectedQuantity.set(validQuantity);
|
|
415
|
+
};
|
|
416
|
+
const incrementQuantity = () => {
|
|
417
|
+
const current = selectedQuantity.get();
|
|
418
|
+
const maxQuantity = quantityAvailable.get();
|
|
419
|
+
const newQuantity = Math.min(current + 1, maxQuantity || 999);
|
|
420
|
+
selectedQuantity.set(newQuantity);
|
|
421
|
+
};
|
|
422
|
+
const decrementQuantity = () => {
|
|
423
|
+
const current = selectedQuantity.get();
|
|
424
|
+
const newQuantity = Math.max(1, current - 1);
|
|
425
|
+
selectedQuantity.set(newQuantity);
|
|
304
426
|
};
|
|
305
427
|
// New methods for smart variant selection
|
|
306
428
|
const getAvailableChoicesForOption = (optionName) => {
|
|
@@ -308,7 +430,7 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
308
430
|
const variantsList = variants.get();
|
|
309
431
|
// Get all possible choices for this option that result in valid variants
|
|
310
432
|
const availableChoices = new Set();
|
|
311
|
-
variantsList.forEach(
|
|
433
|
+
variantsList.forEach(variant => {
|
|
312
434
|
const variantChoices = processVariantChoices(variant);
|
|
313
435
|
// Check if this variant matches all currently selected choices (except for the option we're checking)
|
|
314
436
|
const matchesOtherChoices = Object.entries(currentChoices)
|
|
@@ -320,14 +442,57 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
320
442
|
});
|
|
321
443
|
return Array.from(availableChoices);
|
|
322
444
|
};
|
|
445
|
+
// Core method that provides both availability and stock info efficiently
|
|
446
|
+
const getChoiceInfo = (optionName, choiceValue) => {
|
|
447
|
+
// Create hypothetical choices with this choice selected
|
|
448
|
+
const currentChoices = selectedChoices.get();
|
|
449
|
+
const hypotheticalChoices = {
|
|
450
|
+
...currentChoices,
|
|
451
|
+
[optionName]: choiceValue,
|
|
452
|
+
};
|
|
453
|
+
// Get all variants and find one that matches these choices
|
|
454
|
+
const variantsList = variants.get();
|
|
455
|
+
const matchingVariants = variantsList.filter(variant => {
|
|
456
|
+
if (!variant.choices)
|
|
457
|
+
return false;
|
|
458
|
+
const variantChoices = {};
|
|
459
|
+
for (const choice of variant.choices) {
|
|
460
|
+
if (choice.optionChoiceNames?.optionName &&
|
|
461
|
+
choice.optionChoiceNames?.choiceName) {
|
|
462
|
+
variantChoices[choice.optionChoiceNames.optionName] =
|
|
463
|
+
choice.optionChoiceNames.choiceName;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
// Check if this variant matches our hypothetical choices
|
|
467
|
+
return Object.entries(hypotheticalChoices).every(([key, value]) => variantChoices[key] === value);
|
|
468
|
+
});
|
|
469
|
+
const isAvailable = matchingVariants.length > 0;
|
|
470
|
+
// Check if ANY of the matching variants are in stock
|
|
471
|
+
const isInStock = matchingVariants.some(variant => variant.inventoryStatus?.inStock === true);
|
|
472
|
+
// Check if ANY of the matching variants have pre-order enabled
|
|
473
|
+
const isPreOrderEnabled = matchingVariants.some(variant => variant.inventoryStatus?.preorderEnabled === true);
|
|
474
|
+
return { isAvailable, isInStock, isPreOrderEnabled };
|
|
475
|
+
};
|
|
476
|
+
// Simplified methods using the core getChoiceInfo
|
|
323
477
|
const isChoiceAvailable = (optionName, choiceValue) => {
|
|
324
|
-
|
|
325
|
-
|
|
478
|
+
return getChoiceInfo(optionName, choiceValue).isAvailable;
|
|
479
|
+
};
|
|
480
|
+
const isChoiceInStock = (optionName, choiceValue) => {
|
|
481
|
+
return getChoiceInfo(optionName, choiceValue).isInStock;
|
|
482
|
+
};
|
|
483
|
+
const isChoicePreOrderEnabled = (optionName, choiceValue) => {
|
|
484
|
+
return getChoiceInfo(optionName, choiceValue).isPreOrderEnabled;
|
|
326
485
|
};
|
|
327
486
|
const hasAnySelections = () => {
|
|
328
487
|
const currentChoices = selectedChoices.get();
|
|
329
488
|
return Object.keys(currentChoices).length > 0;
|
|
330
489
|
};
|
|
490
|
+
const IsAllVariantsAreOutOfStock = () => {
|
|
491
|
+
const variantsList = variants.get();
|
|
492
|
+
// All variants must be out of stock AND none should have preorder enabled
|
|
493
|
+
return (variantsList?.every(variant => !variant.inventoryStatus?.inStock &&
|
|
494
|
+
!variant.inventoryStatus?.preorderEnabled) ?? true);
|
|
495
|
+
};
|
|
331
496
|
return {
|
|
332
497
|
selectedChoices,
|
|
333
498
|
selectedVariantId,
|
|
@@ -336,6 +501,7 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
336
501
|
currentCompareAtPrice,
|
|
337
502
|
isInStock,
|
|
338
503
|
isPreOrderEnabled,
|
|
504
|
+
preOrderMessage,
|
|
339
505
|
isLoading,
|
|
340
506
|
error,
|
|
341
507
|
variants,
|
|
@@ -344,53 +510,32 @@ exports.SelectedVariantService = services_definitions_1.implementService.withCon
|
|
|
344
510
|
discountPrice,
|
|
345
511
|
isOnSale,
|
|
346
512
|
quantityAvailable,
|
|
513
|
+
trackQuantity,
|
|
514
|
+
selectedQuantity,
|
|
347
515
|
productId,
|
|
348
|
-
sku,
|
|
349
516
|
ribbonLabel,
|
|
350
517
|
setSelectedChoices,
|
|
351
518
|
addToCart,
|
|
352
519
|
setOption,
|
|
353
520
|
selectVariantById,
|
|
354
|
-
loadProductVariants,
|
|
355
521
|
resetSelections,
|
|
356
522
|
// New methods for smart variant selection
|
|
357
523
|
getAvailableChoicesForOption,
|
|
524
|
+
getChoiceInfo,
|
|
358
525
|
isChoiceAvailable,
|
|
526
|
+
isChoiceInStock,
|
|
527
|
+
isChoicePreOrderEnabled,
|
|
359
528
|
hasAnySelections,
|
|
529
|
+
// Quantity management methods
|
|
530
|
+
setSelectedQuantity,
|
|
531
|
+
incrementQuantity,
|
|
532
|
+
decrementQuantity,
|
|
360
533
|
selectedVariant,
|
|
361
534
|
finalPrice,
|
|
362
535
|
isLowStock,
|
|
363
536
|
product,
|
|
364
537
|
productOptions,
|
|
365
538
|
currency,
|
|
539
|
+
IsAllVariantsAreOutOfStock,
|
|
366
540
|
};
|
|
367
541
|
});
|
|
368
|
-
async function loadSelectedVariantServiceConfig(productSlug) {
|
|
369
|
-
try {
|
|
370
|
-
// Use getProductBySlug directly - single API call with comprehensive fields
|
|
371
|
-
const productResponse = await stores_1.productsV3.getProductBySlug(productSlug, {
|
|
372
|
-
fields: [
|
|
373
|
-
"DESCRIPTION",
|
|
374
|
-
"DIRECT_CATEGORIES_INFO",
|
|
375
|
-
"BREADCRUMBS_INFO",
|
|
376
|
-
"INFO_SECTION",
|
|
377
|
-
"MEDIA_ITEMS_INFO",
|
|
378
|
-
"PLAIN_DESCRIPTION",
|
|
379
|
-
"THUMBNAIL",
|
|
380
|
-
"URL",
|
|
381
|
-
"VARIANT_OPTION_CHOICE_NAMES",
|
|
382
|
-
"WEIGHT_MEASUREMENT_UNIT_INFO",
|
|
383
|
-
],
|
|
384
|
-
});
|
|
385
|
-
if (!productResponse.product) {
|
|
386
|
-
throw new Error("Product not found");
|
|
387
|
-
}
|
|
388
|
-
return {
|
|
389
|
-
product: productResponse.product,
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
catch (error) {
|
|
393
|
-
console.error(`Failed to load product for slug "${productSlug}":`, error);
|
|
394
|
-
throw error;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type ServiceFactoryConfig } from
|
|
2
|
-
import type { Signal } from
|
|
1
|
+
import { type ServiceFactoryConfig } from '@wix/services-definitions';
|
|
2
|
+
import type { Signal } from '../../Signal';
|
|
3
3
|
export interface SharingPlatform {
|
|
4
4
|
name: string;
|
|
5
5
|
icon: string;
|