Skip to content

Instantly share code, notes, and snippets.

@andreibabor
Last active May 8, 2019 08:54
Show Gist options
  • Save andreibabor/532897f5b00a238ff6e5bb7adf161614 to your computer and use it in GitHub Desktop.
Save andreibabor/532897f5b00a238ff6e5bb7adf161614 to your computer and use it in GitHub Desktop.
Shopify: Metafield Variant
<!-- begin metafield -->
<div id="div1"></div>
<script type="text/javascript">
var variants = {};
{% for variant in product.variants %}
variants[{{variant.id}}] = "{{ variant.metafields.product_info.pdf}}";
var div1=document.getElementById("div1");
var newlink = document.createElement('a');
newlink.setAttribute('class', 'link {{variant.id}}');
// newlink.setAttribute('href', '{{ variant.metafields.product_info.pdf }}');
newlink.setAttribute("target", "_blank");
newlink.innerHTML="<span>Click here</span> to see our Certificate of Analysis report</a>"
{% unless variant.metafields.product_info.pdf contains '#' or variant.metafields.product_info.pdf == blank %}
div1.appendChild(newlink);
{% endunless %}
{% endfor %}
</script>
<!-- end metafield -->
#div1{
.link{
display: none;
font-weight: 700;
color: #343434;
span{
color: #69f;
}
}
a[href^="#"]{
display: none !important;
}
}
//de cautat variable callback din theme.js
if (variant.available) {
$('#div1 a:nth-child(1)').attr("href",(variants[variant.id])).css( "display", "block" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment