Blog Sections Open

Using a Counter Snippet Inside a Ditto Filter

Sometimes a listing should depend not on one resource field, but on a computed total derived from child resources. The original pattern behind this article used a small helper snippet and then fed its result into a Ditto

Sometimes a listing should depend not on one resource field, but on a computed total derived from child resources. The original pattern behind this article used a small helper snippet and then fed its result into a Ditto filter.

Example Counter

$docid = (isset($docid) && (int)$docid > 0) ? (int)$docid : $modx->documentIdentifier;
$children = $modx->getActiveChildren($docid, 'id');
$invent = 0;
foreach ($children as $value) {
    $vals = $modx->getTemplateVarOutput(array('inventory'), $value['id']);
    $val = (int) $vals['inventory'];
    if ($val < 0) {
        $val = 0;
    }
    $invent += $val;
}
return $invent;

Filter Use

&filter=`[!counter? &docid=`id`!],0,4`

The main takeaway is that listings can depend on computed business logic, not just raw fields. Once the expensive part is isolated in a helper snippet, Ditto can stay relatively clean.

Newer post

Using ditto_iteration Correctly with Conditional TV Output

Why conditional TV checks can throw off ditto_iteration output and how to keep item numbering aligned with what is actually rendered.

Older post

Working with Shopkeeper Checkbox Parameters

How to handle checkbox-style product parameters cleanly when Shopkeeper and shk_widget are part of the product form.