1

I'm trying to bring in recommended products for adding to a bigger purchase. I set up a simple matrix but can't seem to figure out how to pull product information. This is what I have so far, it throws no errors, but it also gives nothing.

{% for block in product.recommendProduct %}
    {% set i = i + 1 %}
       <div>
           {% set image = block.chooseProduct.mainProductImage.first() %}
            <img src="{{ image.getUrl('productListing') }}">
            <p>{{ block.chooseProduct.title }}</p>
       </div>
{% endfor %}
2

This should work:

{% set image = block.chooseProduct.first().mainProductImage.first() %}

Explanation: your field handle in your product is called recommendProduct (matrix) that contains a relation chooseProduct. Your block.chooseProduct is an object of type ElementCriteriaModel so in order to fetch the product you need to call the first() method (or something similar). So if you have multiple products in your chooseProduct field your code would look like this

{% for block in product.recommendProduct %}
{% set i = i + 1 %}
   <div>
       {% for relatedProduct in block.chooseProduct %}
            {% set image = relatedProduct.mainProductImage.first() %}
            <img src="{{ image.getUrl('productListing') }}">
            <p>{{ relatedProduct.title }}</p>
       {% endfor %} 
   </div>
{% endfor %}

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.