Skip to content

Instantly share code, notes, and snippets.

@richietyler
Last active August 8, 2024 02:18
Show Gist options
  • Save richietyler/d2dee368e99c342b72c1e4e11555279b to your computer and use it in GitHub Desktop.
Save richietyler/d2dee368e99c342b72c1e4e11555279b to your computer and use it in GitHub Desktop.
Liquid snippet for TrueFit Size Sampling Integration compatible with drawer style carts
{%- if template =='cart' or template contains 'product' -%}
<script>(function(r,e,o){
var w=window,d=document,t,s,x;
w.tfcapi=t=w.tfcapi||function(){t.q=t.q||[]; t.q.push(arguments);};
t('init', {storeKey: r, environment: e, ...o});
x=d.getElementsByTagName('script')[0];
s=d.createElement('script');
s.type='text/javascript';s.async=true;
s.src='https://'+r+'-cdn'+(e==='dev'||e==='staging'?'.'+e:'')+ '.truefitcorp.com/fitrec/'+r+'/js/tf-integration.js';
x.parentNode.insertBefore(s,x);
// Don't change anything above this line.
// ATTENTION: Replace [3LA] with your 3-letter TrueFit acronym
})(‘[3LA]’, 'prod');</script>
<script type="text/javascript">
function tfSizeSampling() {
const CART_INFO = [];
fetch(window.Shopify.routes.root + 'cart.js')
.then((response) => response.json())
.then((cart) => {
console.log('TF - cart result:', cart);
cart.items.forEach((item) => {
const sizePosition = item.options_with_values.findIndex((i) => i.name.toLowerCase().includes('size'));
if (sizePosition) {
CART_INFO.push({
styleId: item.product_id.toString(),
styleName: item.product_title,
size: item.variant_options[sizePosition]
});
}
});
console.log('calling tf sizeSampling with cart info:', CART_INFO);
tfcapi('sizeSampling', CART_INFO);
});
}
tfSizeSampling();
{%- if template contains 'product' -%}
const productForm = document.querySelectorAll('form[action="/cart/add"]');
if (productForm) {
productForm.forEach(form => {
form.addEventListener('submit', () => {
setTimeout(() => {
console.log('Item added to cart - triggering tfSizeSampling');
tfSizeSampling();
},1000);
})
})
}
{%- endif -%}
</script>
{%- endif -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment