Skip to content
r.message);
});
}
function refreshShopConfig(source) {
const cfg = window._RestockRocketConfig;
const viaEmbed = function() { return fetchEmbedConfig(source.endpoint, source.apply); };
const flagOn = !!cfg.readFlags && cfg.readFlags[source.flag] === true;
if (!flagOn) { return viaEmbed(); }
return fetchStorefrontShopMetafield(source.metafieldKey)
.then(function(value) {
if (!source.isValid(value)) { throw new Error(source.metafieldKey + ' metafield is not a valid ' + source.endpoint + ' payload'); }
source.apply(value);
console.debug('STOQ - refreshed ' + source.endpoint + ' from the Storefront API');
})
.catch(function(error) {
console.debug('STOQ - Storefront ' + source.endpoint + ' read failed, using embed:', error.message);
return viaEmbed();
});
}
function refreshIntegrations() {
const cfg = window._RestockRocketConfig;
return refreshShopConfig({
endpoint: 'integrations',
metafieldKey: 'in
tegrations',
flag: 'enable_storefront_integrations_read',
isValid: function(value) { return Array.isArray(value); },
apply: function(data) {
cfg.integrations = data;
}
});
}
function refreshSellingPlans() {
const cfg = window._RestockRocketConfig;
return refreshShopConfig({
endpoint: 'selling_plans',
metafieldKey: 'cachedSellingPlans',
flag: 'enable_storefront_selling_plans_read',
isValid: function(value) { return !!value && Array.isArray(value.plans); },
apply: function(data) {
cfg.sellingPlans = typeof stoqKeepPlanLocale === 'function' && !(data.read_flags && data.read_flags.full_locale_translations === true) ? stoqKeepPlanLocale(data.plans) : data.plans;
cfg.disabledSellingPlanIds = data.disabled_plan_ids || [];
if (data && data.read_flags && typeof data.read_flags === 'object' && !Array.isArray(data.read_flags)) {
cfg.readFlags = data.read_flags;
}
}
});
}
function initial
izeScripts(settings) {
settings = applyTranslations(settings);
window._RestockRocketConfig.settings = settings;
console.debug(`STOQ - settings configured for ${window._RestockRocketConfig.pageType}`);
const staleRefresh = !window._RestockRocketConfig.isLiquidCacheFresh && !settings.disable_refresh_on_stale_liquid;
if (staleRefresh) {
console.debug('STOQ - refreshing selling_plans + integrations (Liquid cache stale)');
Promise.race([
Promise.all([refreshSellingPlans(), refreshIntegrations()]),
new Promise(function(resolve) { setTimeout(resolve, 1000); })
]).then(function() { loadScripts(settings); });
return;
}
loadScripts(settings);
}
function loadScripts(settings) {
setupCartSellingPlanUpdater(settings);
if(settings.enable_app) {
const hijackIntegration = window._RestockRocketConfig.integrations.find(function(integration) {
return integration.type === 'hijack' && integration.enabled && integration.page_types.incl
udes(window._RestockRocketConfig.pageType);
})
const hasEnabledOffer = Array.isArray(window._RestockRocketConfig.sellingPlans)
&& window._RestockRocketConfig.sellingPlans.some(function(plan) { return plan && plan.enabled; });
const hasDisabledPlanIds = Array.isArray(window._RestockRocketConfig.disabledSellingPlanIds)
&& window._RestockRocketConfig.disabledSellingPlanIds.length > 0;
const usePreorderBuild = settings.preorder_enabled || hasEnabledOffer || hasDisabledPlanIds;
const pageType = window._RestockRocketConfig.pageType;
const preorderUiOnPage = (settings.preorder_enabled && settings[`preorder_${pageType}_enabled`])
|| window._RestockRocketConfig.integrations.some(function(integration) {
return (integration.type === 'quick_buy' || integration.type === 'featured_product')
&& integration.enabled && integration.page_types.includes(pageType);
})
|| /\b(renderPreorderButton|updateAddToCartButton|updateAddToCartForms|setupAddToCartForm|teardownAddToCartForm|addDiscountedPrice|removeDiscountedPrice)\b/.test(settings.js_config || '');
const preorderCollectionScriptUrl = preorderUiOnPage
? window._RestockRocketConfig.scriptUrlCollectionFull
: window._RestockRocketConfig.scriptUrlCollection;
const collectionScriptUrl = usePreorderBuild
? preorderCollectionScriptUrl
: window._RestockRocketConfig.scriptUrlCollectionBis;
const productScriptUrl = usePreorderBuild
? window._RestockRocketConfig.scriptUrlProduct
: window._RestockRocketConfig.scriptUrlProductBis;
const collectionPageTypes = ['collection', 'index', 'search', 'page'];
if(collectionPageTypes.indexOf(pageType) !== -1 && (settings[`show_button_on_${pageType}`] || settings[`preorder_${pageType}_enabled`])) {
createRestockRocketScript(collectionScriptUrl);
} else if(pageType === 'product') {
createRestockRocketScript(productScriptUrl);
} else if(hijackIntegratio
n) {
createRestockRocketScript(preorderCollectionScriptUrl);
} else if(usePreorderBuild) {
createRestockRocketScript(preorderCollectionScriptUrl);
} else {
console.debug(`STOQ - no scripts enabled for ${pageType}`);
}
const wishlistIntegration = window._RestockRocketConfig.integrations.find(function(integration) { return integration.type === 'wishlist' && integration.enabled; });
if (wishlistIntegration) {
createRestockRocketScript(window._RestockRocketConfig.scriptHost + 'restockrocket-wishlist.js');
if ((wishlistIntegration.page_types || []).indexOf(pageType) !== -1) {
let wishlistHeartsRequested = false;
const wishlistHeartTarget = pageType === 'product' ? '[data-stoq-wishlist-heart]' : '[data-stoq-wishlist-heart], a[href*="/products/"]';
// A product page's heart places itself on the product image unless the merchant turned that off.
const wishlistImageHeart = pageType === 'product' && (wishlistIn