<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=catcostpalns.market.tags.single.first
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

if(count($costplan['bundles']['custom']) > 0) {
  $popular_cust_tmp = array();

  $lastorders = $db->query("SELECT * FROM $db_market_orders WHERE order_status!='new' AND order_status!='cancel' AND order_pid=".$item['item_id'].' LIMIT 10')->fetchAll();

  foreach($lastorders as $lord) {
    $lordcostplan = (!empty($lord['order_catcostplans']) ? json_decode($lord['order_catcostplans'],1) : array());
    if(count($lordcostplan) > 0 && $lordcostplan['package'] == 'single' && count($lordcostplan['extras']['selected']) > 0) {
      foreach($costplan['bundles']['custom'] as $cust) {
        $popular_cust_tmp[$cust['id']] += $lordcostplan['extras']['selected'][$cust['id']]['count'];
      }

    }
  }
}
if (is_array($popular_cust_tmp) && count($popular_cust_tmp) > 0) {
    $popular_avg = array_sum($popular_cust_tmp) / count($popular_cust_tmp);
    foreach ($popular_cust_tmp as $li => $cnt) {
        if ($cnt > $popular_avg) {
            $popular_cust[] = $li;
        }
    }
} else {
    $popular_cust = []; // Если `$popular_cust_tmp` не массив, устанавливаем пустое значение
}

?>