Created
March 5, 2025 20:35
-
-
Save itailulu/7367ca2673e5a4055f1054e74b8b77f5 to your computer and use it in GitHub Desktop.
Underoutfit TryNow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface TryNowProps { | |
selectedVariant: SelectedVariant; | |
quantity?: number; | |
className?: string; | |
sellingPlanId?: SellingPlan['id']; | |
isPdp: boolean; | |
} | |
export class TryNowSDK { | |
async initTryNow({ | |
myshopifyDomain, | |
storefrontAccessToken, | |
cartId, | |
}: { | |
myshopifyDomain: string; | |
storefrontAccessToken?: string; | |
cartId?: string; | |
}) { | |
if (!window?.trynow) return; | |
console.log('initTryNow', { | |
myshopifyDomain, | |
storefrontAccessToken, | |
cartId, | |
}) | |
await window.trynow.initialize({ | |
myshopifyDomain, | |
storefrontAccessToken, | |
cartId, | |
// debug: true, | |
}); | |
} | |
async getTryNowSellingPlanId(): Promise<undefined | string> { | |
if (!window.trynow) return undefined; | |
return await window.trynow.getSellingPlanId(); | |
} | |
} | |
const tryNowSDK = new TryNowSDK(); | |
const initTryNow = async ({ | |
storeDomain, | |
storefrontApiToken, | |
cartId, | |
}: { | |
storeDomain: string; | |
storefrontApiToken?: string; | |
cartId?: string; | |
}) => { | |
await tryNowSDK.initTryNow({ | |
myshopifyDomain: storeDomain, | |
storefrontAccessToken: storefrontApiToken, | |
cartId, | |
}); | |
const planId = await tryNowSDK.getTryNowSellingPlanId(); | |
setTryNowSellingPlanId(planId); | |
}; | |
useEffect(() => { | |
if (scriptStatus !== 'done') return; | |
if (!window.ENV?.PUBLIC_STORE_DOMAIN) return; | |
if (!window.ENV?.PUBLIC_STOREFRONT_API_TOKEN) return; | |
if (!cart.id) return; | |
initTryNow({ | |
storeDomain: window.ENV.PUBLIC_STORE_DOMAIN, | |
storefrontApiToken: window.ENV.PUBLIC_STOREFRONT_API_TOKEN, | |
cartId: cart.id, | |
}).catch((error) => { | |
console.error('InitTryNow', error); | |
}); | |
}, [scriptStatus, cart.id]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment