TransWikia.com

Problem with SUM on Matrix blocks

Craft CMS Asked on December 28, 2020

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).

One Answer

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')
%} 

Answered by James Smith on December 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP