Hi,
Dawn theme 14 version , I am create a metafield for variants. Display the metafield on the each product page. It should dynamically change based on variant selection without reloading the page.
step 1: I am adding the metafields for varients like goto shopify admin page create the variant metafied afteer to add the metafield in product page. this process is like settings->custom data->variants
step 2: To add custom liquid code like online store->customize->click to the product(Template->product information)->Add block->custom liquid->to enter the below code
code:
{% assign current_variant = product.selected_or_first_available_variant %}
{% if current_variant.metafields.custom.varient_data %}
{{ current_variant.metafields.custom.varient_data }}
{% endif %}
step 3: Now goto global.js file in Edit code to add the below code inside the
renderProductInfo(){}
code:
const metafieldSource = html.getElementById(variant-data);
const metafieldDestination = document.getElementById(‘variant-data’);
if (metafieldSource && metafieldDestination) {
metafieldDestination.innerText = metafieldSource.innerText;
}
conclusion:
this is i am adding the code on global.js
renderProductInfo() {
const requestedVariantId = this.currentVariant.id;
const sectionId = this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section;
fetch(
${this.dataset.url}?variant=${requestedVariantId}§ion_id=${ this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section }
)
.then((response) => response.text())
.then((responseText) => {
// prevent unnecessary ui changes from abandoned selections
if (this.currentVariant.id !== requestedVariantId) return;
const html = new DOMParser().parseFromString(responseText, ‘text/html’);
// Update metafield data dynamically
const metafieldSource = html.getElementById(variant-data);
const metafieldDestination = document.getElementById(‘variant-data’);
if (metafieldSource && metafieldDestination) {
metafieldDestination.innerText = metafieldSource.innerText;
}
const destination = document.getElementById(price-${this.dataset.section});
const source = html.getElementById(
price-${this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section}
);
const skuSource = html.getElementById(
Sku-${this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section}
);
const skuDestination = document.getElementById(Sku-${this.dataset.section});
const inventorySource = html.getElementById(
Inventory-${this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section}
);
const inventoryDestination = document.getElementById(Inventory-${this.dataset.section});
const volumePricingSource = html.getElementById(
Volume-${this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section}
);
this.updateMedia(html);
const pricePerItemDestination = document.getElementById(Price-Per-Item-${this.dataset.section});
const pricePerItemSource = html.getElementById(
Price-Per-Item-${this.dataset.originalSection ? this.dataset.originalSection : this.dataset.section}
);
const volumePricingDestination = document.getElementById(Volume-${this.dataset.section});
const qtyRules = document.getElementById(Quantity-Rules-${this.dataset.section});
const volumeNote = document.getElementById(Volume-Note-${this.dataset.section});
if (volumeNote) volumeNote.classList.remove(‘hidden’);
if (volumePricingDestination) volumePricingDestination.classList.remove(‘hidden’);
if (qtyRules) qtyRules.classList.remove(‘hidden’);
if (source && destination) destination.innerHTML = source.innerHTML;
if (inventorySource && inventoryDestination) inventoryDestination.innerHTML = inventorySource.innerHTML;
if (skuSource && skuDestination) {
skuDestination.innerHTML = skuSource.innerHTML;
skuDestination.classList.toggle(‘hidden’, skuSource.classList.contains(‘hidden’));
}
if (volumePricingSource && volumePricingDestination) {
volumePricingDestination.innerHTML = volumePricingSource.innerHTML;
}
if (pricePerItemSource && pricePerItemDestination) {
pricePerItemDestination.innerHTML = pricePerItemSource.innerHTML;
pricePerItemDestination.classList.toggle(‘hidden’, pricePerItemSource.classList.contains(‘hidden’));
}
const price = document.getElementById(price-${this.dataset.section});
if (price) price.classList.remove(‘hidden’);
if (inventoryDestination) inventoryDestination.classList.toggle(‘hidden’, inventorySource.innerText === ‘’);
const addButtonUpdated = html.getElementById(ProductSubmitButton-${sectionId});
this.toggleAddButton(
addButtonUpdated ? addButtonUpdated.hasAttribute(‘disabled’) : true,
window.variantStrings.soldOut
);
publish(PUB_SUB_EVENTS.variantChange, {
data: {
sectionId,
html,
variant: this.currentVariant,
},
});
});
}