Coding condition based on the amount of Product Variants

Coding condition based on the amount of Product Variants

kmwes
Visitor
1 0 0

Hello,

 

I managed to make sku appear on the product cards. But I need help filtering that a bit.

My goal is to only show the sku on products without variants.

 

I've been trying to work this out with the following code. But It's not working.

 

<div class="skuproductcard">
{% if product.has_only_default_variant %}
<p234> ID nr. {{ card_product.variants[0].sku }} </p234>
{% endif %}
</div>

 

I would appreciate any help 🙂

Reply 1 (1)

Ole_Visionz
Shopify Partner
53 10 22

Some things I notice here:

 

First of all, <p234></p234> is no valid HTML element and should be <p></p>

 

Second, did you declare card_product as a variable before? If not, this is not an object you have access to and should be changed to product.

 

Third, if has_only_default_variant is true, then I think product.variants is empty. That means product.variants[0] won't return anything. (But not 100% sure)

If you find my suggestions helpful, kindly express your feedback by liking or marking them as a solution.
Our (German) agency: visionz.de
Our Shopify App for custom pricing & unlimited product options: Visionz Easy Product Options