theme-scripts icon indicating copy to clipboard operation
theme-scripts copied to clipboard

Error on product _validateSerializedArray with a single variant product

Open garrettboatman opened this issue 6 years ago • 7 comments

Error on a fresh yarn create slate-theme product page, adding a single variant product to the cart.

Perhaps related to #85 ?

Console output:

Error: is empty.
    at _validateSerializedArray (webpack-internal:///../node_modules/@shopify/theme-product/theme-product.js:109:11)
    at _createOptionArrayFromOptionCollection (webpack-internal:///../node_modules/@shopify/theme-product/theme-product.js:67:3)
    at getVariantFromSerializedArray (webpack-internal:///../node_modules/@shopify/theme-product/theme-product.js:35:21)
    at ProductForm.variant (webpack-internal:///../node_modules/@shopify/theme-product-form/theme-product-form.js:105:102)
    at ProductForm._getProductFormEventData (webpack-internal:///../node_modules/@shopify/theme-product-form/theme-product-form.js:186:19)
    at ProductForm._onSubmit (webpack-internal:///../node_modules/@shopify/theme-product-form/theme-product-form.js:150:24)

garrettboatman avatar Mar 30 '19 23:03 garrettboatman

I have encountered this problem now, although the error is added when adding the product, but can be added to gouwuche.

Noostop avatar May 16 '19 14:05 Noostop

The JS code expects a non-empty array with options. The array is collected from the form elements with names like "name=options[OptionName]".

The recommended liquid snippet contains logic like: {% unless product.has_only_default_variant %} some elements with the required names created {% endunless %}

Thus, no options elements for the single variant products.

Meanwhile, until it has been fixed in JS, I use slightly different logic: {% if product.has_only_default_variant != true %} as it were {% else %} <input type="hidden" id="defaultOption" name="options[Title]" value="Default title"> {% endif %}

So, a fake option created. Of course, it may confuse the further processors: ProductForm.options() will return non-empty list for the single variant product. At least, it does not throw error.

tms320c avatar Aug 01 '19 18:08 tms320c

Tiny note to add, the fake option above will only work if there's an existing product with an option value of 'Title'. However, using <input type="hidden" id="defaultOption" name="options[]" value="Default title"> should fix any additional errors such as Uncaught TypeError: Cannot read property 'id' of null

brantward avatar Nov 26 '19 08:11 brantward

I also ran into this issue. I believe a more thorough, readable solution is:

{% if product.has_only_default_variant %}
  {% assign option = product.options_with_values[0] %}
  {% assign value = option.values[0] %}
  <input type="hidden" id="Option{{ option.position }}-{{ value }}" name="options[{{ option.name }}]" value="{{ value }}">
{% else %}
  (current unless block)
{% endif %}

cfxd avatar Nov 28 '19 05:11 cfxd

I also ran into this issue. I believe a more thorough, readable solution is:

{% if product.has_only_default_variant %}
  {% assign option = product.options_with_values[0] %}
  {% assign value = option.values[0] %}
  <input type="hidden" id="Option{{ option.position }}-{{ value }}" name="options[{{ option.name }}]" value="{{ value }}"{% if option.selected_value == value %} checked{% endif %}>
{% else %}
  (current unless block)
{% endif %}

The "checked" attribute does not exist on a hidden input field and should only be used with Checkbox or Radio inputs only. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden

donnyburnside avatar Jan 23 '20 16:01 donnyburnside

I changed name="id" to name="options[default]" in the hidden input field, for single variation products. It worked but in all honestly I don't know why.

<input type="hidden" name="options[default]" value="{{ product.selected_or_first_available_variant.id }}" data-productid="{{ product.id }}">

jpjmortimer avatar Jul 19 '21 15:07 jpjmortimer

The "checked" attribute does not exist on a hidden input field and should only be used with Checkbox or Radio inputs only. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden

Right you are. Fixed and rebased!

cfxd avatar Jul 19 '21 16:07 cfxd