通过点击多属性的图片来选择多属性

通常,客户可从下拉菜单中选择多属性。您可能希望您的客户能够通过点击多属性图片来选择产品多属性。

若要通过点击多属性图片来选择产品多属性,请执行以下操作:

PC:

  1. 在 Shopify 后台中,转到在线商店 > 模板

  2. 找到要编辑的模板,然后点击操作 > 编辑代码

  1. 在 Assets 文件夹中打开 theme.js 或 theme.js.liquid 文件。

苹果系统:

  1. 在 Shopify 应用中,轻触商店

  2. 销售渠道部分中,轻触在线商店

  3. 轻触 Manage themes(管理模板)。

  4. 找到要编辑的模板,然后点击操作 > 编辑代码

  1. 在 Assets 文件夹中打开 theme.js 或 theme.js.liquid 文件。

安卓系统:

  1. 在 Shopify 应用中,轻触商店

  2. 销售渠道部分中,轻触在线商店

  3. 轻触 Manage themes(管理模板)。

  4. 找到要编辑的模板,然后点击操作 > 编辑代码

  1. 在 Assets 文件夹中打开 theme.js 或 theme.js.liquid 文件。

  1. 在文件底部,粘贴以下代码:

const selectVariantByClickingImage = {   // Create variant images from productJson object   _createVariantImage: function (product) {     const variantImageObject = {};     product.variants.forEach((variant) => {       if (         typeof variant.featured_image !== 'undefined' &&         variant.featured_image !== null       ) {         const variantImage = variant.featured_image.src           .split('?')[0]           .replace(/http(s)?:/, '');         variantImageObject[variantImage] =           variantImageObject[variantImage] || {};         product.options.forEach((option, index) => {           const optionValue = variant.options[index];           const optionKey = `option-${index}`;           if (             typeof variantImageObject[variantImage][optionKey] === 'undefined'           ) {             variantImageObject[variantImage][optionKey] = optionValue;           } else {             const oldValue = variantImageObject[variantImage][optionKey];             if (oldValue !== null && oldValue !== optionValue) {               variantImageObject[variantImage][optionKey] = null;             }           }         });       }     });     return variantImageObject;   },   _updateVariant: function (event, id, product, variantImages) {     const arrImage = event.target.src       .split('?')[0]       .replace(/http(s)?:/, '')       .split('.');     const strExtention = arrImage.pop();     const strRemaining = arrImage.pop().replace(/_[a-zA-Z0-9@]+$/, '');     const strNewImage = `${arrImage.join('.')}.${strRemaining}.${strExtention}`;     if (typeof variantImages[strNewImage] !== 'undefined') {       product.variants.forEach((option, index) => {         const optionValue = variantImages[strNewImage][`option-${index}`];         if (optionValue !== null && optionValue !== undefined) {           const selects = document.querySelectorAll('#'+ id + ' [class*=single-option-selector]');           const options = selects[index].options;           for (let option, n = 0; (option = options[n]); n += 1) {             if (option.value === optionValue) {               selects[index].selectedIndex = n;               selects[index].dispatchEvent(new Event('change'));               break;             }           }         }       });     }   },   _selectVariant: function() {     const productJson = document.querySelectorAll('[id^=ProductJson-');     if (productJson.length > 0) {       productJson.forEach((product) => {         const sectionId = product.id.replace("ProductJson-", "shopify-section-");         const thumbnails = document.querySelectorAll('#'+ sectionId + ' img[src*="/products/"]');         if (thumbnails.length > 1) {           const productObject = JSON.parse(product.innerHTML);           const variantImages = this._createVariantImage(productObject);           // need to check variants > 1           if (productObject.variants.length > 1) {             thumbnails.forEach((thumbnail) => {               thumbnail.addEventListener('click', (e) =>                 this._updateVariant(e, sectionId, productObject, variantImages),               );             });           }         }       });     }   }, }; if (document.readyState !== 'loading') {   selectVariantByClickingImage._selectVariant(); } else {   document.addEventListener(     'DOMContentLoaded',     selectVariantByClickingImage._selectVariant(),   ); }
  1. 点击保存

提示:此自定义设置并非适用于所有模板。如果不适用于您的模板,您可以聘请 Shopify 专家来帮助您实现此解决方案。

Shopify商户官网原文详情:

Select variants by clicking their images

Normally, variants are selected from a drop-down menu. You might want your customers to be able to select a product variant by clicking a variant image.

To select product variants by clicking a variant image:

PC:

  1. From your Shopify admin, go to Online Store > Themes.

  2. Find the theme you want to edit, and then click Actions > Edit code.

  1. Open the theme.js or theme.js.liquid file in your Assets folder.

iPhone:

  1. From the Shopify app, tap Store.

  2. In the Sales channels section, tap Online Store.

  3. Tap Manage themes.

  4. Find the theme you want to edit, and then click Actions > Edit code.

  1. Open the theme.js or theme.js.liquid file in your Assets folder.

Android:

  1. From the Shopify app, tap Store.

  2. In the Sales channels section, tap Online Store.

  3. Tap Manage themes.

  4. Find the theme you want to edit, and then click Actions > Edit code.

  1. Open the theme.js or theme.js.liquid file in your Assets folder.

  1. At the bottom of the file, paste the following code:

    const selectVariantByClickingImage = {   // Create variant images from productJson object   _createVariantImage: function (product) {     const variantImageObject = {};     product.variants.forEach((variant) => {       if (         typeof variant.featured_image !== 'undefined' &&         variant.featured_image !== null       ) {         const variantImage = variant.featured_image.src           .split('?')[0]           .replace(/http(s)?:/, '');         variantImageObject[variantImage] =           variantImageObject[variantImage] || {};         product.options.forEach((option, index) => {           const optionValue = variant.options[index];           const optionKey = `option-${index}`;           if (             typeof variantImageObject[variantImage][optionKey] === 'undefined'           ) {             variantImageObject[variantImage][optionKey] = optionValue;           } else {             const oldValue = variantImageObject[variantImage][optionKey];             if (oldValue !== null && oldValue !== optionValue) {               variantImageObject[variantImage][optionKey] = null;             }           }         });       }     });     return variantImageObject;   },   _updateVariant: function (event, id, product, variantImages) {     const arrImage = event.target.src       .split('?')[0]       .replace(/http(s)?:/, '')       .split('.');     const strExtention = arrImage.pop();     const strRemaining = arrImage.pop().replace(/_[a-zA-Z0-9@]+$/, '');     const strNewImage = `${arrImage.join('.')}.${strRemaining}.${strExtention}`;     if (typeof variantImages[strNewImage] !== 'undefined') {       product.variants.forEach((option, index) => {         const optionValue = variantImages[strNewImage][`option-${index}`];         if (optionValue !== null && optionValue !== undefined) {           const selects = document.querySelectorAll('#'+ id + ' [class*=single-option-selector]');           const options = selects[index].options;           for (let option, n = 0; (option = options[n]); n += 1) {             if (option.value === optionValue) {               selects[index].selectedIndex = n;               selects[index].dispatchEvent(new Event('change'));               break;             }           }         }       });     }   },   _selectVariant: function() {     const productJson = document.querySelectorAll('[id^=ProductJson-');     if (productJson.length > 0) {       productJson.forEach((product) => {         const sectionId = product.id.replace("ProductJson-", "shopify-section-");         const thumbnails = document.querySelectorAll('#'+ sectionId + ' img[src*="/products/"]');         if (thumbnails.length > 1) {           const productObject = JSON.parse(product.innerHTML);           const variantImages = this._createVariantImage(productObject);           // need to check variants > 1           if (productObject.variants.length > 1) {             thumbnails.forEach((thumbnail) => {               thumbnail.addEventListener('click', (e) =>                 this._updateVariant(e, sectionId, productObject, variantImages),               );             });           }         }       });     }   }, }; if (document.readyState !== 'loading') {   selectVariantByClickingImage._selectVariant(); } else {   document.addEventListener(     'DOMContentLoaded',     selectVariantByClickingImage._selectVariant(),   ); }
  2. Click Save.

文章内容来源:Shopify商户官方网站


(本文内容根据网络资料整理,出于传递更多信息之目的,不代表连连国际赞同其观点和立场)