I am having an issue where if a product does not have a variant it will not be added to the cart. Also, I would like to hide variant selection if product has no variants.
I would really appreciate if someone could help me! Thank you.
Here’s my code:
{% assign has_multiple_variants = false %}
{% if product.variants.size >= 1 %}
{% assign has_multiple_variants = true %}
{% endif %}
{% assign has_multiple_options = false %}
{% if product.options.size > 1 %}
{% assign has_multiple_options = true %}
{% endif %}
{% assign has_no_variants = false %}
{% if product.options.size = 0 %}
{% assign has_no_variants = true %}
{% endif %}
{% assign has_selected_variant = false %}
{% if product.selected_variant != nil %}
{% assign has_selected_variant = true %}
{% endif %}
{% if has_multiple_variants %}
{% include ‘get-variants-with-quantity-json’ with product: product %}
{% endif %}
{% assign can_add_to_cart = false %}
{% if has_selected_variant and product.selected_variant.available %}
{% assign can_add_to_cart = true %}
{% elsif has_multiple_variants == false and product.available %}
{% assign can_add_to_cart = true %}
{% endif %}
{% assign can_add_to_cart = false %}
{% if has_no_variants %}
{% assign can_add_to_cart = true %}
{% endif %}
{% if has_multiple_options or has_multiple_variants %}
{% for option in product.options_with_values %}
{% assign option_name = ‘option’ | append: option.position %}
{% assign selected = false %}
{% if has_selected_variant %}
{% assign selected = product.selected_variant[option_name] %}
{% endif %}
{% if option.name == ‘Color’ %}
{% include ‘product-option-row’ with option: option, option_name: option_name, selected: selected, force_colors: true %}
{% else %}
{% include ‘product-option-row’ with option: option, option_name: option_name, type: ‘select’, selected: selected %}
{% endif %}
{% endfor %}
{% endif %}
{% for option in product.options_with_values %}
{% if product.variants.size == 1 %}
{% endif %}
{% endfor %}
<button type=“submit” name=“add” id=“add-to-cart-button” class=“button-cta” {% if can_add_to_cart != true %}disabled{% endif %}>
Add to cart