Skip to content

Commit 6488645

Browse files
authored
ENGCOM-5199: [Backport] Don't load product collection in review observer #23094
2 parents 7ac9e57 + d505ba2 commit 6488645

File tree

15 files changed

+300
-201
lines changed

15 files changed

+300
-201
lines changed

app/code/Magento/Review/Block/Customer/View.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Review\Block\Customer;
78

89
use Magento\Catalog\Model\Product;
@@ -91,6 +92,7 @@ public function __construct(
9192
* Initialize review id
9293
*
9394
* @return void
95+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
9496
*/
9597
protected function _construct()
9698
{
@@ -160,6 +162,7 @@ public function getRating()
160162
/**
161163
* Get rating summary
162164
*
165+
* @deprecated
163166
* @return array
164167
*/
165168
public function getRatingSummary()
@@ -201,26 +204,7 @@ public function dateFormat($date)
201204
}
202205

203206
/**
204-
* Get product reviews summary
205-
*
206-
* @param \Magento\Catalog\Model\Product $product
207-
* @param bool $templateType
208-
* @param bool $displayIfNoReviews
209-
* @return string
210-
*/
211-
public function getReviewsSummaryHtml(
212-
\Magento\Catalog\Model\Product $product,
213-
$templateType = false,
214-
$displayIfNoReviews = false
215-
) {
216-
if (!$product->getRatingSummary()) {
217-
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
218-
}
219-
return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
220-
}
221-
222-
/**
223-
* @return string
207+
* @inheritDoc
224208
*/
225209
protected function _toHtml()
226210
{

app/code/Magento/Review/Block/Product/Compare/ListCompare/Plugin/Review.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Review\Block\Product;
910

1011
use Magento\Catalog\Block\Product\ReviewRendererInterface;
1112
use Magento\Catalog\Model\Product;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Review\Model\ReviewSummaryFactory;
1215
use Magento\Review\Observer\PredispatchReviewObserver;
1316

1417
/**
@@ -33,17 +36,26 @@ class ReviewRenderer extends \Magento\Framework\View\Element\Template implements
3336
*/
3437
protected $_reviewFactory;
3538

39+
/**
40+
* @var ReviewSummaryFactory
41+
*/
42+
private $reviewSummaryFactory;
43+
3644
/**
3745
* @param \Magento\Framework\View\Element\Template\Context $context
3846
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
3947
* @param array $data
48+
* @param ReviewSummaryFactory $reviewSummaryFactory
4049
*/
4150
public function __construct(
4251
\Magento\Framework\View\Element\Template\Context $context,
4352
\Magento\Review\Model\ReviewFactory $reviewFactory,
44-
array $data = []
53+
array $data = [],
54+
ReviewSummaryFactory $reviewSummaryFactory = null
4555
) {
4656
$this->_reviewFactory = $reviewFactory;
57+
$this->reviewSummaryFactory = $reviewSummaryFactory ??
58+
ObjectManager::getInstance()->get(ReviewSummaryFactory::class);
4759
parent::__construct($context, $data);
4860
}
4961

@@ -52,7 +64,7 @@ public function __construct(
5264
*
5365
* @return string
5466
*/
55-
public function isReviewEnabled() : string
67+
public function isReviewEnabled(): string
5668
{
5769
return $this->_scopeConfig->getValue(
5870
PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
@@ -68,17 +80,22 @@ public function isReviewEnabled() : string
6880
* @param bool $displayIfNoReviews
6981
*
7082
* @return string
83+
* @throws \Magento\Framework\Exception\LocalizedException
84+
* @throws \Magento\Framework\Exception\NoSuchEntityException
7185
*/
7286
public function getReviewsSummaryHtml(
7387
\Magento\Catalog\Model\Product $product,
7488
$templateType = self::DEFAULT_VIEW,
7589
$displayIfNoReviews = false
7690
) {
77-
if (!$product->getRatingSummary()) {
78-
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
91+
if ($product->getRatingSummary() === null) {
92+
$this->reviewSummaryFactory->create()->appendSummaryDataToObject(
93+
$product,
94+
$this->_storeManager->getStore()->getId()
95+
);
7996
}
8097

81-
if (!$product->getRatingSummary() && !$displayIfNoReviews) {
98+
if (null === $product->getRatingSummary() && !$displayIfNoReviews) {
8299
return '';
83100
}
84101
// pick template among available
@@ -101,7 +118,7 @@ public function getReviewsSummaryHtml(
101118
*/
102119
public function getRatingSummary()
103120
{
104-
return $this->getProduct()->getRatingSummary()->getRatingSummary();
121+
return $this->getProduct()->getRatingSummary();
105122
}
106123

107124
/**
@@ -111,7 +128,7 @@ public function getRatingSummary()
111128
*/
112129
public function getReviewsCount()
113130
{
114-
return $this->getProduct()->getRatingSummary()->getReviewsCount();
131+
return $this->getProduct()->getReviewsCount();
115132
}
116133

117134
/**

app/code/Magento/Review/Block/View.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function getRating()
119119
/**
120120
* Retrieve rating summary for current product
121121
*
122+
* @deprecated
122123
* @return string
123124
*/
124125
public function getRatingSummary()
@@ -160,23 +161,4 @@ public function dateFormat($date)
160161
{
161162
return $this->formatDate($date, \IntlDateFormatter::LONG);
162163
}
163-
164-
/**
165-
* Get product reviews summary
166-
*
167-
* @param \Magento\Catalog\Model\Product $product
168-
* @param bool $templateType
169-
* @param bool $displayIfNoReviews
170-
* @return string
171-
*/
172-
public function getReviewsSummaryHtml(
173-
\Magento\Catalog\Model\Product $product,
174-
$templateType = false,
175-
$displayIfNoReviews = false
176-
) {
177-
if (!$product->getRatingSummary()) {
178-
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
179-
}
180-
return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
181-
}
182164
}

app/code/Magento/Review/Model/ResourceModel/Review/Summary.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Review\Model\ResourceModel\Review;
78

89
use Magento\Framework\Model\AbstractModel;
@@ -73,4 +74,46 @@ public function reAggregate($summary)
7374
}
7475
return $this;
7576
}
77+
78+
/**
79+
* Append review summary fields to product collection
80+
*
81+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
82+
* @param string $storeId
83+
* @param string $entityCode
84+
* @return Summary
85+
* @throws \Magento\Framework\Exception\LocalizedException
86+
*/
87+
public function appendSummaryFieldsToCollection(
88+
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
89+
string $storeId,
90+
string $entityCode
91+
) {
92+
if (!$productCollection->isLoaded()) {
93+
$summaryEntitySubSelect = $this->getConnection()->select();
94+
$summaryEntitySubSelect
95+
->from(
96+
['review_entity' => $this->getTable('review_entity')],
97+
['entity_id']
98+
)->where(
99+
'entity_code = ?',
100+
$entityCode
101+
);
102+
$joinCond = new \Zend_Db_Expr(
103+
"e.entity_id = review_summary.entity_pk_value AND review_summary.store_id = {$storeId}"
104+
. " AND review_summary.entity_type = ({$summaryEntitySubSelect})"
105+
);
106+
$productCollection->getSelect()
107+
->joinLeft(
108+
['review_summary' => $this->getMainTable()],
109+
$joinCond,
110+
[
111+
'reviews_count' => new \Zend_Db_Expr("IFNULL(review_summary.reviews_count, 0)"),
112+
'rating_summary' => new \Zend_Db_Expr("IFNULL(review_summary.rating_summary, 0)")
113+
]
114+
);
115+
}
116+
117+
return $this;
118+
}
76119
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Review\Model;
78

89
use Magento\Framework\DataObject;
@@ -100,6 +101,7 @@ class Review extends \Magento\Framework\Model\AbstractModel implements IdentityI
100101
/**
101102
* Review model summary
102103
*
104+
* @deprecated Summary factory injected as separate property
103105
* @var \Magento\Review\Model\Review\Summary
104106
*/
105107
protected $_reviewSummary;
@@ -214,6 +216,7 @@ public function aggregate()
214216
/**
215217
* Get entity summary
216218
*
219+
* @deprecated
217220
* @param Product $product
218221
* @param int $storeId
219222
* @return void
@@ -301,10 +304,12 @@ public function afterDeleteCommit()
301304
}
302305

303306
/**
304-
* Append review summary to product collection
307+
* Append review summary data object to product collection
305308
*
309+
* @deprecated
306310
* @param ProductCollection $collection
307311
* @return $this
312+
* @throws \Magento\Framework\Exception\NoSuchEntityException
308313
*/
309314
public function appendSummary($collection)
310315
{
@@ -313,7 +318,7 @@ public function appendSummary($collection)
313318
$entityIds[] = $item->getEntityId();
314319
}
315320

316-
if (sizeof($entityIds) == 0) {
321+
if (count($entityIds) === 0) {
317322
return $this;
318323
}
319324

@@ -356,7 +361,7 @@ public function isAvailableOnStore($store = null)
356361
{
357362
$store = $this->_storeManager->getStore($store);
358363
if ($store) {
359-
return in_array($store->getId(), (array) $this->getStores());
364+
return in_array($store->getId(), (array)$this->getStores());
360365
}
361366
return false;
362367
}

0 commit comments

Comments
 (0)