Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hello, I am trying to display variant metafield on the product page under the product title for the Testament theme. I added this code in the main-product.liquid file under the product title but it is static and does not change dynamically when different variants are selected.
Code I used:
{% for variant in product.variants %}
{% if product.selected_or_first_available_variant.id == variant.id %}
<strong>{{ variant.metafields.custom.description }}</strong>
{% endif %}
{% endfor %}
I know there is something missing but I can't figure it out. I feel like I am half way there. If someone can please assist me then I would greatly appreciate it!
Hi @Sensei_Ryan
We would like to give you a suggestion. It may be different cause I used the dawn theme to test this
- Solution 1:
Step 1: Go to Online store -> edit code -> global.js
Step 2: Find the onVariantChange() function and replace it with this code
onVariantChange() {
this.updateOptions();
this.updateMasterId();
this.toggleAddButton(true, '', false);
this.updatePickupAvailability();
this.removeErrorMessage();
if (!this.currentVariant) {
this.toggleAddButton(true, '', true);
this.setUnavailable();
} else {
this.updateMedia();
this.updateURL();
this.updateVariantInput();
this.renderProductInfo();
this.updateShareUrl();
// Below is the newly added line as shown in the video
this.updateVariantDetails(this.currentVariant);
}
}
updateVariantDetails(currentVariant){
const variants = document.querySelectorAll('[data-variant-id]')
variants.forEach( function(variant) {
variant.style.display = 'none';
if(variant.dataset.variantId == currentVariant.id){
variant.style.display = 'block'
}
});
}
Step 3: Find the CSS file (with dawn theme is base.css/ collage.css/theme.css...) and paste this code
.variant__description {
display: none
}
Step 4: Enjoy the result
This is our tested video https://www.loom.com/share/ac2c265554c54d6b9f96c759d669bfd1
A note that it's working with the Dawn theme, so I'm not sure if it works with your theme or not. So can you kindly give me permission to your store to try the code?
- Solution 2: You can try this app to show the variant. When you click 1 variant and it will show all your variants chosen and will display on the cart page and have a lot of types of variants like: (button, swatch, textfile, numberfield, .datepicker, file upload...).
Your theme will become like this:
+ Product page:
+ Cart page:
I hope that it will work for you.
If our suggestions are useful, please let us know by giving it a like, marking it as a solution, or donating here ☕.
B2B Solution & Custom Pricing | Product Labels by BSS
Need help from our expert? Kindly share your request with us via community@bsscommerce.com
Thanks for the info! I couldn't get it to work myself though. What email should I send the permission to?
I have a question for you. I have used that code and it works great! All my variant metadata does exactly what I want it to.
My problem is, that when I add this part of the code:
updateVariantDetails(currentVariant){
const variants = document.querySelectorAll('[data-variant-id]')
variants.forEach( function(variant) {
variant.style.display = 'none';
if(variant.dataset.variantId == currentVariant.id){
variant.style.display = 'block'
}
});
}
my local pickup availability no longer refreshes with the variant change. It disappears when you choose a different variant, but will reappear when you select the variant that the page loaded to. I have tried everything I can think of and I can't seem to get the pickup availability to show right. Otherwise, amazing coding!!
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:
<div id="variant-metafield">
{% assign current_variant = product.selected_or_first_available_variant %}
{% if current_variant.metafields.custom.varient_data %}
<p id="variant-data">{{ current_variant.metafields.custom.varient_data }}</p>
{% endif %}
</div>
step 3: Now goto global.js file in Edit code to add the below code inside the
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024