Skip to content

Instantly share code, notes, and snippets.

@Sentinel-7
Created October 14, 2021 14:37
Show Gist options
  • Save Sentinel-7/144e99478caf546a027fbf64eae0121b to your computer and use it in GitHub Desktop.
Save Sentinel-7/144e99478caf546a027fbf64eae0121b to your computer and use it in GitHub Desktop.
Считаем количество товаров в категории
<?php
if (empty($parent)) {$parent = $id;}
$pids = array_merge(array($parent), $modx->getChildIds($parent));
$ids = array();
$q = $modx->newQuery('msProduct');
$q->where(array('class_key' => 'msProduct','parent:IN' => $pids,'published' => 1,'deleted' => 0));
$q->select('`msProduct`.`id`');
if ($q->prepare() && $q->stmt->execute()) {
$ids = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
}
$q = $modx->newQuery('msProduct');
$q->leftJoin('msCategoryMember', 'Member', '`Member`.`product_id` = `msProduct`.`id`');
$q->where(array('class_key' => 'msProduct','Member.category_id:IN' => $pids,'published' => 1,'deleted' => 0));
$q->select('`msProduct`.`id`');
if ($q->prepare() && $q->stmt->execute()) {
$ids2 = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($ids2)) {
$ids = array_unique(array_merge($ids, $ids2));
}
}
return count($ids);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment