From 80f47b2dc299774c517f870b07f495b4784d42b8 Mon Sep 17 00:00:00 2001 From: mgielecinski Date: Mon, 26 Jan 2026 11:50:20 +0100 Subject: [PATCH 1/2] add grade summary --- productcomments.php | 17 ++++++++++++ src/Repository/ProductCommentRepository.php | 30 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/productcomments.php b/productcomments.php index 851d063..7ba34c3 100644 --- a/productcomments.php +++ b/productcomments.php @@ -974,8 +974,24 @@ private function renderProductCommentsList($product) $commentRepository = $this->get('product_comment_repository'); $averageGrade = $commentRepository->getAverageGrade($product->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')); $commentsNb = $commentRepository->getCommentsNumber($product->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')); + $summaryGrades = $commentRepository->getSummaryGrades($product->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')); $isPostAllowed = $commentRepository->isPostAllowed($product->id, (int) $this->context->cookie->id_customer, (int) $this->context->cookie->id_guest); + $summary = [ + 5 => ['count' => 0, 'percent' => 0], + 4 => ['count' => 0, 'percent' => 0], + 3 => ['count' => 0, 'percent' => 0], + 2 => ['count' => 0, 'percent' => 0], + 1 => ['count' => 0, 'percent' => 0], + ]; + + foreach($summaryGrades as $grade) { + $summary[(int) $grade['grade']] = [ + 'count' => (int) $grade['count'], + 'percent' => (100 / $commentsNb) * (int) $grade['count'] + ]; + } + /* configure pagination */ $commentsTotalPages = 0; $commentsPerPage = (int) Configuration::get('PRODUCT_COMMENTS_COMMENTS_PER_PAGE'); @@ -986,6 +1002,7 @@ private function renderProductCommentsList($product) $this->context->smarty->assign([ 'post_allowed' => $isPostAllowed, 'usefulness_enabled' => Configuration::get('PRODUCT_COMMENTS_USEFULNESS'), + 'summary' => $summary, 'average_grade' => $averageGrade, 'nb_comments' => $commentsNb, 'list_comments_url' => $this->context->link->getModuleLink( diff --git a/src/Repository/ProductCommentRepository.php b/src/Repository/ProductCommentRepository.php index 88db968..8904c2b 100644 --- a/src/Repository/ProductCommentRepository.php +++ b/src/Repository/ProductCommentRepository.php @@ -219,6 +219,36 @@ public function getAverageGrade($productId, $validatedOnly) return (float) $qb->execute()->fetch(\PDO::FETCH_COLUMN); } + /** + * @param int $productId + * @param bool $validatedOnly + * + * @return array + */ + public function getSummaryGrades($productId, $validatedOnly) + { + /** @var QueryBuilder $qb */ + $qb = $this->connection->createQueryBuilder(); + $qb + ->select('pc.grade, COUNT(*) as count') + ->from($this->databasePrefix . 'product_comment', 'pc') + ->andWhere('pc.id_product = :id_product') + ->andWhere('pc.deleted = :deleted') + ->setParameter('deleted', 0) + ->setParameter('id_product', $productId) + ->groupBy('pc.grade') + ; + + if ($validatedOnly) { + $qb + ->andWhere('pc.validate = :validate') + ->setParameter('validate', 1) + ; + } + + return $qb->execute()->fetchAll(); + } + /** * @param int $langId * @param int $shopId From abd16e4686e77fbf61b69088b5ca30d7e5ca5b0b Mon Sep 17 00:00:00 2001 From: mgielecinski Date: Mon, 26 Jan 2026 11:55:42 +0100 Subject: [PATCH 2/2] cs fix --- productcomments.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/productcomments.php b/productcomments.php index 7ba34c3..33c1a65 100644 --- a/productcomments.php +++ b/productcomments.php @@ -984,11 +984,11 @@ private function renderProductCommentsList($product) 2 => ['count' => 0, 'percent' => 0], 1 => ['count' => 0, 'percent' => 0], ]; - - foreach($summaryGrades as $grade) { + + foreach ($summaryGrades as $grade) { $summary[(int) $grade['grade']] = [ 'count' => (int) $grade['count'], - 'percent' => (100 / $commentsNb) * (int) $grade['count'] + 'percent' => (100 / $commentsNb) * (int) $grade['count'], ]; }