Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Last active April 24, 2026 15:24
Show Gist options
  • Select an option

  • Save ceaksan/c5c01b5fb097fe164c681b5c33eb7a3d to your computer and use it in GitHub Desktop.

Select an option

Save ceaksan/c5c01b5fb097fe164c681b5c33eb7a3d to your computer and use it in GitHub Desktop.
Shopify theme.liquid — customer metafield -> cart attribute sync (B2B, consent, customer_id)
{%- comment -%} theme.liquid — head sonuna yakın, Shopify.analytics yüklendikten sonra {%- endcomment -%}
{% if customer %}
<script>
(async function syncCustomerContext() {
try {
const desired = {
_b2b: {{ customer.b2b? | default: customer.metafields.custom.b2b_status.value | default: false | json }},
_customer_id: {{ customer.id | json }},
_customer_tags: {{ customer.tags | join: ',' | json }}
};
const cart = await fetch('/cart.js', { credentials: 'same-origin' }).then(r => r.json());
const current = cart.attributes || {};
const diff = Object.keys(desired).some(k => String(current[k]) !== String(desired[k]));
if (!diff) return;
await fetch('/cart/update.js', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
attributes: Object.fromEntries(
Object.entries(desired).map(([k, v]) => [k, String(v)])
)
})
});
} catch (err) {
// Sessiz fail; analytics enrichment non-critical path
}
})();
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment