1

Folks:

I'm using CraftCommerce. I have a main product and several accessories related to that product. Accessories have a title, description, price, and image.

Now, I have no problem pulling the title and description for the related accessories using this:

    {% for relatedProducts in product.relatedAccessories %}
       {{ relatedProducts.title }} - {{ relatedProducts.accessoryDescription }}
    {% endfor %}

Where I've got a problem is pulling the related image. If I use {{ asset.getUrl }} it pulls the first image from the main product entry rather than the image in the related entry. I have tried Get image from related entries asset field and How do I pull related entries and assets? without success.

Any assistance would be great.

2

Ok first thing take the s off relatedProduct because each loop is over one relatedProduct ;) - then you can do something like this:

{% for relatedProduct in product.relatedAccessories %}
   {{ relatedProduct.title }} - {{ relatedProduct.accessoryDescription }}
   Url: {{ relatedProduct.yourAssetHandle.first().url }}
{% endfor %}

You should probably protect that by first checking {% if relatedProduct.yourAssetHandle.first()|length %} unless you can be completely certain you will have an asset (required field for example).

| improve this answer | |
  • Perfect! That solved it. And a few other things for me to boot. – Wonko Mar 4 '16 at 15:43
  • Glad to hear it - please accept it as an answer as I am shallow enough to desire points ;) – Jeremy Daalder Mar 4 '16 at 21:36

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.