I'm moving an e-commerce site over from EE. Many of the products have a "product image". Many do not, but we have an assets directory full of images that match their SKUs.
When listing the products by category, I want to call images in the following order: 1. If there's a product image, crop and display the product image. 2. Else if there's an asset filename that matches the product SKU, crop and display that asset. 3. Else, display a stock image that says no image available.
I'm running in to a problem with step 2. I can display the product SKU in lowercase in the template, but I don't know how to make the asset query using the product SKU.
How can I get the results of productPic in to the assets query below it?
{% for rental in rentals %}
<li class="cell text-center">
<a href="{{ rental.url }}">
{# If there's a product image, crop and display the product image.#}
{% if rental.productImage|length %}
{% set image = rental.productImage.one() %}
<img src="{{ image.getUrl(thumb) }}" alt="{{ rental.title }}" />
{# Elseif there's an asset filename that matches the product SKU, crop and display that asset. Using rental.title temporarily to trigger this conditional. #}
{% elseif rental.title|length %}
{% set variant = rental.defaultVariant %}
{% set productPic = variant.sku|lower %}
{{ productPic }}
{% set image = craft.assets.filename('').one() %}
<img src="{{ image.getUrl() }}" alt="No Image Available" />
{# Else, display a stock image that says no image available. #}
{% else %}
{% set image = craft.assets.id(9225).one() %}
<img src="{{ image.getUrl() }}" alt="No Image Available" />
{% endif %}
</a>
<br />
<a href="{{ rental.url }}">{{ rental.title }}</a>
</li>
{% endfor %}