@anker-in/shopify-react 0.1.1-beta.21 → 0.1.1-beta.23
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/dist/hooks/index.d.mts +245 -210
- package/dist/hooks/index.d.ts +245 -210
- package/dist/hooks/index.js +72 -9
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +71 -8
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +72 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +71 -8
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +1 -2
- package/dist/provider/index.d.ts +1 -2
- package/dist/provider/index.js +9 -7
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +8 -6
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-C1So3siD.d.mts → types-BSsb8OPm.d.mts} +1 -34
- package/dist/{types-C1So3siD.d.ts → types-BSsb8OPm.d.ts} +1 -34
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createContext, useMemo, useContext, useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
-
import { createShopifyClient,
|
|
2
|
+
import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
|
|
3
3
|
export * from '@anker-in/shopify-sdk';
|
|
4
4
|
import Cookies5 from 'js-cookie';
|
|
5
5
|
import { jsx } from 'react/jsx-runtime';
|
|
6
6
|
import Decimal2 from 'decimal.js';
|
|
7
|
+
import { atobID, btoaID } from '@anker-in/shopify-core';
|
|
7
8
|
import useSWR from 'swr';
|
|
8
9
|
import useSWRMutation from 'swr/mutation';
|
|
9
10
|
import { useRequest } from 'ahooks';
|
|
@@ -140,9 +141,10 @@ function normalizeAddToCartLines(lines) {
|
|
|
140
141
|
const variant = line.variant;
|
|
141
142
|
const product = variant.product;
|
|
142
143
|
const quantity = line.quantity || 1;
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
const
|
|
144
|
+
const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
145
|
+
const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
|
|
146
|
+
const subtotalAmount = originalPrice * quantity;
|
|
147
|
+
const totalAmount = finalPrice * quantity;
|
|
146
148
|
return {
|
|
147
149
|
id: `temp-line-${index}-${variant.id}`,
|
|
148
150
|
// Temporary ID for pre-cart lines
|
|
@@ -156,7 +158,7 @@ function normalizeAddToCartLines(lines) {
|
|
|
156
158
|
customAttributes: line.attributes || [],
|
|
157
159
|
variant: {
|
|
158
160
|
id: variant.id,
|
|
159
|
-
price,
|
|
161
|
+
price: variant.price?.amount ? Number(variant.price.amount) : 0,
|
|
160
162
|
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
161
163
|
sku: variant.sku || "",
|
|
162
164
|
name: variant.title || "",
|
|
@@ -998,6 +1000,61 @@ function useRemoveCartCodes(options) {
|
|
|
998
1000
|
return useSWRMutation("remove-codes", removeCodes, options);
|
|
999
1001
|
}
|
|
1000
1002
|
|
|
1003
|
+
// src/hooks/cart/utils/add-to-cart.ts
|
|
1004
|
+
var getLinesWithAttributes = ({
|
|
1005
|
+
cart,
|
|
1006
|
+
lineItems
|
|
1007
|
+
}) => {
|
|
1008
|
+
return lineItems.map((line) => {
|
|
1009
|
+
const sameLineInCart = cart?.lineItems.find(
|
|
1010
|
+
(lineInCart) => lineInCart.variant.sku === line.variant?.sku && lineInCart.product?.handle === line.variant?.product?.handle
|
|
1011
|
+
);
|
|
1012
|
+
const codeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
1013
|
+
(attr) => attr.key === CODE_AMOUNT_KEY
|
|
1014
|
+
);
|
|
1015
|
+
const scriptCodeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
1016
|
+
(attr) => attr.key === SCRIPT_CODE_AMOUNT_KEY
|
|
1017
|
+
);
|
|
1018
|
+
let functionAttribute = null;
|
|
1019
|
+
try {
|
|
1020
|
+
functionAttribute = sameLineInCart?.customAttributes?.find(
|
|
1021
|
+
(attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY && JSON.parse(attr.value)?.discounted_amount
|
|
1022
|
+
);
|
|
1023
|
+
} catch (error) {
|
|
1024
|
+
}
|
|
1025
|
+
if (codeAmountAttribute || functionAttribute || scriptCodeAmountAttribute) {
|
|
1026
|
+
return {
|
|
1027
|
+
...line,
|
|
1028
|
+
attributes: [
|
|
1029
|
+
...line.attributes || [],
|
|
1030
|
+
codeAmountAttribute,
|
|
1031
|
+
functionAttribute,
|
|
1032
|
+
scriptCodeAmountAttribute
|
|
1033
|
+
].filter(Boolean)
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
return line;
|
|
1037
|
+
});
|
|
1038
|
+
};
|
|
1039
|
+
var getLinesWithFunctionAttributes = (lineItems) => {
|
|
1040
|
+
return lineItems.map((line) => {
|
|
1041
|
+
let itemAttributes = line.attributes || [];
|
|
1042
|
+
const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
1043
|
+
if (!functionEnvAttribute) {
|
|
1044
|
+
itemAttributes = itemAttributes.concat([
|
|
1045
|
+
{
|
|
1046
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1047
|
+
value: JSON.stringify({
|
|
1048
|
+
is_gift: false,
|
|
1049
|
+
discounted_amount: Number(line.variant?.finalPrice?.amount || line.variant?.price?.amount) * (line.quantity || 1)
|
|
1050
|
+
})
|
|
1051
|
+
}
|
|
1052
|
+
]);
|
|
1053
|
+
}
|
|
1054
|
+
return { ...line, attributes: itemAttributes };
|
|
1055
|
+
});
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1001
1058
|
// src/hooks/cart/use-add-to-cart.ts
|
|
1002
1059
|
function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
1003
1060
|
const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
|
|
@@ -1021,7 +1078,12 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
1021
1078
|
if (!lineItems || lineItems.length === 0) {
|
|
1022
1079
|
return;
|
|
1023
1080
|
}
|
|
1024
|
-
const
|
|
1081
|
+
const linesWithAttributes = getLinesWithAttributes({
|
|
1082
|
+
cart,
|
|
1083
|
+
lineItems
|
|
1084
|
+
});
|
|
1085
|
+
const linesWithFunctionAttributes = getLinesWithFunctionAttributes(linesWithAttributes);
|
|
1086
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
1025
1087
|
merchandiseId: item.variant?.id || "",
|
|
1026
1088
|
quantity: item.quantity || 1,
|
|
1027
1089
|
attributes: item.attributes,
|
|
@@ -1172,7 +1234,8 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
1172
1234
|
if (!lineItems || lineItems.length === 0) {
|
|
1173
1235
|
return;
|
|
1174
1236
|
}
|
|
1175
|
-
const
|
|
1237
|
+
const linesWithFunctionAttributes = getLinesWithFunctionAttributes(lineItems);
|
|
1238
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
1176
1239
|
merchandiseId: item.variant?.id || "",
|
|
1177
1240
|
quantity: item.quantity || 1,
|
|
1178
1241
|
attributes: item.attributes,
|
|
@@ -1625,7 +1688,7 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1625
1688
|
);
|
|
1626
1689
|
const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
|
|
1627
1690
|
const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
|
|
1628
|
-
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute) {
|
|
1691
|
+
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute && !functionEnvValue.is_gift) {
|
|
1629
1692
|
attrNeedUpdate.push({
|
|
1630
1693
|
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1631
1694
|
value: JSON.stringify({
|