1

I have a custom field enabled on a product - it is a user field that associates a product with a user.

What would the correct setup be to only display products that pertain to the currentUser - as this would match the custom user field?

1

Depends on whether the user field is on the product or a variant.

If the custom field is on the product:

{% set products = craft.commerce.products.relatedTo({
    sourceElement: currentUser,
    field: 'userFieldName'
}) %}

If the custom field is on the variant:

{% set variantCriteria = craft.commerce.variants.relatedTo({
    sourceElement: currentUser,
    field: 'userFieldName'
}) %}

{% set products = craft.commerce.products.hasVariant(variantCriteria) %}
  • The custom field is on the product. I should have added, that I previously attempted your first suggestion, but it didn't return any results. – JackO Feb 20 '17 at 15:18

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.