Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setTotalReviews example
$reviewCriteria
=
$this
->
createReviewCriteria
(
$request
,
$context
)
;
$reviews
=
$this
->productReviewRoute
->
load
(
$product
->
getParentId
(
)
??
$product
->
getId
(
)
,
$request
,
$context
,
$reviewCriteria
)
->
getResult
(
)
;
$matrix
=
$this
->
getReviewRatingMatrix
(
$reviews
)
;
$reviewResult
= ProductReviewResult::
createFrom
(
$reviews
)
;
$reviewResult
->
setMatrix
(
$matrix
)
;
$reviewResult
->
setProductId
(
$product
->
getId
(
)
)
;
$reviewResult
->
setCustomerReview
(
$this
->
getCustomerReview
(
$product
->
getId
(
)
,
$context
)
)
;
$reviewResult
->
setTotalReviews
(
$matrix
->
getTotalReviewCount
(
)
)
;
$reviewResult
->
setProductId
(
$product
->
getId
(
)
)
;
$reviewResult
->
setParentId
(
$product
->
getParentId
(
)
??
$product
->
getId
(
)
)
;
return
$reviewResult
;
}
private
function
createReviewCriteria
(
Request
$request
, SalesChannelContext
$context
)
: Criteria
{
$limit
=
(int)
$request
->
get
(
'limit', self::LIMIT
)
;
$page
=
(int)
$request
->
get
(
'p', self::DEFAULT_PAGE
)
;
$offset
=
$limit
*
(
$page
- 1
)
;
if
(
$productConfig
->
isStatic
(
)
)
{
$product
=
$this
->
getSlotProduct
(
$slot
,
$result
,
$productConfig
->
getStringValue
(
)
)
;
}
/** @var SalesChannelProductEntity|null $product */
if
(
$product
!== null
)
{
$buyBox
->
setProduct
(
$product
)
;
$buyBox
->
setProductId
(
$product
->
getId
(
)
)
;
$buyBox
->
setConfiguratorSettings
(
$this
->configuratorLoader->
load
(
$product
,
$resolverContext
->
getSalesChannelContext
(
)
)
)
;
$buyBox
->
setTotalReviews
(
$this
->
getReviewsCount
(
$product
,
$resolverContext
->
getSalesChannelContext
(
)
)
)
;
}
}
private
function
getReviewsCount
(
SalesChannelProductEntity
$product
, SalesChannelContext
$context
)
: int
{
$reviewCriteria
=
$this
->
createReviewCriteria
(
$context
,
$product
->
getParentId
(
)
??
$product
->
getId
(
)
)
;
$aggregation
=
$this
->repository->
aggregate
(
$reviewCriteria
,
$context
->
getContext
(
)
)
->
get
(
'review-count'
)
;
return
$aggregation
instanceof CountResult ?
$aggregation
->
getCount
(
)
: 0;
}
$reviewResult
->
setProductId
(
$request
->
get
(
'productId'
)
)
;
$reviewResult
->
setParentId
(
$request
->
get
(
'parentId'
)
)
;
$aggregation
=
$reviews
->
getAggregations
(
)
->
get
(
'ratingMatrix'
)
;
$matrix
=
new
RatingMatrix
(
[
]
)
;
if
(
$aggregation
instanceof TermsResult
)
{
$matrix
=
new
RatingMatrix
(
$aggregation
->
getBuckets
(
)
)
;
}
$reviewResult
->
setMatrix
(
$matrix
)
;
$reviewResult
->
setCustomerReview
(
$this
->
getCustomerReview
(
$productId
,
$context
)
)
;
$reviewResult
->
setTotalReviews
(
$matrix
->
getTotalReviewCount
(
)
)
;
return
$reviewResult
;
}
private
function
createCriteria
(
Request
$request
, SalesChannelContext
$context
)
: Criteria
{
$limit
=
(int)
$request
->
get
(
'limit', self::LIMIT
)
;
$page
=
(int)
$request
->
get
(
'p', self::DEFAULT_PAGE
)
;
$offset
=
$limit
*
(
$page
- 1
)
;
$criteria
=
new
Criteria
(
)
;