3

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

4

1 回答 1

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);
  }
},

然后,一旦你完成了教程,你会发现它仍然无法正常工作。这是因为您添加到 theme.liquid 的代码使用了一个不再在您的变体 Selects 中的类。在 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 %}

现在,本教程应该可以正常工作了。我希望这可以帮助某人!

于 2017-06-29T13:54:24.323 回答