Skip to content

Instantly share code, notes, and snippets.

@ig0r74
Created March 6, 2022 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ig0r74/008833714e84c62882089fd6c8aee97f to your computer and use it in GitHub Desktop.
Save ig0r74/008833714e84c62882089fd6c8aee97f to your computer and use it in GitHub Desktop.
MODX Revolution newQuery groupby & SUM
<?php
// ********************************* //
/** @var mindbox $mindbox */
$modx->loadClass('modMindbox', MODX_CORE_PATH . 'components/mindbox/model/', true, true);
$mindbox = new modMindbox($modx, []);
if (!$mindbox) {
return 'Could not load modMindbox class!';
}
$q = $modx->newQuery('msOrder', [
'id:>=' => 42446,
]);
$total = $modx->getCount('msOrder', $q);
$q->sortby('id', 'ASC');
$q->select('num, user_id, SUM(cost) as total_spent');
$q->groupby('user_id');
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['total_spent'] > 0) {
$percent = $mindbox->getCustomerSegmentation($row['user_id']);
usleep(50000);
$percent = $percent > 0 ? $percent : 3;
$bonuses = $row['total_spent'] / 100 * $percent;
print $row['user_id'] . " - " . $row['total_spent'] . " - $percent <br>";
if ($bonuses > 0) {
print $bonuses.'<br>';
// $mindbox->changeBalance($row['user_id'], $bonuses);
usleep(250000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment