Skip to content

Commit e467331

Browse files
committed
MC-36149: [CLOUD] 'Be the first to review this product' link is not changed after admin has approved the review
1 parent 63434d2 commit e467331

File tree

3 files changed

+98
-19
lines changed

3 files changed

+98
-19
lines changed

app/code/Magento/Review/Block/Product/ReviewRenderer.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Review renderer
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
@@ -11,13 +9,21 @@
119
use Magento\Catalog\Block\Product\ReviewRendererInterface;
1210
use Magento\Catalog\Model\Product;
1311
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\View\Element\Template;
15+
use Magento\Framework\View\Element\Template\Context;
16+
use Magento\Review\Model\AppendSummaryDataToObjectByEntityCodeFactory;
17+
use Magento\Review\Model\Review;
18+
use Magento\Review\Model\ReviewFactory;
1419
use Magento\Review\Model\ReviewSummaryFactory;
1520
use Magento\Review\Observer\PredispatchReviewObserver;
21+
use Magento\Store\Model\ScopeInterface;
1622

1723
/**
18-
* Class ReviewRenderer
24+
* Review renderer
1925
*/
20-
class ReviewRenderer extends \Magento\Framework\View\Element\Template implements ReviewRendererInterface
26+
class ReviewRenderer extends Template implements ReviewRendererInterface
2127
{
2228
/**
2329
* Array of available template name
@@ -32,7 +38,7 @@ class ReviewRenderer extends \Magento\Framework\View\Element\Template implements
3238
/**
3339
* Review model factory
3440
*
35-
* @var \Magento\Review\Model\ReviewFactory
41+
* @var ReviewFactory
3642
*/
3743
protected $_reviewFactory;
3844

@@ -42,20 +48,29 @@ class ReviewRenderer extends \Magento\Framework\View\Element\Template implements
4248
private $reviewSummaryFactory;
4349

4450
/**
45-
* @param \Magento\Framework\View\Element\Template\Context $context
46-
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
51+
* @var AppendSummaryDataToObjectByEntityCodeFactory
52+
*/
53+
private $appendSummaryDataToObjectByEntityCodeFactory;
54+
55+
/**
56+
* @param Context $context
57+
* @param ReviewFactory $reviewFactory
4758
* @param array $data
48-
* @param ReviewSummaryFactory $reviewSummaryFactory
59+
* @param ReviewSummaryFactory|null $reviewSummaryFactory
60+
* @param AppendSummaryDataToObjectByEntityCodeFactory|null $appendSummaryDataToObjectByEntityCodeFactory
4961
*/
5062
public function __construct(
51-
\Magento\Framework\View\Element\Template\Context $context,
52-
\Magento\Review\Model\ReviewFactory $reviewFactory,
63+
Context $context,
64+
ReviewFactory $reviewFactory,
5365
array $data = [],
54-
ReviewSummaryFactory $reviewSummaryFactory = null
66+
ReviewSummaryFactory $reviewSummaryFactory = null,
67+
AppendSummaryDataToObjectByEntityCodeFactory $appendSummaryDataToObjectByEntityCodeFactory = null
5568
) {
5669
$this->_reviewFactory = $reviewFactory;
5770
$this->reviewSummaryFactory = $reviewSummaryFactory ??
5871
ObjectManager::getInstance()->get(ReviewSummaryFactory::class);
72+
$this->appendSummaryDataToObjectByEntityCodeFactory = $appendSummaryDataToObjectByEntityCodeFactory ??
73+
ObjectManager::getInstance()->get(AppendSummaryDataToObjectByEntityCodeFactory::class);
5974
parent::__construct($context, $data);
6075
}
6176

@@ -68,7 +83,7 @@ public function isReviewEnabled(): string
6883
{
6984
return $this->_scopeConfig->getValue(
7085
PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
71-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
86+
ScopeInterface::SCOPE_STORE
7287
);
7388
}
7489

@@ -80,19 +95,21 @@ public function isReviewEnabled(): string
8095
* @param bool $displayIfNoReviews
8196
*
8297
* @return string
83-
* @throws \Magento\Framework\Exception\LocalizedException
84-
* @throws \Magento\Framework\Exception\NoSuchEntityException
98+
* @throws LocalizedException
99+
* @throws NoSuchEntityException
85100
*/
86101
public function getReviewsSummaryHtml(
87-
\Magento\Catalog\Model\Product $product,
102+
Product $product,
88103
$templateType = self::DEFAULT_VIEW,
89104
$displayIfNoReviews = false
90105
) {
91106
if ($product->getRatingSummary() === null) {
92-
$this->reviewSummaryFactory->create()->appendSummaryDataToObject(
93-
$product,
94-
$this->_storeManager->getStore()->getId()
95-
);
107+
$this->appendSummaryDataToObjectByEntityCodeFactory->create()
108+
->execute(
109+
$product,
110+
$this->_storeManager->getStore()->getId(),
111+
Review::ENTITY_PRODUCT_CODE
112+
);
96113
}
97114

98115
if (null === $product->getRatingSummary() && !$displayIfNoReviews) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Review\Model;
9+
10+
use Magento\Framework\Model\AbstractModel;
11+
use Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory as SummaryCollectionFactory;
12+
13+
/**
14+
* Add review summary data to object by its entity code
15+
*/
16+
class AppendSummaryDataToObjectByEntityCode
17+
{
18+
/**
19+
* @var SummaryCollectionFactory
20+
*/
21+
private $summaryCollectionFactory;
22+
23+
/**
24+
* @param SummaryCollectionFactory $summaryCollectionFactory
25+
*/
26+
public function __construct(
27+
SummaryCollectionFactory $summaryCollectionFactory
28+
) {
29+
$this->summaryCollectionFactory = $summaryCollectionFactory;
30+
}
31+
32+
/**
33+
* @param AbstractModel $object
34+
* @param int $storeId
35+
* @param string $entityCode
36+
* @retrun void
37+
*/
38+
public function execute(AbstractModel $object, int $storeId, string $entityCode): void
39+
{
40+
$summaryCollection = $this->summaryCollectionFactory->create();
41+
$summaryCollection->addStoreFilter($storeId);
42+
$summaryCollection->getSelect()
43+
->joinLeft(
44+
['review_entity' => $summaryCollection->getResource()->getTable('review_entity')],
45+
'main_table.entity_type = review_entity.entity_id',
46+
'entity_code'
47+
)
48+
->where('entity_pk_value IN (?)', $object->getId())
49+
->where('entity_code = ?', $entityCode);
50+
$summaryItem = $summaryCollection->getFirstItem();
51+
52+
$object->addData(
53+
[
54+
'reviews_count' => $summaryItem->getData('reviews_count'),
55+
'rating_summary' => $summaryItem->getData('rating_summary'),
56+
]
57+
);
58+
}
59+
}

app/code/Magento/Review/Model/ReviewSummary.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
/**
1414
* ReviewSummary model.
15+
*
16+
* @deprecated Filtering collection by entity_type ID leads to wrong result if AUTO_INCREMENT begins not form 1.
17+
* @see \Magento\Review\Model\AppendSummaryDataToObjectByEntityCode
1518
*/
1619
class ReviewSummary
1720
{

0 commit comments

Comments
 (0)