1

I have some Matrix blocks where I need to sum one of the fields within, but I only want to sum it when another field within that block is or is not empty.

{% set reimbursementAmount = craft.matrixBlocks()
  .relatedTo(cat)
  .fieldId(9)
  .sum('field_item_reimbursementAmount')
  .specialCase(':empty:')
  %} 

With this, I get an error:

Impossible to invoke a method ("specialCase") on a string variable ("101.00").

How do I sum the desired field, but only where another field (specialCase) is empty (or is not empty, as the case may be).

1 Answer 1

1

I haven't tested this, but I think .sum() will execute your query, so put it at the end:

{% set reimbursementAmount = craft.matrixBlocks()
  .relatedTo(cat)
  .fieldId(9)
  .specialCase(':empty:')
  .sum('field_item_reimbursementAmount')
%} 
1
  • Thanks, ended up using a Twig loop to SUM the values.
    – 4midori
    Commented Sep 4, 2020 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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