counts

HTML

<span id="count">6</span>
<button class="btn">Увеличить</button>

JavaScript

<?php
if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {return;}
$action = $_POST['action'];
if (empty($action)) {return;}
$res = '';
switch ($action) {
  case 'countButton':
    $resource = $modx->getObject('modResource', $modx->resource->id);
    $parentID = $modx->resource->getTVValue('parentID');
    $parent = $modx->getObject('modResource', $parentID);
    
    $grandParentID = $parent->resource->getTVValue('parentID');
    $grandParent = $modx->getObject('modResource', $grandParentID);
    
    // Получаем предыдущее значение счетчика
    $votes = $resource->getTVValue('votes') + 1;
    $points = $resource->getTVValue('points') + $_POST['but'];
    $rating = $points / $votes;
    $parentVotes = $parent->getTVValue('votes') + 1;
    $parentPoints = $parent->getTVValue('points') + $_POST['but'];
    $parentRating = $parentPoints / $parentVotes;
    
    $grandParentVotes = $grandParent->getTVValue('votes') + 1;
    $grandParentPoints = $grandParent->getTVValue('points') + $_POST['but'];
    $grandParentRating = $grandParentPoints / $grandParentVotes;
    
    // Устанавливаем новое значение
    $resource->setTVValue('votes',$votes);
    $resource->setTVValue('points',$points);
    $resource->setTVValue('rating',round($rating,2));
    $parent->setTVValue('votes',$parentVotes);
    $parent->setTVValue('points',$parentPoints);
    $parent->setTVValue('rating',round($parentRating,2));
    
    $grandParent->setTVValue('votes',$grandParentVotes);
    $grandParent->setTVValue('points',$grandParentPoints);
    $grandParent->setTVValue('rating',round($grandParentRating,2));
    
    $resource->save();
    $parent->save();
    $grandParent->save();
    // Очищаем кеш ресурса, чтобы изменения отобразились сразу
    $resource->_contextKey = $resource->get('context_key');
    $cache = $modx->cacheManager->getCacheProvider(
             $modx->getOption('cache_resource_key', null, 'resource'));
    $key = $resource->getCacheKey();
    $cache->delete($key,...