@anker-in/shopify-react 1.2.4-beta.0 → 1.2.5
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 +6 -23
- package/dist/hooks/index.d.ts +6 -23
- package/dist/hooks/index.js +171 -139
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +173 -141
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +61 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +63 -7
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +3 -1
- package/dist/provider/index.d.ts +3 -1
- package/dist/provider/index.js +173 -7
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +175 -9
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-DfR13pDe.d.mts → types-C0UyuPrG.d.mts} +12 -5
- package/dist/{types-DfR13pDe.d.ts → types-C0UyuPrG.d.ts} +12 -5
- package/package.json +3 -3
package/dist/hooks/index.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as swr_mutation from 'swr/mutation';
|
|
2
2
|
import { SWRMutationConfiguration } from 'swr/mutation';
|
|
3
3
|
import * as _anker_in_shopify_sdk from '@anker-in/shopify-sdk';
|
|
4
|
-
import { CartLineInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
|
|
5
|
-
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-
|
|
6
|
-
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-
|
|
4
|
+
import { CartLineInput, BuyerIdentityInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
|
|
5
|
+
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-C0UyuPrG.mjs';
|
|
6
|
+
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-C0UyuPrG.mjs';
|
|
7
7
|
import * as swr from 'swr';
|
|
8
8
|
import swr__default, { SWRConfiguration } from 'swr';
|
|
9
9
|
import * as swr__internal from 'swr/_internal';
|
|
@@ -16,10 +16,7 @@ interface CreateCartInput {
|
|
|
16
16
|
/** Lines to add to the cart */
|
|
17
17
|
lines?: CartLineInput[];
|
|
18
18
|
/** Buyer identity for cart creation */
|
|
19
|
-
buyerIdentity?:
|
|
20
|
-
email?: string;
|
|
21
|
-
countryCode?: string;
|
|
22
|
-
};
|
|
19
|
+
buyerIdentity?: BuyerIdentityInput;
|
|
23
20
|
/** Discount codes */
|
|
24
21
|
discountCodes?: string[];
|
|
25
22
|
/** Custom attributes */
|
|
@@ -502,18 +499,7 @@ interface UpdateBuyerIdentityInput {
|
|
|
502
499
|
/** Cart ID (optional, will use cookie) */
|
|
503
500
|
cartId?: string;
|
|
504
501
|
/** Buyer identity information */
|
|
505
|
-
buyerIdentity:
|
|
506
|
-
/** The email address of the buyer */
|
|
507
|
-
email?: string;
|
|
508
|
-
/** The phone number of the buyer */
|
|
509
|
-
phone?: string;
|
|
510
|
-
/** The country where the buyer is located */
|
|
511
|
-
countryCode?: string;
|
|
512
|
-
/** The access token used to identify the customer */
|
|
513
|
-
customerAccessToken?: string;
|
|
514
|
-
/** The company location of the buyer */
|
|
515
|
-
companyLocationId?: string;
|
|
516
|
-
};
|
|
502
|
+
buyerIdentity: BuyerIdentityInput;
|
|
517
503
|
}
|
|
518
504
|
interface UseUpdateBuyerIdentityOptions {
|
|
519
505
|
/** Function to update cart state */
|
|
@@ -571,10 +557,7 @@ interface BuyNowInput {
|
|
|
571
557
|
value: string;
|
|
572
558
|
}>;
|
|
573
559
|
/** Buyer identity */
|
|
574
|
-
buyerIdentity?:
|
|
575
|
-
email?: string;
|
|
576
|
-
countryCode?: string;
|
|
577
|
-
};
|
|
560
|
+
buyerIdentity?: BuyerIdentityInput;
|
|
578
561
|
/** GTM tracking parameters */
|
|
579
562
|
gtmParams?: Omit<GtmParams, 'brand'>;
|
|
580
563
|
/** Facebook Pixel tracking configuration */
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as swr_mutation from 'swr/mutation';
|
|
2
2
|
import { SWRMutationConfiguration } from 'swr/mutation';
|
|
3
3
|
import * as _anker_in_shopify_sdk from '@anker-in/shopify-sdk';
|
|
4
|
-
import { CartLineInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
|
|
5
|
-
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-
|
|
6
|
-
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-
|
|
4
|
+
import { CartLineInput, BuyerIdentityInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
|
|
5
|
+
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-C0UyuPrG.js';
|
|
6
|
+
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-C0UyuPrG.js';
|
|
7
7
|
import * as swr from 'swr';
|
|
8
8
|
import swr__default, { SWRConfiguration } from 'swr';
|
|
9
9
|
import * as swr__internal from 'swr/_internal';
|
|
@@ -16,10 +16,7 @@ interface CreateCartInput {
|
|
|
16
16
|
/** Lines to add to the cart */
|
|
17
17
|
lines?: CartLineInput[];
|
|
18
18
|
/** Buyer identity for cart creation */
|
|
19
|
-
buyerIdentity?:
|
|
20
|
-
email?: string;
|
|
21
|
-
countryCode?: string;
|
|
22
|
-
};
|
|
19
|
+
buyerIdentity?: BuyerIdentityInput;
|
|
23
20
|
/** Discount codes */
|
|
24
21
|
discountCodes?: string[];
|
|
25
22
|
/** Custom attributes */
|
|
@@ -502,18 +499,7 @@ interface UpdateBuyerIdentityInput {
|
|
|
502
499
|
/** Cart ID (optional, will use cookie) */
|
|
503
500
|
cartId?: string;
|
|
504
501
|
/** Buyer identity information */
|
|
505
|
-
buyerIdentity:
|
|
506
|
-
/** The email address of the buyer */
|
|
507
|
-
email?: string;
|
|
508
|
-
/** The phone number of the buyer */
|
|
509
|
-
phone?: string;
|
|
510
|
-
/** The country where the buyer is located */
|
|
511
|
-
countryCode?: string;
|
|
512
|
-
/** The access token used to identify the customer */
|
|
513
|
-
customerAccessToken?: string;
|
|
514
|
-
/** The company location of the buyer */
|
|
515
|
-
companyLocationId?: string;
|
|
516
|
-
};
|
|
502
|
+
buyerIdentity: BuyerIdentityInput;
|
|
517
503
|
}
|
|
518
504
|
interface UseUpdateBuyerIdentityOptions {
|
|
519
505
|
/** Function to update cart state */
|
|
@@ -571,10 +557,7 @@ interface BuyNowInput {
|
|
|
571
557
|
value: string;
|
|
572
558
|
}>;
|
|
573
559
|
/** Buyer identity */
|
|
574
|
-
buyerIdentity?:
|
|
575
|
-
email?: string;
|
|
576
|
-
countryCode?: string;
|
|
577
|
-
};
|
|
560
|
+
buyerIdentity?: BuyerIdentityInput;
|
|
578
561
|
/** GTM tracking parameters */
|
|
579
562
|
gtmParams?: Omit<GtmParams, 'brand'>;
|
|
580
563
|
/** Facebook Pixel tracking configuration */
|
package/dist/hooks/index.js
CHANGED
|
@@ -671,6 +671,177 @@ var useScriptAutoFreeGift = ({
|
|
|
671
671
|
giftProductsResult: finalGiftProductsResult
|
|
672
672
|
};
|
|
673
673
|
};
|
|
674
|
+
function useRemoveCartLines(options) {
|
|
675
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
676
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
677
|
+
const removeLines = react.useCallback(
|
|
678
|
+
async (_key, { arg }) => {
|
|
679
|
+
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
680
|
+
let updatedCart = await shopifySdk.removeCartLines(client, {
|
|
681
|
+
cartId,
|
|
682
|
+
lineIds,
|
|
683
|
+
metafieldIdentifiers,
|
|
684
|
+
cookieAdapter: cartCookieAdapter
|
|
685
|
+
});
|
|
686
|
+
if (updatedCart && autoRemoveInvalidCodes) {
|
|
687
|
+
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
688
|
+
if (unApplicableCodes.length > 0) {
|
|
689
|
+
if (onCodesRemoved) {
|
|
690
|
+
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
691
|
+
if (handledCart) {
|
|
692
|
+
updatedCart = handledCart;
|
|
693
|
+
}
|
|
694
|
+
} else {
|
|
695
|
+
updatedCart = await shopifySdk.updateCartCodes(client, {
|
|
696
|
+
cartId: updatedCart.id,
|
|
697
|
+
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
698
|
+
metafieldIdentifiers,
|
|
699
|
+
cookieAdapter: cartCookieAdapter
|
|
700
|
+
}) || updatedCart;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
if (updatedCart) {
|
|
705
|
+
mutateCart(updatedCart);
|
|
706
|
+
}
|
|
707
|
+
return updatedCart;
|
|
708
|
+
},
|
|
709
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
710
|
+
);
|
|
711
|
+
return useSWRMutation__default.default("remove-cart-lines", removeLines, options);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// src/hooks/cart/feature/use-auto-remove-free-gifts.ts
|
|
715
|
+
function useAutoRemoveFreeGifts(options = {}) {
|
|
716
|
+
const {
|
|
717
|
+
removeFunctionGifts = true,
|
|
718
|
+
removeScriptGifts = true,
|
|
719
|
+
isGiftLineItem,
|
|
720
|
+
runOnlyOnceAfterInit = false,
|
|
721
|
+
initDelay = 500
|
|
722
|
+
} = options;
|
|
723
|
+
const [isRemoving, setIsRemoving] = react.useState(false);
|
|
724
|
+
const [isInitialized, setIsInitialized] = react.useState(!runOnlyOnceAfterInit);
|
|
725
|
+
const [isFinished, setIsFinished] = react.useState(false);
|
|
726
|
+
const { cart } = useCartContext();
|
|
727
|
+
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
728
|
+
const giftsToRemove = react.useMemo(() => {
|
|
729
|
+
if (!cart?.lineItems) {
|
|
730
|
+
return [];
|
|
731
|
+
}
|
|
732
|
+
return cart.lineItems.filter((item) => {
|
|
733
|
+
if (removeFunctionGifts) {
|
|
734
|
+
const functionAttr = item.customAttributes?.find(
|
|
735
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
736
|
+
)?.value;
|
|
737
|
+
if (functionAttr) {
|
|
738
|
+
try {
|
|
739
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
740
|
+
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
741
|
+
return true;
|
|
742
|
+
}
|
|
743
|
+
} catch (error) {
|
|
744
|
+
console.error("Failed to parse _discounts_function_env:", error);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if (removeScriptGifts) {
|
|
749
|
+
const scriptGiftAttr = item.customAttributes?.find(
|
|
750
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
751
|
+
);
|
|
752
|
+
if (scriptGiftAttr) {
|
|
753
|
+
return true;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
757
|
+
return true;
|
|
758
|
+
}
|
|
759
|
+
return false;
|
|
760
|
+
});
|
|
761
|
+
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
762
|
+
ahooks.useDebounceEffect(
|
|
763
|
+
() => {
|
|
764
|
+
if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (!cart?.lineItems?.length) {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
setIsInitialized(true);
|
|
771
|
+
if (giftsToRemove.length === 0) {
|
|
772
|
+
setIsFinished(true);
|
|
773
|
+
}
|
|
774
|
+
},
|
|
775
|
+
[runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
|
|
776
|
+
{
|
|
777
|
+
trailing: true,
|
|
778
|
+
wait: initDelay
|
|
779
|
+
}
|
|
780
|
+
);
|
|
781
|
+
react.useEffect(() => {
|
|
782
|
+
if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
if (isRemoving || giftsToRemove.length === 0) {
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
const performRemoval = async () => {
|
|
789
|
+
setIsRemoving(true);
|
|
790
|
+
try {
|
|
791
|
+
await removeCartLines2({
|
|
792
|
+
lineIds: giftsToRemove.map((item) => item.id)
|
|
793
|
+
});
|
|
794
|
+
} catch (error) {
|
|
795
|
+
console.error("Failed to remove free gifts:", error);
|
|
796
|
+
} finally {
|
|
797
|
+
setIsRemoving(false);
|
|
798
|
+
if (runOnlyOnceAfterInit) {
|
|
799
|
+
setIsFinished(true);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
performRemoval();
|
|
804
|
+
}, [
|
|
805
|
+
runOnlyOnceAfterInit,
|
|
806
|
+
isInitialized,
|
|
807
|
+
isFinished,
|
|
808
|
+
isRemoving,
|
|
809
|
+
giftsToRemove,
|
|
810
|
+
removeCartLines2
|
|
811
|
+
]);
|
|
812
|
+
return {
|
|
813
|
+
isRemoving
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
function isFunctionGift(line) {
|
|
817
|
+
const functionAttr = line.customAttributes?.find(
|
|
818
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
819
|
+
)?.value;
|
|
820
|
+
if (!functionAttr) {
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
try {
|
|
824
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
825
|
+
return Boolean(
|
|
826
|
+
functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
|
|
827
|
+
);
|
|
828
|
+
} catch {
|
|
829
|
+
return false;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
function isScriptGift(line) {
|
|
833
|
+
return line.customAttributes?.some(
|
|
834
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
835
|
+
) ?? false;
|
|
836
|
+
}
|
|
837
|
+
function isBuyGetGift(line) {
|
|
838
|
+
return line.customAttributes?.some(
|
|
839
|
+
(attr) => attr.key === "_freegift_related_handlesku"
|
|
840
|
+
) ?? false;
|
|
841
|
+
}
|
|
842
|
+
function isAnyGift(line) {
|
|
843
|
+
return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
|
|
844
|
+
}
|
|
674
845
|
var CartContext = react.createContext(null);
|
|
675
846
|
function useCartContext(options) {
|
|
676
847
|
const context = react.useContext(CartContext);
|
|
@@ -1101,145 +1272,6 @@ var getLinesWithAttributes = ({
|
|
|
1101
1272
|
return functionLine;
|
|
1102
1273
|
});
|
|
1103
1274
|
};
|
|
1104
|
-
function useRemoveCartLines(options) {
|
|
1105
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1106
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1107
|
-
const removeLines = react.useCallback(
|
|
1108
|
-
async (_key, { arg }) => {
|
|
1109
|
-
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
1110
|
-
let updatedCart = await shopifySdk.removeCartLines(client, {
|
|
1111
|
-
cartId,
|
|
1112
|
-
lineIds,
|
|
1113
|
-
metafieldIdentifiers,
|
|
1114
|
-
cookieAdapter: cartCookieAdapter
|
|
1115
|
-
});
|
|
1116
|
-
if (updatedCart && autoRemoveInvalidCodes) {
|
|
1117
|
-
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
1118
|
-
if (unApplicableCodes.length > 0) {
|
|
1119
|
-
if (onCodesRemoved) {
|
|
1120
|
-
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
1121
|
-
if (handledCart) {
|
|
1122
|
-
updatedCart = handledCart;
|
|
1123
|
-
}
|
|
1124
|
-
} else {
|
|
1125
|
-
updatedCart = await shopifySdk.updateCartCodes(client, {
|
|
1126
|
-
cartId: updatedCart.id,
|
|
1127
|
-
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
1128
|
-
metafieldIdentifiers,
|
|
1129
|
-
cookieAdapter: cartCookieAdapter
|
|
1130
|
-
}) || updatedCart;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
}
|
|
1134
|
-
if (updatedCart) {
|
|
1135
|
-
mutateCart(updatedCart);
|
|
1136
|
-
}
|
|
1137
|
-
return updatedCart;
|
|
1138
|
-
},
|
|
1139
|
-
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1140
|
-
);
|
|
1141
|
-
return useSWRMutation__default.default("remove-cart-lines", removeLines, options);
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
// src/hooks/cart/feature/use-auto-remove-free-gifts.ts
|
|
1145
|
-
function useAutoRemoveFreeGifts(options = {}) {
|
|
1146
|
-
const {
|
|
1147
|
-
removeFunctionGifts = true,
|
|
1148
|
-
removeScriptGifts = true,
|
|
1149
|
-
isGiftLineItem
|
|
1150
|
-
} = options;
|
|
1151
|
-
const [isRemoving, setIsRemoving] = react.useState(false);
|
|
1152
|
-
const { cart } = useCartContext();
|
|
1153
|
-
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
1154
|
-
const giftsToRemove = react.useMemo(() => {
|
|
1155
|
-
if (!cart?.lineItems) {
|
|
1156
|
-
return [];
|
|
1157
|
-
}
|
|
1158
|
-
return cart.lineItems.filter((item) => {
|
|
1159
|
-
if (removeFunctionGifts) {
|
|
1160
|
-
const functionAttr = item.customAttributes?.find(
|
|
1161
|
-
(attr) => attr.key === "_discounts_function_env"
|
|
1162
|
-
)?.value;
|
|
1163
|
-
if (functionAttr) {
|
|
1164
|
-
try {
|
|
1165
|
-
const functionAttrObj = JSON.parse(functionAttr);
|
|
1166
|
-
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
1167
|
-
return true;
|
|
1168
|
-
}
|
|
1169
|
-
} catch (error) {
|
|
1170
|
-
console.error("Failed to parse _discounts_function_env:", error);
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
if (removeScriptGifts) {
|
|
1175
|
-
const scriptGiftAttr = item.customAttributes?.find(
|
|
1176
|
-
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
1177
|
-
);
|
|
1178
|
-
if (scriptGiftAttr) {
|
|
1179
|
-
return true;
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
1183
|
-
return true;
|
|
1184
|
-
}
|
|
1185
|
-
return false;
|
|
1186
|
-
});
|
|
1187
|
-
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
1188
|
-
react.useEffect(() => {
|
|
1189
|
-
if (isRemoving || giftsToRemove.length === 0) {
|
|
1190
|
-
return;
|
|
1191
|
-
}
|
|
1192
|
-
const performRemoval = async () => {
|
|
1193
|
-
setIsRemoving(true);
|
|
1194
|
-
try {
|
|
1195
|
-
await removeCartLines2({
|
|
1196
|
-
lineIds: giftsToRemove.map((item) => item.id)
|
|
1197
|
-
});
|
|
1198
|
-
} catch (error) {
|
|
1199
|
-
console.error("Failed to remove free gifts:", error);
|
|
1200
|
-
} finally {
|
|
1201
|
-
setIsRemoving(false);
|
|
1202
|
-
}
|
|
1203
|
-
};
|
|
1204
|
-
performRemoval();
|
|
1205
|
-
}, [
|
|
1206
|
-
isRemoving,
|
|
1207
|
-
giftsToRemove,
|
|
1208
|
-
removeCartLines2
|
|
1209
|
-
]);
|
|
1210
|
-
return {
|
|
1211
|
-
isRemoving
|
|
1212
|
-
};
|
|
1213
|
-
}
|
|
1214
|
-
function isFunctionGift(line) {
|
|
1215
|
-
const functionAttr = line.customAttributes?.find(
|
|
1216
|
-
(attr) => attr.key === "_discounts_function_env"
|
|
1217
|
-
)?.value;
|
|
1218
|
-
if (!functionAttr) {
|
|
1219
|
-
return false;
|
|
1220
|
-
}
|
|
1221
|
-
try {
|
|
1222
|
-
const functionAttrObj = JSON.parse(functionAttr);
|
|
1223
|
-
return Boolean(
|
|
1224
|
-
functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
|
|
1225
|
-
);
|
|
1226
|
-
} catch {
|
|
1227
|
-
return false;
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
function isScriptGift(line) {
|
|
1231
|
-
return line.customAttributes?.some(
|
|
1232
|
-
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
1233
|
-
) ?? false;
|
|
1234
|
-
}
|
|
1235
|
-
function isBuyGetGift(line) {
|
|
1236
|
-
return line.customAttributes?.some(
|
|
1237
|
-
(attr) => attr.key === "_freegift_related_handlesku"
|
|
1238
|
-
) ?? false;
|
|
1239
|
-
}
|
|
1240
|
-
function isAnyGift(line) {
|
|
1241
|
-
return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
|
|
1242
|
-
}
|
|
1243
1275
|
function useCalcGiftsFromLines({
|
|
1244
1276
|
lines,
|
|
1245
1277
|
customer,
|