Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setProductId example
private
function
loadProductReviews
(
SalesChannelProductEntity
$product
, Request
$request
, SalesChannelContext
$context
)
: ProductReviewResult
{
$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
)
;
$productConfig
=
$slot
->
getFieldConfig
(
)
->
get
(
'product'
)
;
if
(
$productConfig
=== null ||
$productConfig
->
getValue
(
)
=== null
)
{
return
;
}
if
(
$resolverContext
instanceof EntityResolverContext &&
$productConfig
->
isMapped
(
)
)
{
/** @var SalesChannelProductEntity $product */
$product
=
$this
->
resolveEntityValue
(
$resolverContext
->
getEntity
(
)
,
$productConfig
->
getStringValue
(
)
)
;
$productBox
->
setProduct
(
$product
)
;
$productBox
->
setProductId
(
$product
->
getId
(
)
)
;
}
if
(
$productConfig
->
isStatic
(
)
)
{
$this
->
resolveProductFromRemote
(
$slot
,
$productBox
,
$result
,
$productConfig
->
getStringValue
(
)
,
$resolverContext
->
getSalesChannelContext
(
)
)
;
}
}
private
function
resolveProductFromRemote
(
CmsSlotEntity
$slot
,
ProductBoxStruct
$productBox
,
ElementDataCollection
$result
,
$payload
=
$this
->loader->
load
(
$lineItems
, Context::
createDefaultContext
(
)
)
;
static
::
assertEquals
(
[
]
,
$payload
)
;
}
public
function
testNoPayloadContinue
(
)
: void
{
$productDownload
=
new
ProductDownloadEntity
(
)
;
$productDownload
->
setId
(
Uuid::
randomHex
(
)
)
;
$productDownload
->
setProductId
(
Uuid::
randomHex
(
)
)
;
$entitySearchResult
=
$this
->
createMock
(
EntitySearchResult::
class
)
;
$entitySearchResult
->
method
(
'getEntities'
)
->
willReturn
(
new
EntityCollection
(
[
$productDownload
]
)
)
;
$this
->productDownloadRepository
->
expects
(
static
::
once
(
)
)
->
method
(
'search'
)
->
willReturn
(
$entitySearchResult
)
;
$lineItems
=
[
[
'id' => Uuid::
randomHex
(
)
,
$criteria
=
$this
->
createCriteria
(
$request
,
$context
)
;
$reviews
=
$this
->route
->
load
(
$productId
,
$request
,
$context
,
$criteria
)
->
getResult
(
)
;
$reviews
= StorefrontSearchResult::
createFrom
(
$reviews
)
;
$this
->eventDispatcher->
dispatch
(
new
ProductReviewsLoadedEvent
(
$reviews
,
$context
,
$request
)
)
;
$reviewResult
= ReviewLoaderResult::
createFrom
(
$reviews
)
;
$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
(
)
)
;
if
(
$productConfig
->
isMapped
(
)
&&
$resolverContext
instanceof EntityResolverContext
)
{
$product
=
$this
->
resolveEntityValue
(
$resolverContext
->
getEntity
(
)
,
$productConfig
->
getStringValue
(
)
)
;
}
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'
)
;
$productDownload
=
new
ProductDownloadEntity
(
)
;
$productDownload
->
setId
(
Uuid::
randomHex
(
)
)
;
$productDownload
->
setMediaId
(
Uuid::
randomHex
(
)
)
;
$productDownload
->
setPosition
(
0
)
;
$productDownloadRepository
=
$this
->
createMock
(
EntityRepository::
class
)
;
$productDownloadRepository
->
method
(
'search'
)
->
willReturnCallback
(
function
DCriteria
$criteria
)
use
(
$productDownload
)
: EntitySearchResult
{
$filters
=
$criteria
->
getFilters
(
)
;
if
(
isset
(
$filters
[
0
]
)
&&
$filters
[
0
]
instanceof EqualsAnyFilter
)
{
$value
= ReflectionHelper::
getPropertyValue
(
$filters
[
0
]
, 'value'
)
;
$productDownload
->
setProductId
(
$value
[
0
]
?? null
)
;
}
return
new
EntitySearchResult
(
'productDownload',
1,
new
EntityCollection
(
[
$productDownload
]
)
,
null,
new
Criteria
(
)
,
Context::
createDefaultContext
(
)
)
;
}
)
;