@anker-in/shopify-react 0.1.1-beta.22 → 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 +64 -3
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +64 -3
- 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 +64 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +64 -3
- 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 +1 -1
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +1 -1
- 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 +5 -5
package/dist/hooks/index.mjs
CHANGED
|
@@ -903,6 +903,61 @@ function useRemoveCartCodes(options) {
|
|
|
903
903
|
return useSWRMutation("remove-codes", removeCodes, options);
|
|
904
904
|
}
|
|
905
905
|
|
|
906
|
+
// src/hooks/cart/utils/add-to-cart.ts
|
|
907
|
+
var getLinesWithAttributes = ({
|
|
908
|
+
cart,
|
|
909
|
+
lineItems
|
|
910
|
+
}) => {
|
|
911
|
+
return lineItems.map((line) => {
|
|
912
|
+
const sameLineInCart = cart?.lineItems.find(
|
|
913
|
+
(lineInCart) => lineInCart.variant.sku === line.variant?.sku && lineInCart.product?.handle === line.variant?.product?.handle
|
|
914
|
+
);
|
|
915
|
+
const codeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
916
|
+
(attr) => attr.key === CODE_AMOUNT_KEY
|
|
917
|
+
);
|
|
918
|
+
const scriptCodeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
919
|
+
(attr) => attr.key === SCRIPT_CODE_AMOUNT_KEY
|
|
920
|
+
);
|
|
921
|
+
let functionAttribute = null;
|
|
922
|
+
try {
|
|
923
|
+
functionAttribute = sameLineInCart?.customAttributes?.find(
|
|
924
|
+
(attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY && JSON.parse(attr.value)?.discounted_amount
|
|
925
|
+
);
|
|
926
|
+
} catch (error) {
|
|
927
|
+
}
|
|
928
|
+
if (codeAmountAttribute || functionAttribute || scriptCodeAmountAttribute) {
|
|
929
|
+
return {
|
|
930
|
+
...line,
|
|
931
|
+
attributes: [
|
|
932
|
+
...line.attributes || [],
|
|
933
|
+
codeAmountAttribute,
|
|
934
|
+
functionAttribute,
|
|
935
|
+
scriptCodeAmountAttribute
|
|
936
|
+
].filter(Boolean)
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
return line;
|
|
940
|
+
});
|
|
941
|
+
};
|
|
942
|
+
var getLinesWithFunctionAttributes = (lineItems) => {
|
|
943
|
+
return lineItems.map((line) => {
|
|
944
|
+
let itemAttributes = line.attributes || [];
|
|
945
|
+
const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
946
|
+
if (!functionEnvAttribute) {
|
|
947
|
+
itemAttributes = itemAttributes.concat([
|
|
948
|
+
{
|
|
949
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
950
|
+
value: JSON.stringify({
|
|
951
|
+
is_gift: false,
|
|
952
|
+
discounted_amount: Number(line.variant?.finalPrice?.amount || line.variant?.price?.amount) * (line.quantity || 1)
|
|
953
|
+
})
|
|
954
|
+
}
|
|
955
|
+
]);
|
|
956
|
+
}
|
|
957
|
+
return { ...line, attributes: itemAttributes };
|
|
958
|
+
});
|
|
959
|
+
};
|
|
960
|
+
|
|
906
961
|
// src/hooks/cart/use-add-to-cart.ts
|
|
907
962
|
function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
908
963
|
const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
|
|
@@ -926,7 +981,12 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
926
981
|
if (!lineItems || lineItems.length === 0) {
|
|
927
982
|
return;
|
|
928
983
|
}
|
|
929
|
-
const
|
|
984
|
+
const linesWithAttributes = getLinesWithAttributes({
|
|
985
|
+
cart,
|
|
986
|
+
lineItems
|
|
987
|
+
});
|
|
988
|
+
const linesWithFunctionAttributes = getLinesWithFunctionAttributes(linesWithAttributes);
|
|
989
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
930
990
|
merchandiseId: item.variant?.id || "",
|
|
931
991
|
quantity: item.quantity || 1,
|
|
932
992
|
attributes: item.attributes,
|
|
@@ -1077,7 +1137,8 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
1077
1137
|
if (!lineItems || lineItems.length === 0) {
|
|
1078
1138
|
return;
|
|
1079
1139
|
}
|
|
1080
|
-
const
|
|
1140
|
+
const linesWithFunctionAttributes = getLinesWithFunctionAttributes(lineItems);
|
|
1141
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
1081
1142
|
merchandiseId: item.variant?.id || "",
|
|
1082
1143
|
quantity: item.quantity || 1,
|
|
1083
1144
|
attributes: item.attributes,
|
|
@@ -1530,7 +1591,7 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1530
1591
|
);
|
|
1531
1592
|
const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
|
|
1532
1593
|
const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
|
|
1533
|
-
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute) {
|
|
1594
|
+
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute && !functionEnvValue.is_gift) {
|
|
1534
1595
|
attrNeedUpdate.push({
|
|
1535
1596
|
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1536
1597
|
value: JSON.stringify({
|