将CartJS与多个变体下拉菜单结合使用

时间:2018-07-23 00:03:24

标签: shopify

我一直无法让CartJS与Shopify中的多个变体下拉菜单一起使用。每当我尝试将产品添加到购物车时,它都会给出400(错误请求)的信息。我猜是因为我的变体为空。我当前的代码是:

{% for product_option in product.options_with_values %}
  <label>
    {{ product_option.name }}
    <select>
      {% for value in product_option.values %}
        <option {% if product_option.selected_value == value %}selected{% endif %}>
          {{ value }}
        </option>
      {% endfor %}
    </select>
  </label>
{% endfor %}

以及option_selector代码。

<script>
  var selectCallback = function(variant, selector) {
    timber.productPage({
      money_format: "{{ shop.money_format }}",
      variant: variant,
      selector: selector
    });
  };

  (function($) {
    new Shopify.OptionSelectors('productSelect', {
      product: {{ product | json }},
      onVariantSelected: selectCallback,
      enableHistoryState: true
    });

    // Add label if only one product option and it isn't 'Title'. Could be 'Size'.
    {% if product.options.size == 1 and product.options.first != 'Title' %}
      $('.selector-wrapper:eq(0)').prepend('<label for="productSelect-option-0">{{ product.options.first | escape }}</label>');
    {% endif %}

    // Hide selectors if we only have 1 variant and its title contains 'Default'.
    {% if product.variants.size == 1 and product.variants.first.title contains 'Default' %}
      $('.selector-wrapper').hide();
    {% endif %}
  });
</script>

1 个答案:

答案 0 :(得分:0)

链接选项可能是解决方法。

它与选择器(下拉列表)中的值匹配,以获得正确的变体。

https://help.shopify.com/en/themes/customization/products/variants/link-product-options-in-menus