Created
March 6, 2022 15:28
-
-
Save ig0r74/008833714e84c62882089fd6c8aee97f to your computer and use it in GitHub Desktop.
MODX Revolution newQuery groupby & SUM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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