Skip to content

Commit

Permalink
Hook on filterProductContent for adding ProductComments data
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 9, 2021
1 parent 2d28a22 commit 3b21137
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
25 changes: 24 additions & 1 deletion productcomments.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function install($keep = true)
!$this->registerHook('header') || //Adds css and javascript on front
!$this->registerHook('displayProductListReviews') || //Product list miniature
!$this->registerHook('displayProductAdditionalInfo') || //Display info in checkout column

!$this->registerHook('filterProductContent') || // Add infos to Product page
!$this->registerHook('registerGDPRConsent') ||
!$this->registerHook('actionDeleteGDPRCustomer') ||
!$this->registerHook('actionExportGDPRData') ||
Expand Down Expand Up @@ -896,6 +896,29 @@ public function hookDisplayFooterProduct($params)
return $this->renderProductCommentsList($params['product']) . $this->renderProductCommentModal($params['product']);
}

/**
* Inject data about productcomments in the product object for frontoffice
*
* @param array $params
* @return void
*/
public function hookFilterProductContent(array $params): void
{
if (empty($params['object']->id)) {
return;
}
/** @var ProductCommentRepository $productCommentRepository */
$productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository');

$averageRating = $productCommentRepository->getAverageGrade($params['object']->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'));
$commentsNb = $productCommentRepository->getCommentsNumber($params['object']->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'));

$params['object']->productComments = [
'averageRating' => $averageRating,
'numComments' => $commentsNb,
];
}

/**
* Used to render the product comments list
*
Expand Down
33 changes: 33 additions & 0 deletions upgrade/install-4.2.3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}

function upgrade_module_4_2_3($object)
{
return $object->registerHook('filterProductContent');
}

0 comments on commit 3b21137

Please sign in to comment.