Last active
April 23, 2026 07:48
-
-
Save ceaksan/ca99b51c2da31ad6ce82506dfdf7b46a to your computer and use it in GitHub Desktop.
Shopify theme.liquid — Shopify.customerPrivacy state -> cart attribute consent snapshot sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {%- comment -%} theme.liquid, customer context bloğunun altına {%- endcomment -%} | |
| <script> | |
| (function () { | |
| function readConsent() { | |
| const cp = window.Shopify && window.Shopify.customerPrivacy; | |
| if (!cp || typeof cp.getVisitorConsent !== 'function') return null; | |
| const c = cp.getVisitorConsent(); | |
| return { | |
| _analytics_consent: c.analytics === true ? 'granted' : 'denied', | |
| _marketing_consent: c.marketing === true ? 'granted' : 'denied', | |
| _preferences_consent: c.preferences === true ? 'granted' : 'denied', | |
| _sale_of_data_consent: c.sale_of_data === true ? 'granted' : 'denied' | |
| }; | |
| } | |
| async function syncConsentToCart() { | |
| const desired = readConsent(); | |
| if (!desired) return; | |
| try { | |
| const cart = await fetch('/cart.js', { credentials: 'same-origin' }).then(r => r.json()); | |
| const current = cart.attributes || {}; | |
| const diff = Object.keys(desired).some(k => current[k] !== desired[k]); | |
| if (!diff) return; | |
| await fetch('/cart/update.js', { | |
| method: 'POST', | |
| credentials: 'same-origin', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ attributes: desired }) | |
| }); | |
| } catch (err) { | |
| // non-critical path | |
| } | |
| } | |
| document.addEventListener('DOMContentLoaded', syncConsentToCart); | |
| document.addEventListener('visitorConsentCollected', syncConsentToCart); | |
| })(); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment