1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Content\Product\Cms; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity; |
6
|
|
|
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection; |
7
|
|
|
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext; |
8
|
|
|
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext; |
9
|
|
|
use Shopware\Core\Content\Cms\SalesChannel\Struct\BuyBoxStruct; |
10
|
|
|
use Shopware\Core\Content\Product\SalesChannel\Detail\ProductConfiguratorLoader; |
11
|
|
|
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity; |
12
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; |
13
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation; |
14
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\CountResult; |
15
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
16
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; |
17
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter; |
18
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @internal (flag:FEATURE_NEXT_10078) |
22
|
|
|
*/ |
23
|
|
|
class BuyBoxCmsElementResolver extends AbstractProductDetailCmsElementResolver |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ProductConfiguratorLoader |
27
|
|
|
*/ |
28
|
|
|
private $configuratorLoader; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var EntityRepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $repository; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
ProductConfiguratorLoader $configuratorLoader, |
37
|
|
|
EntityRepositoryInterface $repository |
38
|
|
|
) { |
39
|
|
|
$this->configuratorLoader = $configuratorLoader; |
40
|
|
|
$this->repository = $repository; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getType(): string |
44
|
|
|
{ |
45
|
|
|
return 'buy-box'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void |
49
|
|
|
{ |
50
|
|
|
$buyBox = new BuyBoxStruct(); |
51
|
|
|
$slot->setData($buyBox); |
52
|
|
|
|
53
|
|
|
$config = $slot->getFieldConfig(); |
54
|
|
|
$productConfig = $config->get('product'); |
55
|
|
|
|
56
|
|
|
if ($productConfig === null) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$product = null; |
61
|
|
|
|
62
|
|
|
if ($productConfig->isMapped() && $resolverContext instanceof EntityResolverContext) { |
63
|
|
|
$product = $this->resolveEntityValue($resolverContext->getEntity(), $productConfig->getValue()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($productConfig->isStatic()) { |
67
|
|
|
$product = $this->getSlotProduct($slot, $result, $productConfig->getValue()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var SalesChannelProductEntity|null $product */ |
71
|
|
|
if ($product !== null) { |
72
|
|
|
$buyBox->setProduct($product); |
73
|
|
|
$buyBox->setProductId($product->getId()); |
74
|
|
|
$buyBox->setConfiguratorSettings($this->configuratorLoader->load($product, $resolverContext->getSalesChannelContext())); |
75
|
|
|
$buyBox->setTotalReviews($this->getReviewsCount($product, $resolverContext->getSalesChannelContext())); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function getReviewsCount(SalesChannelProductEntity $product, SalesChannelContext $context): int |
80
|
|
|
{ |
81
|
|
|
$reviewCriteria = $this->createReviewCriteria($context, $product->getId()); |
82
|
|
|
|
83
|
|
|
$reviews = $this->repository->aggregate($reviewCriteria, $context->getContext()); |
84
|
|
|
$aggregation = $reviews->get('review-count'); |
85
|
|
|
|
86
|
|
|
return $aggregation instanceof CountResult ? $aggregation->getCount() : 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function createReviewCriteria(SalesChannelContext $context, string $productId): Criteria |
90
|
|
|
{ |
91
|
|
|
$criteria = new Criteria(); |
92
|
|
|
|
93
|
|
|
$reviewFilters[] = new EqualsFilter('status', true); |
|
|
|
|
94
|
|
|
if ($context->getCustomer() !== null) { |
95
|
|
|
$reviewFilters[] = new EqualsFilter('customerId', $context->getCustomer()->getId()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$criteria->addFilter( |
99
|
|
|
new MultiFilter(MultiFilter::CONNECTION_AND, [ |
100
|
|
|
new MultiFilter(MultiFilter::CONNECTION_OR, $reviewFilters), |
101
|
|
|
new MultiFilter(MultiFilter::CONNECTION_OR, [ |
102
|
|
|
new EqualsFilter('product.id', $productId), |
103
|
|
|
new EqualsFilter('product.parentId', $productId), |
104
|
|
|
]), |
105
|
|
|
]) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$criteria->addAggregation(new CountAggregation('review-count', 'id')); |
109
|
|
|
|
110
|
|
|
return $criteria; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|