Last active
April 27, 2025 20:03
-
-
Save TITAN-UZ/9c2166a95ab7a8ee17cae0a927aed21b to your computer and use it in GitHub Desktop.
bCat
This file contains hidden or 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 | |
| // Получаем параметр vendor | |
| $vendor = (int) $modx->getOption('vendor', $scriptProperties, 0); | |
| // Если vendor не передан, возвращаем сообщение об ошибке | |
| if (empty($vendor)) { | |
| return 'Не указан vendor'; | |
| } | |
| // Ищем ID товаров по vendor через xPDO | |
| $c = $modx->newQuery('msProductData', ['vendor' => $vendor]); | |
| $c->select('id'); | |
| $productIds = $modx->getIterator('msProductData', $c); | |
| $ids = []; | |
| foreach ($productIds as $product) { | |
| $ids[] = $product->get('id'); | |
| } | |
| if (empty($ids)) { | |
| return 'Нет товаров с таким vendor'; | |
| } | |
| // Ищем родительские категории товаров | |
| $c = $modx->newQuery('modResource', [ | |
| 'id:IN' => $ids, | |
| 'class_key' => 'msProduct', | |
| 'published' => 1, | |
| 'deleted' => 0, | |
| ]); | |
| $c->select('parent'); | |
| $c->distinct(); | |
| $parents = $modx->getIterator('modResource', $c); | |
| $parentIds = []; | |
| foreach ($parents as $parent) { | |
| $parentIds[] = $parent->get('parent'); | |
| } | |
| $parentIds = array_unique($parentIds); | |
| if (empty($parentIds)) { | |
| return 'Нет категорий для товаров'; | |
| } | |
| // Теперь находим сами категории | |
| $criteria = [ | |
| 'id:IN' => $parentIds, | |
| 'class_key' => 'msCategory', | |
| 'published' => 1, | |
| 'deleted' => 0, | |
| ]; | |
| $categories = $modx->getCollection('modResource', $criteria); | |
| // Подготовка вывода | |
| $output = []; | |
| foreach ($categories as $category) { | |
| // Получаем TV поле imgm (изображение категории) | |
| $img = $category->getTVValue('imgm'); | |
| $title = $category->get('pagetitle'); | |
| $uri = $category->get('uri'); | |
| // Подготовка данных для чанка | |
| $categoryData = [ | |
| 'uri' => $uri, | |
| 'img' => $img, | |
| 'title' => $title, | |
| ]; | |
| // Проверяем, что данные правильно передаются в чанк | |
| if (!empty($img) && !empty($uri) && !empty($title)) { | |
| // Используем чанк для вывода категории | |
| $output[] = $modx->getChunk('categoryTpl', $categoryData); | |
| } else { | |
| // Выводим сообщение, если данных нет | |
| $output[] = 'Нет данных для категории'; | |
| } | |
| } | |
| // Если данные есть, выводим их | |
| if (!empty($output)) { | |
| return implode( $output); | |
| } else { | |
| return 'Нет категорий для отображения'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment