Skip to content

Instantly share code, notes, and snippets.

@hsleewis
Last active January 3, 2019 17:16
Show Gist options
  • Save hsleewis/bf426a7da7fe7b11c7923e6752ea63da to your computer and use it in GitHub Desktop.
Save hsleewis/bf426a7da7fe7b11c7923e6752ea63da to your computer and use it in GitHub Desktop.
line item properties with variants example
THIS DOES NOT WORK FOR ALL THEMES
====
STEP 1: add this right above the </body> tag of your product template:
====
<div id="bundles_child_products" style="display:none;">
{% for variant in product.variants %}
{% if variant.metafields.bundles_app.content.size > 0 %}
<span id="bundle_{{variant.id}}" data-json='{{ variant.metafields.bundles_app.content | json}}'>
{% endif %}
{% endfor %}
</div>
====
STEP 2: add this to the callback of your linked options javascript:
====
// REMOVE THE OLD LINE ITEM PROPERTIES
Array.from(document.querySelectorAll('.bundles_lineitemProperty')).forEach(function(item) {
item.parentNode.removeChild(item)
});
// GET SELECTED ITEM AND CHECK IF ITS A BUNDLE
var bundle = document.querySelector('#bundle_'+ variant.id);
if( bundle != null ){
var json = JSON.parse( bundle.dataset.json );
// SELECT CART, IF YOU HAVE A DIFFERENT CART, CHANGE BELOW
var cartForm = document.querySelector("form[action='/cart/add']");
json.forEach(function (value, i) {
var itemNo = i +1;
var title = value.title;
var quantity = value.quantity;
var sku = value.sku;
// CREATE LINE ITEM PROPERTIES, IF YOU WANT TO SHOW THEM IN CART AND CHECKOUT, REMOVE _ BEFORE PRODUCT
var inputName = 'properties[_Product '+itemNo+']';
if( sku != null){
var inputValue = quantity + 'x ' + title + ' ['+sku+']';
}else{
var inputValue = quantity + 'x ' + title;
}
var lineItemPropertyInput = document.createElement("input");
lineItemPropertyInput.setAttribute('class', 'bundles_lineitemProperty');
lineItemPropertyInput.setAttribute('name', inputName);
lineItemPropertyInput.setAttribute('type', 'hidden');
lineItemPropertyInput.setAttribute('value', inputValue);
cartForm.appendChild(lineItemPropertyInput);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment