I have a weird issue with the attribute data. I have read similar cases, but I haven’t found the correct answer. I have this code that adds a value fetched from the URL params and stores it in sessionStorage (just in case) and in cart.json, so I can have this value in my orders and generate reports based on this data. Recently, I added a “default” value to one of these fields so that if it is not loaded in the params or session, I get a “default value.” This way, if for any reason I need to modify this value in the orders, I have this “key=value” available and don’t have to depend on a third party to allow me to add it. Does that make sense?
After adding this “default value,” we noticed that several orders are arriving without this value, which concerns us because our business model depends on having reliable information in all cases. Am I executing the code incorrectly? I’ve tested multiple cases, and in all of them, the value from cart.json is coming through correctly (verified through console.log), but it is not being sent with the order when it passes through the checkout.
Here is my script, and I have it in the theme.liquid.
<script>
document.addEventListener("DOMContentLoaded", async function () {
console.log("? Script cargado en:", window.location.href);
// Obtener 'sref' y 'ml-shared-cart-id' de la URL
const urlParams = new URLSearchParams(window.location.search);
let specialistRef = urlParams.get('sref') || sessionStorage.getItem('sref') || "0000";
let shareCart = urlParams.get('ml-shared-cart-id') || sessionStorage.getItem('shareCart') || "";
console.log("? 'sref' en URL o SessionStorage:", specialistRef);
console.log("? 'ml-shared-cart-id' en URL o SessionStorage:", shareCart);
// Guardar valores en sessionStorage si vienen en la URL
if (urlParams.get('sref')) {
sessionStorage.setItem('sref', specialistRef);
}
if (urlParams.get('ml-shared-cart-id')) {
sessionStorage.setItem('shareCart', shareCart);
}
// Obtener datos actuales del carrito
const cartResponse = await fetch('/cart.js');
const cartData = await cartResponse.json();
let currentSpecialistRef = cartData.attributes?.specialist_ref || "";
console.log("? 'specialist_ref' actual en el carrito:", currentSpecialistRef);
// Lógica de actualización:
// 1. Si el carrito tiene un 'specialist_ref' diferente de 0000 y viene otro diferente de 0000 → ACTUALIZAR
// 2. Si el carrito tiene '0000' o está vacío → ACTUALIZAR con el mejor valor disponible
// 3. Si el carrito ya tiene un valor válido y el nuevo es '0000' → NO MODIFICAR
if (
(currentSpecialistRef !== "0000" && specialistRef !== "0000" && currentSpecialistRef !== specialistRef) ||
(currentSpecialistRef === "" || currentSpecialistRef === "0000")
) {
fetch('/cart/update.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
attributes: {
specialist_ref: specialistRef,
share_cart: shareCart || ""
}
}),
})
.then(response => response.json())
.then(data => {
console.log("✅ Notas del carrito actualizadas:", data);
})
.catch(error => console.error("⚠️ Error actualizando notas del carrito:", error));
} else {
console.log("✅ 'specialist_ref' ya es válido y no se modifica.");
}
});
</script>
I added two data attributes where in one, the specialist_ref is present, and in the other, it is not (both orders were placed on the same day, one following the other)
thanks for any help!

