Untitled
unknown
plain_text
22 days ago
2.9 kB
7
Indexable
<script>
(() => {
const addToCart = async (
/** @type {Parameters<typeof window.__ADORIC_BUNDLES__.overrides.addToCart>[0]} */ {
checkoutItems,
notAddedCheckoutItems,
requestConfig,
requestBody
}
) => {
if (!checkoutItems.length) return;
const variantGids = checkoutItems.map(({ id }) => `gid://shopify/ProductVariant/${id}`);
const gqlResponse = await fetch('/api/2026-04/graphql.json', {
method: 'POST',
body: JSON.stringify({
query: `
query getVariants($ids: [ID!]!) {
nodes(ids: $ids) {
... on ProductVariant {
id
product {
id
metafield(
namespace: "custom",
key: "item_postion_number_in_warhouse"
) {
value
}
}
}
}
}
`,
variables: { ids: variantGids }
}),
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': window.__ADORIC_BUNDLES__.constants.TOKEN,
'X-Adoric-Request': 'true'
}
});
const { data } = await gqlResponse.json();
const getPosition = (variantId) =>
data?.nodes?.find(({ id }) => id === `gid://shopify/ProductVariant/${variantId}`)
?.product?.metafield?.value;
if (notAddedCheckoutItems.length) {
const items = notAddedCheckoutItems.map((item) => ({
...item,
properties: { ...item.properties, _ItemPostion: getPosition(item.id) }
}));
await fetch(`/cart/add.js`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Adoric-Request': 'true' },
body: JSON.stringify({ items })
});
}
if (requestBody instanceof FormData) {
const id = Number(requestBody.get('id'));
const patched = new FormData();
for (const [key, value] of requestBody.entries()) {
patched.append(key, value);
}
patched.append('properties[_ItemPostion]', getPosition(id) ?? '');
return { ...requestConfig, body: patched };
}
try {
const parsed = JSON.parse(requestBody);
if (Array.isArray(parsed.items)) {
parsed.items = parsed.items.map((item) => ({
...item,
properties: { ...item.properties, _ItemPostion: getPosition(item.id) }
}));
} else if (parsed.id) {
parsed.properties = {
...parsed.properties,
_ItemPostion: getPosition(parsed.id)
};
}
return { ...requestConfig, body: JSON.stringify(parsed) };
} catch {
return;
}
};
const setOverride = () => {
window.__ADORIC_BUNDLES__.overrides.addToCart = addToCart;
};
if (window.__ADORIC_BUNDLES__) {
setOverride();
return;
}
let attempts = 0;
const intervalId = setInterval(() => {
attempts++;
if (window.__ADORIC_BUNDLES__) {
setOverride();
clearInterval(intervalId);
} else if (attempts >= 10) {
clearInterval(intervalId);
}
}, 1000);
})();
</script>Editor is loading...
Leave a Comment