Shopify变量样本或单选按钮,而不是Slate中的下拉菜单

sop*_*e f 3 shopify slate

对于Slate,尚不支持Shopify的色板教程,并且代码库中不再存在引用的选择回调.是否可以修改本教程以使用Slate主题来创建单选按钮或样本而不是用于在产品模板上选择变体的下拉列表?

sop*_*e f 7

是.通过稍微修改代码,我能够使本教程工作.只有在更新shopify教程以对应Slate之后,此解决方法才有意义.

按照说明按照教程进行操作.当您进入" 查找selectCallback函数 " 步骤时,您会注意到Slate中没有selectCallback函数.哎呀!而是在theme.js中找到"_onSelectChange"并在那里添加代码.这是添加了色板代码的最终功能:

/**
 * Event handler for when a variant input changes.
 */
_onSelectChange: function() {
  var variant = this._getVariantFromOptions();

  this.$container.trigger({
    type: 'variantChange',
    variant: variant
  });

  if (!variant) {
    return;
  }

// BEGIN SWATCHES
var selector = this.originalSelectorId;
if (variant) {
    var form = $(selector).closest('form');
    for (var i=0,length=variant.options.length; i<length; i++) {
        var radioButton = form.find('.swatch[data-option-index="' + i + '"] :radio[value="' + variant.options[i] +'"]');
        if (radioButton.size()) {
            radioButton.get(0).checked = true;
        }
    }
}
// END SWATCHES

  this._updateMasterSelect(variant);
  this._updateImages(variant);
  this._updatePrice(variant);
  this.currentVariant = variant;

  if (this.enableHistoryState) {
    this._updateHistoryState(variant);
  }
},
Run Code Online (Sandbox Code Playgroud)

然后,一旦你完成了教程,你会发现它仍然无法正常工作.这是因为您添加到theme.liquid的代码使用的类不再适用于您的变体选择.在product.liquid上(这是大多数Slate主题的一节)将类"single-option-selector"添加到您的选择中,如下所示:

    {% unless product.has_only_default_variant %}
    {% for option in product.options_with_values %}
    <div class="selector-wrapper js">
    <label for="SingleOptionSelector-{{ forloop.index0 }}">
      {{ option.name }}
    </label>
    <select
              id="SingleOptionSelector-{{ forloop.index0 }}"
              class="single-option-selector"
              data-single-option-selector
              data-index="option{{ option.position }}">
             {% for value in option.values %}
                 <option value="{{ value | escape }}"
                 {% if option.selected_value == value %}selected="selected"{% endif %}>
                 {{ value }}
                </option>
            {% endfor %}
    </select>
    </div>
    {% endfor %}
    {% endunless %}
Run Code Online (Sandbox Code Playgroud)

现在,教程应该按照预期的方式工作.我希望这可以帮助别人!