1

The cart of Craft Commerce can't really be cached so I try to increase performance by loading product images in the cart via eager-loading.

All tutorials tell me to use the .with(['variantThumb']). But .with can only be used on the craft.entries().section and not on simple loops, I don't understand how to do this.

The stuff in the cart is available via the cart handle:

{% for item in cart.lineItems %}
    {% set cartThumb = item.purchasable.variantThumb.one() %}
    {{ macro.checkoutThumb(cartThumb) }}
{% endfor %}

I don't understand how to use eager-loading in this case.

1 Answer 1

1

You can use something like this

{% for item in cart.lineItems %}
    {% set product = craft.products().hasVariant(craft.variants().id(item.purchasableId)).with([['variantThumb', { withTransforms: 'cartThumb'}]]).one() %}
    {% set cartThumb = product.variantThumb[0] %}
    {{ macro.checkoutThumb(cartThumb) }}
{% endfor %}

I am not sure if there will be any significant change on the amount of the queries thought.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.