<
nitConfig.product.variants))
? _stoqInitConfig.product.variants.map(function(v) { return v.id })
: [];
const _stoqSelectedVariantId = _stoqInitConfig.selected_variant_id;
var _stoqPageSeq = 0;
try {
_stoqPageSeq = (parseInt(window.sessionStorage.getItem('_stoq_pls') || '0', 10) || 0) + 1;
window.sessionStorage.setItem('_stoq_pls', String(_stoqPageSeq));
} catch (e) { }
Shopify?.analytics?.publish?.('stoq_initialized', {
cart_token: _stoqInitConfig.cartToken || '',
page_url: window.location.href,
page_type: _stoqInitConfig.pageType || '',
shop_domain: _stoqInitConfig.shop || '',
market_id: _stoqInitConfig.marketId || '',
country_code: _stoqInitConfig.countryIsoCode || '',
detected_variant_ids: _stoqDetectedVariantIds,
selected_variant_id: _stoqSelectedVariantId || '',
page_load_seq: _stoqPageSeq,
liquid_rendered_at: _stoqInitConfig.liquidRenderedAt || 0,
app_version: _stoqInitConfig.appVersion || '',
.nslatedFields && typeof translatedFields === 'object') {
Object.keys(translatedFields).forEach(function(key) {
const value = translatedFields[key];
if (value !== null && value !== undefined && value !== '') {
settings[key] = value;
}
});
} else {
console.debug('STOQ - No translated fields found for locale:', normalizedLocale);
}
delete settings.translations;
return settings;
} catch (e) {
console.debug('STOQ - error applying translations:', e);
return settings;
}
}
function setupCartSellingPlanUpdater(settings) {
window.addEventListener('stoq:inventory-data-loaded', function(event) {
console.debug('STOQ - Inventory data loaded, updating cart selling plans');
if (window._RestockRocket && window._RestockRocket.updateCartSellingPlans) {
window._RestockRocket.updateCartSellingPlans()
.then(hasUpdates => {
if (hasUpdates) {
console.debug('STO
tus); }
return response.json();
})
.then(function(body) {
if (body && body.errors && body.errors.length) {
throw new Error('Storefront API errors: ' + body.errors.map(function(e) { return e && e.message; }).join(', '));
}
return body && body.data;
});
}
function fetchStorefrontShopMetafield(key) {
const cfg = window._RestockRocketConfig;
const query = 'query StoqShopMetafield($namespace: String!, $key: String!) { shop { metafield(namespace: $namespace, key: $key) { value } } }';
return storefrontGraphql(query, { namespace: cfg.metafieldNamespace, key: key })
.then(function(data) {
const value = data && data.shop && data.shop.metafield && data.shop.metafield.value;
if (!value) { throw new Error('Storefront API returned no ' + key + ' metafield value'); }
return JSON.parse(value);
});
}
function fetchStorefrontSettings() {
const query = 'query StoqSettings($namespace: String!) { shop { settings: metafield(namespace: $n
Powered by hedSettings);
} else {
console.debug('STOQ - fetching fresh settings');
const headers = {
'X-Shopify-Shop-Domain': window._RestockRocketConfig.shop || window.Shopify.shop,
'ngrok-skip-browser-warning': 'skip'
};
if (window.Shopify?.theme?.role === 'main') {
headers['X-Shopify-Theme-Schema-Name'] = window.Shopify.theme.schema_name;
headers['X-Shopify-Theme-Schema-Version'] = window.Shopify.theme.schema_version;
headers['X-Shopify-Theme-Store-Id'] = window.Shopify.theme.theme_store_id;
}
const storefrontRead = window._RestockRocketConfig.readFlags?.enable_storefront_settings_read === true
? Promise.race([
fetchStorefrontSettings(),
new Promise(function(_, reject) { setTimeout(function() { reject(new Error('timed out')); }, 1500); })
]).catch(function(error) {
console.debug('STOQ - Storefront settings read failed, using setting.json:', error.message);
})
: Promise.resolve();
storefrontRead
ShopifytConfig.shop || window.Shopify.shop,
'ngrok-skip-browser-warning': 'skip'
}
}
)
.then(function(response) {
if (!response.ok) throw new Error(`Failed to fetch ${endpoint}`);
return response.json();
})
.then(function(data) {
try {
apply(data);
} catch (applyError) {
console.error('STOQ - apply failed for ' + endpoint + ':', applyError);
throw applyError;
}
})
.catch(function(error) {
console.debug(`STOQ - using cached ${endpoint}:`, error.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 || source.embedOnly === true) { return viaEmbed(); }
return fetchStorefrontShopMetafield(source.metafieldKey)
.then(function(value) {
if (!source.isValid(value)) { th. flag: 'enable_storefront_selling_plans_read',
isValid: function(value) { return !!value && Array.isArray(value.plans); },
apply: function(data) {
cfg.sellingPlans = 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 initializeScripts(settings) {
settings = applyTranslations(settings);
window._RestockRocketConfig.settings = settings;
console.debug(`STOQ - settings configured for ${window._RestockRocketConfig.pageType}`);
const forceEmbedSellingPlans = settings.force_embed_selling_plans === true;
const staleRefresh = !window._RestockRocketConfig.isLiquidCacheFresh && !settings.disable_refresh_on_stale_liquid;
if (forceEmbedSellingPlans || staleRefresh) {
console.debug('STOQ - refreshing selling_plans' + (staleRefresh ? ' + integrations (L
y(window._RestockRocketConfig.disabledSellingPlanIds)
&& window._RestockRocketConfig.disabledSellingPlanIds.length > 0;
const usePreorderBuild = settings.preorder_enabled || hasEnabledOffer || hasDisabledPlanIds;
const collectionScriptUrl = usePreorderBuild
? window._RestockRocketConfig.scriptUrlCollection
: window._RestockRocketConfig.scriptUrlCollectionBis;
const productScriptUrl = usePreorderBuild
? window._RestockRocketConfig.scriptUrlProduct
: window._RestockRocketConfig.scriptUrlProductBis;
const pageType = window._RestockRocketConfig.pageType;
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(hijackInteg