Use "product.first_available_variant" for AddToCart when product.selected_variant.id is Null

Highlighted
New Member
1 0 0

My store Add To Cart button as coded is enabled and allows adding a product without a variant.  This is problematic - and I only want them to add /Product?12345678

I could A) disable Add To Cart until they click a variant or B) check for product.selected_variant.id == null and if true - 'build' the right product/variant structure - using product.first_available_variant to pass to add to cart.

 

I ran into issues with A (happy to hear suggestions for that), but I have been trying B - as the interface is cleaner and it's a valid assumption in my store that if no variant - I do indeed want them to use the first available variant.


I THINK I am close to a solution, but am running into "invalid parameter" issues when attempting the Add. 

I THINK I either have a syntax problem - or need to add some code somewhere other than just product.liquid.


ANY pointers are appreciated !!

 

Current Production Code in Product.liquid

<option data-sku="{{ variant.sku }}"
{% if variant.inventory_quantity <= 0 and variant.available == false %}DISABLED {% endif %}
{% if variant == product.selected_or_first_available_variant %}selected="selected" {% endif %}
value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money }}</option>

 

Attempt 1

<option data-sku="{{ variant.sku }}"
{% if variant.inventory_quantity <= 0 and variant.available == false %}DISABLED {% endif %}
{% if variant == product.selected_or_first_available_variant %}selected="selected" {% endif %}
{% if product.selected_variant.id == null %}selected="selected" value="{{ product.first_available_variant }}" {% endif %}
value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money }}</option>


Attempt 2

<option data-sku="{{ variant.sku }}"
{% if variant.inventory_quantity <= 0 and variant.available == false %}DISABLED {% endif %}
{% if variant == product.selected_or_first_available_variant %}selected="selected" {% endif %}
{% if product.selected_variant.id == null %}selected="selected" {% endif %}
value="{{ product.first_available_variant }}">{{ variant.title }} - {{ variant.price | money }}</option>

0 Likes