Display image asset of a variant Craft Commerce newbie question....

How do I display the image asset for a custom variant field?

I'd like to display the associated image of a product variant in the cart.
Ok so in general this depends on if the custom field is on the product or the variant.   Assume the filed is called masterImage either way.

If the field is on a variant:
{% set image = lineItem.purchasable.product.masterImage.first() %}

If the field is on the product:
{% set image = lineItem.purchasable.masterImage.first() %}

Remember (either way) you should always check there is an image first really:

{% if lineItem.purchasable.product.masterImage|length %} ... set the image

In practise I usually have code that falls back to the product image if the variant image is not set for whatever reason:

    {% set image = lineItem.purchasable.masterImage.first() ?? null %}
    {# Fallback on product image if there is no variant image set #}
    {% if not image|length %}
        {% set image = lineItem.purchasable.product.masterImage.first() ?? null %}
    {% endif %}


(Note here I am not doing the initial test on the asset because I am instead using the null coalescing operator)