var appstleInit = function () { var head = document.getElementsByTagName('head')[0]; var startingTime = new Date().getTime(); window.appstleLoadScript = function(url, type, callback) { var script = document.createElement('script'); script.type = type || 'text/javascript'; if (script.readyState) { script.onreadystatechange = function() { if (script.readyState == 'loaded' || script.readyState == 'complete') { script.onreadystatechange = null; if (callback) { callback(); } } }; } else { script.onload = function() { if (callback) { callback(); } }; } script.src = url; head.appendChild(script); }; if (!RS?.Config?.disableLoadingJquery) { // var jQueryScript = document.createElement('script'); // jQueryScript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'; // jQueryScript.type = 'text/javascript'; // head.appendChild(jQueryScript); appstleLoadScript('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', 'text/javascript', function() { window['appstle_jQuery'] = jQuery.noConflict(true); }); } appstleLoadScript('https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js', 'module') if (typeof Mustache == 'undefined') { var mustacheScript = document.createElement('script'); mustacheScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.1.0/mustache.js'; mustacheScript.type = 'text/javascript'; head.appendChild(mustacheScript); } // Poll for jQuery to come into existance var checkReady = function (callback) { if ((RS?.Config?.disableLoadingJquery ? window.jQuery : window.appstle_jQuery) && window.Mustache && window.Shopify) { callback((RS?.Config?.disableLoadingJquery ? window.jQuery : window.appstle_jQuery)); } else { window.setTimeout(function () { checkReady(callback); }, 20); } }; const urlParams = new URLSearchParams(window.location.search); var globalUrlParameter = urlParams.get('variant'); // Start polling... checkReady(function ($) { var jQuery = $; $(function () { var endingTime = new Date().getTime(); var tookTime = endingTime - startingTime; console.log("jQuery is loaded, after " + tookTime + " milliseconds!"); RS.Config = Object.assign(RS.Config, "undefined" != typeof _RSConfig && null !== _RSConfig ? _RSConfig : {}) window.RSConfig = RS.Config; window.getCurrentSellingPlanId = function() { return jQuery('[name=selling_plan]:checked').val(); } function renderWidget(standAloneProduct, standAloneElement, widgetId) { if(!window["Shopify"]) { return; } var product = RSConfig.product; if (standAloneProduct) { product = standAloneProduct; } processProductVariants(product); var localVariantsByTitle = RSConfig.variantsByTitle; var localVariantsById = RSConfig.variantsById; var localWindowVariant; function processProductVariants(product) { if (!product) { return; } var variants = product.variants; var _variantsByTitle = {}; var _variantsById = {}; for (var index = 0; index < variants.length; index++) { var variant = variants[index]; _variantsByTitle[variant.title] = Object.assign({}, variant); _variantsById[variant.id] = Object.assign({}, variant); } RSConfig.variantsByTitle = _variantsByTitle; RSConfig.variantsById = _variantsById; } RSConfig.showPrepaidPlansSeparately = (RSConfig.widgetType === "WIDGET_TYPE_7") ? true : false; var widgetLabels = JSON.parse(RS.Config.labels) var Selling_Plan_Variants_Global = {} function detectVariant(previousVariantId, selector, product) { var variantId = urlParam("variant"); if (variantId && RSConfig?.detectVariantFromURLParams) { return localVariantsById[variantId]; } var closestCartSelectors = selector.closest('form[action$="/cart/add"]').find('[name=id]'); if (closestCartSelectors.length > 0) { for (var i = 0; i < closestCartSelectors.length; i++) { if (closestCartSelectors[i].value && jQuery(closestCartSelectors[i]).is(":checked")) { variantId = closestCartSelectors[i].value; return localVariantsById[variantId]; } } } if (selector.closest('form[action$="/cart/add"]').find('[name=id]').length > 0 && selector.closest('form[action$="/cart/add"]').find('[name=id]').val()) { variantId = selector.closest('form[action$="/cart/add"]').find('[name=id]').val(); return localVariantsById[variantId]; } else if ((RSConfig?.widgetParentSelector ? (selector.closest(RSConfig?.widgetParentSelector).find('[name=id]').length > 0 && selector.closest(RSConfig?.widgetParentSelector).find('[name=id]').val()) : undefined) || (selector.parents('html').find('form[action$="/cart/add"]').find('[name=id]').length && selector.parents('html').find('form[action$="/cart/add"]').find('[name=id]')[0]?.value) ) { variantId = selector.closest(RSConfig?.widgetParentSelector).find('[name=id]').val() || selector.parents('html').find('form[action$="/cart/add"]').find('[name=id]')[0]?.value; return localVariantsById[variantId]; } else if (variantId) { return localVariantsById[variantId]; } else if (Object.values(localVariantsById).some(data => data?.title == $("form select[name='options[Bundle]']")?.val())) { return Object.values(localVariantsById).find(data => data?.title == $("select[name='options[Bundle]']").val()) } else { var titleTokens; titleTokens = function () { var variantTokens = []; var singleOptionSelector = selector.closest('form[action$="/cart/add"]').find("select.single-option-selector"); if (singleOptionSelector != null && singleOptionSelector.selectedIndex != null && singleOptionSelector.selectedIndex !== -1) { variantTokens.push(singleOptionSelector[singleOptionSelector.selectedIndex].value) } else { variantTokens.push(void 0); } return variantTokens }(); var variant = localVariantsByTitle[titleTokens.join(" / ")] if (variant) { return variant; } else { var variant = product.variants[0]; return variant; } } } function getSellingPlanAllocation(variantId, sellingPlanId) { var variant = localVariantsById[variantId]; return variant.selling_plan_allocations.find(function (plan) { return plan.selling_plan_id === sellingPlanId; }); } function compareCustomerTags(array1, array2) { return array1.filter(function (n) { return array2.indexOf(n) != -1; }); } function isSellingPlanVisible(sellingPlanId) { var customerId = RSConfig?.customerId; var userTags = RSConfig.customer_tags || []; var isVisible = true; if (!customerId && RSConfig.memberOnlySellingPlansJson && RSConfig.memberOnlySellingPlansJson[sellingPlanId] && RSConfig.memberOnlySellingPlansJson[sellingPlanId]['enableMemberInclusiveTag']) { isVisible = false; } if (customerId && RSConfig.nonMemberOnlySellingPlansJson && RSConfig.nonMemberOnlySellingPlansJson[sellingPlanId]) { isVisible = false; } if (isVisible && customerId && RSConfig.memberOnlySellingPlansJson && RSConfig.memberOnlySellingPlansJson[sellingPlanId]) { if (RSConfig.memberOnlySellingPlansJson[sellingPlanId].memberInclusiveTags && RSConfig.memberOnlySellingPlansJson[sellingPlanId].memberInclusiveTags.trim()) { var sellingPlanTags = RSConfig.memberOnlySellingPlansJson[sellingPlanId].memberInclusiveTags.split(","); var tagFound = compareCustomerTags(userTags, sellingPlanTags); isVisible = (tagFound.length > 0); } if (RSConfig.memberOnlySellingPlansJson[sellingPlanId].memberExclusiveTags && RSConfig.memberOnlySellingPlansJson[sellingPlanId].memberExclusiveTags.trim()) { var sellingPlanExclusiveTags = RSConfig.memberOnlySellingPlansJson[sellingPlanId].memberExclusiveTags.split(","); var exclusiveTagFound = compareCustomerTags(userTags, sellingPlanExclusiveTags); isVisible = !(exclusiveTagFound.length > 0); } } return isVisible; } function buildSellingPlantText(sellingPlan) { var sellingPlanModel = sellingPlan.totalPrice == sellingPlan.formattedPrice ? { sellingPlanName: sellingPlan.name, sellingPlanPrice: `${sellingPlan.formattedPrice}`, secondSellingPlanPrice: `${sellingPlan.secondFormattedPrice}`, discountText: sellingPlan?.discountText, totalPrice: `${sellingPlan?.totalPrice}`, } : { sellingPlanName: sellingPlan.name, totalPrice: `${sellingPlan?.totalPrice}`, sellingPlanPrice: `${sellingPlan.formattedPrice}`, secondSellingPlanPrice: `${sellingPlan.secondFormattedPrice}`, discountText: sellingPlan.discountText }; var sellingPlanDisplayText = Mustache.render(RS.Config.sellingPlanTitleText, sellingPlanModel); return wrapPriceWithSpanTag(sellingPlanDisplayText); } function buildAtcButtonSelector() { var atcButtonSelector = RSConfig.atcButtonSelector || "form[action$='/cart/add'] [type='submit']" var atcButtonSelectorModel = { productId: product.id, }; atcButtonSelector = Mustache.render(atcButtonSelector, atcButtonSelectorModel); return atcButtonSelector; } function buildOneTimePriceText(price) { var priceModel = { price: price, }; var priceText = Mustache.render(RS.Config.oneTimePriceText, priceModel); return wrapPriceWithSpanTag(priceText); } function buildSubscriptionOptionText(isPrepaidPlan, discountValue, frequency, formattedSelectedSellingPlanPrice) { let subscriptionOptionModel = { discountValue: discountValue, frequency: frequency, price: formattedSelectedSellingPlanPrice }; let subscriptionOptionText = Mustache.render(RS.Config.subscriptionOptionText, subscriptionOptionModel); return wrapPriceWithSpanTag(subscriptionOptionText); } function buildSelectedPriceText(price, isPrePaid, totalPrice) { var priceModel = isPrePaid ? { pricePerDelivery: price, totalPrice: totalPrice } : { price: price, }; var text = isPrePaid ? RS.Config.selectedPrepaidSellingPlanPriceText : RS.Config.selectedPayAsYouGoSellingPlanPriceText; var priceText = Mustache.render(text, priceModel); return wrapPriceWithSpanTag(priceText); } function buildPrepaidPerDeliveryPriceText(price) { var priceModel = { prepaidPerDeliveryPrice: price, } return wrapPriceWithSpanTag(Mustache.render(RS.Config.totalPricePerDeliveryText, priceModel)); } function buildSelectedTooltipPrePaidText(price, totalPrice) { var prepaidPricetooltipModel = { pricePerDelivery: price, totalPrice: totalPrice } var text = RS.Config.tooltipDescriptionOnPrepaidPlan; var prepaidPricetooltipText = Mustache.render(text, prepaidPricetooltipModel); return wrapPriceWithSpanTag(prepaidPricetooltipText); } function buildSelectedTooltipDiscountText(selectedSellingPlanAllocationObj, multipleDiscount) { if (selectedSellingPlanAllocationObj?.price_adjustments?.length == 2) { var discountModel = { firstPrice: formatPriceWithQuantity(selectedSellingPlanAllocationObj?.price_adjustments[0]?.price), secondPrice: formatPriceWithQuantity(selectedSellingPlanAllocationObj?.price_adjustments[1]?.price), discountOne: multipleDiscount?.length > 0 ? multipleDiscount[0] : "", discountTwo: multipleDiscount?.length == 2 ? multipleDiscount[1] : "" }; return wrapPriceWithSpanTag(Mustache.render(RS.Config.tooltipDescriptionOnMultipleDiscount, discountModel)); } } function buildTooltipDetailsText(prepaidText, isPrePaid, discountText) { var tooltipDetailModel = isPrePaid ? { prepaidDetails: prepaidText, discountDetails: discountText, defaultTooltipDescription: RS.Config.tooltipDesctiption } : { defaultTooltipDescription: RS.Config.tooltipDesctiption, discountDetails: discountText }; // let text = RS.Config.tooltipDesctiption + (isPrePaid ? "
{{prepaidDetails}}" : "") + (discountText!= undefined ? "
{{discountDetails}}": ""); let text = RS.Config.tooltipDescriptionCustomization; var tooltipText = Mustache.render(text, tooltipDetailModel); return wrapPriceWithSpanTag(tooltipText); } function populateDropdown(purchaseOptions, variant, isPrepaidDropdown) { var sellingPlanVariants = []; let validSellingPlanCounter = 0; jQuery.each(product.selling_plan_groups, function (index, sellingPlanGroup) { if (sellingPlanGroup.app_id === 'appstle') { jQuery.each(sellingPlanGroup.selling_plans, function (subIndex, sellingPlan) { var visible = isSellingPlanVisible(sellingPlan.id); if (visible) { var sellingPlanAllocation = getSellingPlanAllocation(variant.id, sellingPlan.id); if (!sellingPlanAllocation) { return } else { validSellingPlanCounter++; } var price = sellingPlanAllocation.per_delivery_price; var totalPrice = formatPriceWithQuantity(sellingPlanAllocation?.price); var formattedPrice = formatPriceWithQuantity(price); var compareAtPrice = sellingPlanAllocation.compare_at_price; var formattedCompareAtPrice = formatPriceWithQuantity(compareAtPrice); var secondPrice = null; var secondFormattedPrice = null; if (sellingPlanAllocation && sellingPlanAllocation.price_adjustments && sellingPlanAllocation.price_adjustments.length === 2) { secondPrice = sellingPlanAllocation.price_adjustments[1].price; secondFormattedPrice = formatPriceWithQuantity(secondPrice); } else { secondPrice = price; secondFormattedPrice = formattedPrice; } let discountText; var priceAdjustment = sellingPlan?.price_adjustments[0]; if (priceAdjustment?.value_type !== 'percentage') { discountText = formatPriceWithQuantity(priceAdjustment?.value); } else { discountText = priceAdjustment?.value + '%'; } let jsonOfSellingPlans = RS?.Config?.sellingPlansJson if(jsonOfSellingPlans?.length) { var sellingPlanFrequency = jsonOfSellingPlans?.find(item => item?.id?.split('/')?.pop() == sellingPlan.id) var sellingPlanFrequencyText; if(sellingPlanFrequency?.frequencyInterval === "MONTH" && sellingPlanFrequency?.billingFrequencyCount > 1){ sellingPlanFrequencyText = sellingPlanFrequency?.billingFrequencyCount + ' ' + widgetLabels['appstle.subscription.wg.monthsFrequencyTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "MONTH" && sellingPlanFrequency?.billingFrequencyCount === 1){ sellingPlanFrequencyText = widgetLabels['appstle.subscription.wg.monthlyLabelTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "WEEK" && sellingPlanFrequency?.billingFrequencyCount > 1){ sellingPlanFrequencyText = sellingPlanFrequency?.billingFrequencyCount + ' ' + widgetLabels['appstle.subscription.wg.weeksFrequencyTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "WEEK" && sellingPlanFrequency?.billingFrequencyCount === 1){ sellingPlanFrequencyText = widgetLabels['appstle.subscription.wg.weeklyLabelTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "DAY" && sellingPlanFrequency?.billingFrequencyCount > 1){ sellingPlanFrequencyText = sellingPlanFrequency?.billingFrequencyCount + ' ' + widgetLabels['appstle.subscription.wg.daysFrequencyTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "DAY" && sellingPlanFrequency?.billingFrequencyCount === 1){ sellingPlanFrequencyText = widgetLabels['appstle.subscription.wg.dayFrequencyTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "YEAR" && sellingPlanFrequency?.billingFrequencyCount > 1){ sellingPlanFrequencyText = sellingPlanFrequency?.billingFrequencyCount + ' ' + widgetLabels['appstle.subscription.wg.yearsFrequencyTextV2'] } if(sellingPlanFrequency?.frequencyInterval === "YEAR" && sellingPlanFrequency?.billingFrequencyCount === 1){ sellingPlanFrequencyText = widgetLabels['appstle.subscription.wg.yearlyLabelTextV2'] } } var sellingPlanDetails = { "name": sellingPlan.name, "description": sellingPlan.description, "sellingPlanId": sellingPlan.id, "formattedPrice": formattedPrice, "compareAtPrice": compareAtPrice, "formattedCompareAtPrice": formattedCompareAtPrice, "price": price, "totalPrice": totalPrice, "secondPrice": secondPrice, "secondFormattedPrice": secondFormattedPrice, "sellingPlanFrequencyText": sellingPlanFrequencyText, "discountText": priceAdjustment?.value ? discountText : null, "formattedDiscountText": priceAdjustment?.value ? buildDiscountText(discountText) : '', "showFormattedDiscountText": priceAdjustment?.value ? true : false, "isChecked": (validSellingPlanCounter == 1 && (RSConfig?.subscriptionOptionSelectedByDefault || product.requires_selling_plan)) ? true : false, "formattedPrepaidPerDeliveryPriceText": buildPrepaidPerDeliveryPriceText(formattedPrice) } jQuery.extend(sellingPlanDetails, JSON.parse(JSON.stringify(getSellingPlanDetailsById(sellingPlan.id)))) sellingPlanDetails.id = sellingPlan.id; sellingPlanDetails["isFrequencySubsequent"] = sellingPlanDetails.frequencyCount > 1; sellingPlanDetails["frequencyIntervalLowerCase"] = sellingPlanDetails.frequencyInterval.toLowerCase(); if(sellingPlanDetails.discountText === null) { sellingPlanDetails["secondFormattedDiscountText"] = '' sellingPlanDetails["showSecondFormattedDiscountText"] = false; } else { sellingPlanDetails["secondFormattedDiscountText"] = discountText + ` ${widgetLabels["appstle.subscription.wg.offFrequencyTextV2"] || 'off'}`; sellingPlanDetails["showSecondFormattedDiscountText"] = true; } sellingPlanDetails.prepaidFlag = eval(sellingPlanDetails.prepaidFlag); var daysInBillingFrequency = 1; var totalDaysInBillingFrequency = 0; var sellingPlanFrequencyType = ""; if (sellingPlanDetails.billingFrequencyInterval === 'WEEK') { daysInBillingFrequency = 7; sellingPlanFrequencyType = widgetLabels['appstle.subscription.wg.weeklyLabelTextV2'] || "Weekly"; } else if (sellingPlanDetails.billingFrequencyInterval === 'MONTH') { daysInBillingFrequency = 30; sellingPlanFrequencyType = widgetLabels['appstle.subscription.wg.monthlyLabelTextV2'] || "Monthly"; } else if (sellingPlanDetails.billingFrequencyInterval === 'YEAR') { daysInBillingFrequency = 365; sellingPlanFrequencyType = widgetLabels['appstle.subscription.wg.yearlyLabelTextV2'] || "Yearly"; } if (sellingPlanDetails.prepaidFlag) { sellingPlanFrequencyType = widgetLabels['appstle.subscription.wg.prepayLabelTextV2'] || "Prepay"; } totalDaysInBillingFrequency = sellingPlanDetails.billingFrequencyCount * daysInBillingFrequency; sellingPlanDetails["pricePerDay"] = formatPrice((sellingPlanDetails.price * sellingPlanDetails.billingFrequencyCount) / totalDaysInBillingFrequency) + `/${widgetLabels['appstle.subscription.wg.dayFrequencyTextV2']}`; sellingPlanDetails["sellingPlanFrequencyType"] = sellingPlanFrequencyType; if (((sellingPlanDetails.price * sellingPlanDetails.billingFrequencyCount) / totalDaysInBillingFrequency) > 0) { sellingPlanDetails["showPricePerDay"] = true; } else { sellingPlanDetails["showPricePerDay"] = false; } sellingPlanVariants.push(sellingPlanDetails); } }); } }); if (sellingPlanVariants.length > 0) { if (sellingPlanVariants.length < 2) { Selling_Plan_Variants_Global.multipleSellingPlan = false jQuery(purchaseOptions).find('.appstle_subscribe_option').children().hide(); var singleSellingPlanDisplayText = buildSellingPlantText(sellingPlanVariants[0]); Selling_Plan_Variants_Global.singleSellingPlanDisplayText = singleSellingPlanDisplayText var planText = jQuery(`
${singleSellingPlanDisplayText}
`); planText.appendTo(purchaseOptions.find('.appstle_subscribe_option')) if (sellingPlanVariants[0]?.description && sellingPlanVariants[0]?.description.includes('{{sellingPlanName}}')) { jQuery( `
${sellingPlanVariants[0]?.description?.replace( '{{sellingPlanName}}', sellingPlanVariants[0]?.name )}
` ).appendTo(purchaseOptions.find('.appstle_subscribe_option')); } else { if (sellingPlanVariants[0]?.description) { jQuery(`
${sellingPlanVariants[0]?.description}
`).appendTo( purchaseOptions.find('.appstle_subscribe_option') ); } } } else { Selling_Plan_Variants_Global.multipleSellingPlan = true } sellingPlanVariants = sellingPlanVariants?.map(plan => { let s = RSConfig?.sellingPlansJson?.find(f => f.idNew == plan.idNew); plan.frequencySequence = s?.frequencySequence || 0; return plan; }); sellingPlanVariants.sort(function (sellingPlanA, sellingPlanB) { return sellingPlanA.frequencySequence - sellingPlanB.frequencySequence; }); if (!RSConfig?.sortByDefaultSequence) { sellingPlanVariants.sort(function (sellingPlanA, sellingPlanB) { return sellingPlanA.price - sellingPlanB.price; }) } if (RSConfig?.sortAlphabetically) { sellingPlanVariants?.sort(function(a,b){ return a?.name?.localeCompare(b.name); }) } if (RSConfig?.showPrepaidPlansSeparately && !isPrepaidDropdown) { sellingPlanVariants = sellingPlanVariants.filter(item => !item?.prepaidFlag) } if (RSConfig?.showPrepaidPlansSeparately && isPrepaidDropdown) { sellingPlanVariants = sellingPlanVariants.filter(item => item?.prepaidFlag) } jQuery(sellingPlanVariants).each(function (index, sellingPlan) { var sellingPlanDisplayText = buildSellingPlantText(sellingPlan); sellingPlan.sellingPlanDisplayText = sellingPlanDisplayText; if (!RSConfig?.switchRadioButtonWidget) { jQuery('