【发布时间】:2020-01-18 05:53:02
【问题描述】:
在我的 shopify 主题中,我试图在颜色标签旁边显示颜色名称。例如,如果选择绿色,“颜色:”将变为“颜色:绿色”,如果选择红色,则将变为“颜色:红色”。我目前正在使用一个名为 Basel 的主题。
**巴塞尔主题产品页面 - https://new-basel2.myshopify.com/collections/bag/products/vintage-cinch-backpack
我尝试在我的液体文件中添加几行不同的代码,但没有任何效果。我现在意识到我需要使用 javascript 来做到这一点。这样当一个样本被点击时,它的值就会被获取并放入标签中。
我一点也不精通javascript。如果有人可以帮助编写代码或提供任何帮助,我将非常感激。
swatch.liquid -
{%- include 'check_not_feature_img' -%}
<table class="variations{%- if settings.swatch_design == '2' %} variant_square{%- elsif settings.swatch_design == '3' %} variant_simple{%- endif -%}" cellspacing="0">
{%- assign option_index = 0 -%}
{%- if settings.color_name_check != blank -%}{%- assign _gl_color_name = settings.color_name_check | replace: ' ,', ',' | replace: ', ', ',' | split: ',' -%}{%- assign gl_color_name = _gl_color_name | downcase -%}{%-else-%}{%- assign gl_color_name = '\nathan_dt\' -%}{%-endif-%}
<tbody>
{%- for option in product.options_with_values -%}
{%- assign option_index = forloop.index0 -%}
{%- assign downcased_option = option.name | downcase -%}
{%- assign downcased_option_check = option.name | downcase | prepend: '"' | append: '"' | strip -%}
{%- if downcased_option == 'color' or downcased_option == 'colour' or gl_color_name contains downcased_option_check -%}
<tr data-option-index="{{ option_index }}" id="gl_select_{{ option_index }}">
<td class="label"><label for="{{ option_index }}">{{ option.name }}</label></td>
<td class="value with-swatches">
<div class="swatches-select" data-id="{{ option_index }}" data-option-index="{{ option_index }}">
{%-assign index = 0 %}
{%- for value in option.values -%}
<div class="basel-swatch bg_color basel-tooltip swatch-size-{{settings.swatch_size}}{%- if settings.swatch_style == '1' %} colored-swatch{%- else %} image-swatch{%- endif %}{%- if first_available_variant and option.selected_value == value %} active-swatch{%- elsif forloop.first == true and product.selected_variant == blank and first_available_variant == false %} active-swatch{%- elsif option.selected_value == value and product.selected_variant != blank and first_available_variant == false %} active-swatch{%- endif %} bg_color_{{ value | handleize }} bg_{{ value | handleize }} swatch-enabled" data-value='{{ value | handleize }}' data-image-id="{{ featured_image_id[index] }}">
<span class="basel-tooltip-label">{{ value }}</span>{{ value }}
</div>
{%-assign index = index | plus: 1 %}
{%- endfor -%}
</div>
</td>
</tr>
{%- else- %}
<tr data-option-index="{{ option_index }}" id="gl_select_{{ option_index }}">
<td class="label"><label for="{{ option_index }}">{{ option.name }}</label></td>
<td class="value with-swatches">
<div class="swatches-select" data-id="{{ option_index }}" data-option-index="{{ option_index }}">
{%- for value in option.values -%}
<div class="basel-swatch basel-tooltip text-only swatch-size-{{settings.swatch_size}}{%- if first_available_variant and option.selected_value == value %} active-swatch{%- elsif forloop.first == true and product.selected_variant == blank and first_available_variant == false %} active-swatch{%- elsif option.selected_value == value and product.selected_variant != blank and first_available_variant == false %} active-swatch{%- endif %} bg_{{ value | handleize }} swatch-enabled" data-value='{{ value | handleize }}'>
<span class="basel-tooltip-label">{{ value }}</span>{{ value }}
</div>
{%- endfor -%}
</div>
</td>
</tr>
{%- endif -%}
{%- endfor -%}
</tbody>
</table>
我需要 javascript 来更改标签后的颜色名称 -
<td class="label"><label for="{{ option_index }}">{{ option.name }}</label></td>
包含var selectCallback = function(variant, selector)的3个文件
product.design_default.liquid - https://gist.github.com/lok343/9e3286aa262b915438c091bf820eebcf
sn-ps/product.json.liquid - https://gist.github.com/lok343/b7251646824b6a9f265b0d7e270f64cd
sn-ps/product_js.liquid - https://gist.github.com/lok343/8690d8a7fb8246f0ca6730899f736050
更新代码
var selectCallback = function(variant, selector) {
var selectedOptions = selector.selectedValues(); // This gives an array of all the selected options
var optionData = selector.product.options; // This gives information about the options set up on the product
var form = selector.variantIdField.form; // This gives us the enclosing form element without needing to do any query-selecting
// Now lets loop through the options and update the elements
for(var i=0; i<selectedOptions.length; i++){
var optionLabel = form.querySelector('[data-option-index="' + i + '"] label') // This reads, "Find the first <label> element inside the element with the specified data-option-index attribute inside my product form"
// Let's make sure our code will not break and quickly check to make sure we found the element we were after. If it's blank, continue to the next option. Raising an appropriate error is left as an exercise for the reader.
if(!optionLabel){
continue;
}
var optionName = typeof optionData[i] === 'object' ? optionData[i].name : optionData[i]; // This is checking to see which of Shopify's 2 different option formats got fed into this function - it could have been either an array of titles or an array of objects that include both the name and an array of all the values. The ( check ? result_if_true : result_if_false ) syntax is called a ternary expression and is shorthand for if(check){ value_if_true } else { value_if_false }
if (optionName.toLowerCase('Color')) {
optionLabel.innerText = optionName + ': ' + selectedOptions[i]
}
}
【问题讨论】:
-
唯一可行的方法是使用一组预定义的颜色/名称。您的选项数组是否包含颜色名称?如果是这样,当用户选择一种颜色时,使用 JavaScript 在标签旁边设置颜色名称。
-
@TomShaw - 我假设这个用户想要显示的颜色名称将是产品上定义的颜色名称,所以是的 - 肯定会有一个预定义颜色/名称的列表,因为那些将是产品本身:)
标签: javascript jquery html shopify