1

I actually found this code on here Search Form with Craft Commerce. I had a problem with this code pulling up products that didn't match, and then after creating a new product it worked for a bit and then broke and continued to only pull up the same ones, even if I got rid the values inside with[].

So two questions, how do I enforce that the product has to match BOTH criteria in the with[] and does anyone have any clue about why it would stop refreshing products? I've tried clearing cache in both browser and craftcms.

I apologize if this has been asked before, I've been through multiple previously asked questions with little luck.

{% set spec = craft.request.getParam('a') %}
{% set spec2 = craft.request.getParam('b') %}
{% set category = craft.categories.group('productSpecs') %}
{% set products = craft.commerce.products({
            relatedTo: category,
             with: [
                spec,
                spec2,
            ], 
            order: "title asc",
        }) %}
{% for product in products %}
    {{ product.title }}
{% endfor %}
3

I think you're misunderstanding what with does. Or I am. Ha!

From what I understand with is for eager loading, but doesn't control anything in the relation itself. I don't think your relation makes any sense either because all you'll do is substitute a string into your spec variables.

Try something more like:

{% set products = craft.commerce.products({
    relatedTo: [
        'and',
        CONDITION1,
        CONDITION2
    ],
    with: [
        fieldYouWantToShow,
        otherFieldYouWantToShow,
    ], 
    order: "title asc",
}) %}

And your conditionals should be something like:

{ targetElement: category },
{ sourceElement: craft.entries.section('reviews').status(null) }

The bottom example here is what you're needing: https://craftcms.com/docs/relations

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.