Shopify themes, liquid, logos, and UX
HI Sir,
After select one product, loading option1 selection will cause some delay. How can I able to base on selected product ID to retriever all option1 variants ID in customer-card-product.liquid
Hi @Boyi,
Do you mean that when you change variants, it will take time to load, and you don't want this?
Yes, some delay (maybe few seconds) when we change option1. Base on our collected info, many cust only stay with us for few mins so few seconds is quite long already. I already have js code to save the 1st selection into cache so subsequent selection will be no delay. We wish to resolve the delay at 1st selection as well. One idea in my mind is to preload them into cache when cust select product but we need to know all variant ID of option1 of product. That is reason we hope can do it in customer-card-product.liquit file or separate js file.
I noted below code is used to select product and I am able to get product handle already. But now problem is how can I get all option1's variant ID of this product. Would you please help to advise for it. Tks first for your help.
====================================================================
Hi @Boyi,
You have 2 options for it:
- For code JS:
<script type="application/json" id="ProductJson-{{ section.id }}">
{{ product | json }}
</script>
- For Liquid:
{% for variant in product.variants %}
{% comment %}
show variant object
{% endcomment %}
{% endfor %}
Hi @Boyi,
I recommend using JS, it will make everything better
Hi Namphan,
Tks for your advice first. I actually able to capture the event in global.js file as below but fetch not work. Would you please help to advise how to amend below code to get all option1's variant ID?
document.addEventListener('DOMContentLoaded', function() {
// Function to handle clicks on product links
function handleProductLinkClick(event) {
const productHandle = this.getAttribute('data-selected-product-handle');
// Retrieve the product data using Shopify's frontend API endpoint
fetch('/products/' + productHandle + '.js').then(function(response){ return response.json() }).then(
function(product){
let variant = product.variants.find(function(variant){ return variant.id == variant_id })
// Do stuff with your product & variant objects
}
)
)
Hi @Boyi,
Please change code:
document.addEventListener('DOMContentLoaded', function() {
async function handleProductLinkClick(event) {
const productHandle = this.getAttribute('data-selected-product-handle');
const data = await getData(productHandle);
}
function getData(handle) {
const data = new Promise(async (resolve, reject) => {
const response = await fetch(`/products/${handle}.js`);
if (response.ok) {
const responseAsJson = await response.json();
resolve(responseAsJson);
} else {
reject(handle);
}
});
return data;
}
}
Hi @namphan ,
Tks for your help first. Btw I have tried it but unluck and nothing to return. I add 2 debug console.log as below and nothing captured. If something wrong with this code?
document.addEventListener('DOMContentLoaded', function() {
async function handleProductLinkClick(event) {
const productHandle = this.getAttribute('data-selected-product-handle');
const data = await getData(productHandle);
console.log('Shopify product:', data);
}
function getData(handle) {
const data = new Promise(async (resolve, reject) => {
const response = await fetch(`/products/${handle}.js`);
console.log('Shopify response:', response);
if (response.ok) {
const responseAsJson = await response.json();
resolve(responseAsJson);
} else {
reject(handle);
}
});
return data;
}
}
Hi @Boyi,
Can I send you a collaborator invitation? it will help me check everything in more detail
@namphan ,
Sure. Tks first. If possible we can do it tomorrow Singapore time between 2~5pm or 26th Jun Singapore time after 2pm?
Rgds
Tan Bing
Hi @Boyi,
You can send it via private message, I will check it
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025