Всем привет!
Долго бился с этой задачей, т.к. не являюсь глубоким экспертом в PHP.
Возможно кому-то сократит время для решения такой задачи, как подсчет кол-ва товаров в текущей группе со всеми модификациями.
Первый и не совсем правильный вариант, это решение через циклы, который при большом кол-ве товаров и их модификаций будет нагружать сервер, а то и "ложить" сайт:
if ($Shop_Controller_Show->group && !$Shop_Controller_Show->item)
{
$iCurrentShopGroupId = intval(is_array($Shop_Controller_Show->group) ? Core_Array::first($Shop_Controller_Show->group) : $Shop_Controller_Show->group);
$oShopGroup = Core_Entity::factory('Shop_Group', $iCurrentShopGroupId);
$aShopItems = $oShopGroup->Shop_Items->findAll();
$productsCount = 0;
$modificationsCount = 0;
foreach ($aShopItems as $oShopItem) {
if ($oShopItem->modification_id == 0 && $oShopItem->active) {
$productsCount++;
$aModifications = $oShopItem->Modifications->findAll();
foreach ($aModifications as $oModification) {
if ($oModification->active) {
$modificationsCount++;
}
}
}
}
$totalCount = $productsCount + $modificationsCount;
$Shop_Controller_Show
->addEntity(
Core::factory('Core_Xml_Entity')
->name('items_total_count_with_modifications')->value(
intval($totalCount)
)
);
}
Второй, написанный силами ChatGPT на базе указанного выше кода через циклы. После различных итераций и вариантов, получен готовый код от ChatGPT через queryBuilder(), он же более правильный при больших кол-вах товаров и их модификаций:
if ($Shop_Controller_Show->group && !$Shop_Controller_Show->item)
{
$iCurrentShopGroupId = intval(is_array($Shop_Controller_Show->group) ? Core_Array::first($Shop_Controller_Show->group) : $Shop_Controller_Show->group);
$productsCount = Core_Entity::factory('Shop_Item')
->queryBuilder()
->select([Core_QueryBuilder::expression('COUNT(*)'), 'count'])
->where('shop_group_id', '=', $iCurrentShopGroupId)
->where('modification_id', '=', 0)
->where('active', '=', 1)
->where('deleted', '=', 0)
->execute()
->asAssoc()
->current()['count'];
$modificationsCount = Core_Entity::factory('Shop_Item')
->queryBuilder()
->select([Core_QueryBuilder::expression('COUNT(DISTINCT modifications.id)'), 'count'])
->join(['shop_items', 'modifications'], 'modifications.modification_id', '=', 'shop_items.id')
->where('shop_items.shop_group_id', '=', $iCurrentShopGroupId)
->where('modifications.active', '=', 1)
->where('shop_items.active', '=', 1)
->where('shop_items.deleted', '=', 0)
->where('modifications.deleted', '=', 0)
->where('modifications.modification_id', '!=', 0)
->execute()
->asAssoc()
->current()['count'];
$totalCount = $productsCount + $modificationsCount;
$Shop_Controller_Show->addEntity(
Core::factory('Core_Xml_Entity')
->name('items_total_count_with_modifications')
->value(intval($totalCount))
);
}
Код для ТДС интернет-магазина, далее в XML появляется ветка items_total_count_with_modifications и её значение $totalCount.
Возможно разработчики HostCMS как-то дополнят это решение. Заранее благодарю!