/
ProductDetailRoute.php
200 lines (164 loc) · 8.07 KB
/
ProductDetailRoute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php declare(strict_types=1);
namespace Shopware\Core\Content\Product\SalesChannel\Detail;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Category\Service\CategoryBreadcrumbBuilder;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
use Shopware\Core\Content\Product\Exception\ProductNotFoundException;
use Shopware\Core\Content\Product\SalesChannel\AbstractProductCloseoutFilterFactory;
use Shopware\Core\Content\Product\SalesChannel\Detail\Event\ResolveVariantIdEvent;
use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Profiling\Profiler;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
#[Route(defaults: ['_routeScope' => ['store-api']])]
#[Package('inventory')]
class ProductDetailRoute extends AbstractProductDetailRoute
{
/**
* @internal
*/
public function __construct(
private readonly SalesChannelRepository $productRepository,
private readonly SystemConfigService $config,
private readonly Connection $connection,
private readonly ProductConfiguratorLoader $configuratorLoader,
private readonly CategoryBreadcrumbBuilder $breadcrumbBuilder,
private readonly SalesChannelCmsPageLoaderInterface $cmsPageLoader,
private readonly SalesChannelProductDefinition $productDefinition,
private readonly AbstractProductCloseoutFilterFactory $productCloseoutFilterFactory,
private readonly EventDispatcherInterface $eventDispatcher
) {
}
public function getDecorated(): AbstractProductDetailRoute
{
throw new DecorationPatternException(self::class);
}
#[Route(path: '/store-api/product/{productId}', name: 'store-api.product.detail', methods: ['POST'], defaults: ['_entity' => 'product'])]
public function load(string $productId, Request $request, SalesChannelContext $context, Criteria $criteria): ProductDetailRouteResponse
{
return Profiler::trace('product-detail-route', function () use ($productId, $request, $context, $criteria) {
$mainVariantId = $this->checkVariantListingConfig($productId, $context);
$resolveVariantIdEvent = new ResolveVariantIdEvent(
$productId,
$mainVariantId,
$context,
);
$this->eventDispatcher->dispatch($resolveVariantIdEvent);
$productId = $resolveVariantIdEvent->getResolvedVariantId() ?? $this->findBestVariant($productId, $context);
$this->addFilters($context, $criteria);
$criteria->setIds([$productId]);
$criteria->setTitle('product-detail-route');
$product = $this->productRepository
->search($criteria, $context)
->first();
if (!($product instanceof SalesChannelProductEntity)) {
throw new ProductNotFoundException($productId);
}
$product->setSeoCategory(
$this->breadcrumbBuilder->getProductSeoCategory($product, $context)
);
$configurator = $this->configuratorLoader->load($product, $context);
$pageId = $product->getCmsPageId();
if ($pageId) {
// clone product to prevent recursion encoding (see NEXT-17603)
$resolverContext = new EntityResolverContext($context, $request, $this->productDefinition, clone $product);
$pages = $this->cmsPageLoader->load(
$request,
$this->createCriteria($pageId, $request),
$context,
$product->getTranslation('slotConfig'),
$resolverContext
);
if ($page = $pages->first()) {
$product->setCmsPage($page);
}
}
return new ProductDetailRouteResponse($product, $configurator);
});
}
private function addFilters(SalesChannelContext $context, Criteria $criteria): void
{
$criteria->addFilter(
new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_LINK)
);
$salesChannelId = $context->getSalesChannel()->getId();
$hideCloseoutProductsWhenOutOfStock = $this->config->get('core.listing.hideCloseoutProductsWhenOutOfStock', $salesChannelId);
if ($hideCloseoutProductsWhenOutOfStock) {
$filter = $this->productCloseoutFilterFactory->create($context);
$filter->addQuery(new EqualsFilter('product.parentId', null));
$criteria->addFilter($filter);
}
}
private function checkVariantListingConfig(string $productId, SalesChannelContext $context): ?string
{
if (!Uuid::isValid($productId)) {
return null;
}
$productData = $this->connection->fetchAssociative(
'# product-detail-route::check-variant-listing-config
SELECT
variant_listing_config as variantListingConfig,
parent_id as parentId
FROM product
WHERE id = :id
AND version_id = :versionId',
[
'id' => Uuid::fromHexToBytes($productId),
'versionId' => Uuid::fromHexToBytes($context->getContext()->getVersionId()),
]
);
if (empty($productData) || empty($productData['parentId']) || $productData['variantListingConfig'] === null) {
return null;
}
$variantListingConfig = json_decode((string) $productData['variantListingConfig'], true, 512, \JSON_THROW_ON_ERROR);
if (isset($variantListingConfig['displayParent']) && $variantListingConfig['displayParent'] !== true) {
return null;
}
return $variantListingConfig['mainVariantId'] ?? null;
}
/**
* @throws InconsistentCriteriaIdsException
*/
private function findBestVariant(string $productId, SalesChannelContext $context): string
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('product.parentId', $productId))
->addSorting(new FieldSorting('product.available', FieldSorting::DESCENDING))
->addSorting(new FieldSorting('product.price'))
->setLimit(1);
$criteria->setTitle('product-detail-route::find-best-variant');
$variantId = $this->productRepository->searchIds($criteria, $context);
return $variantId->firstId() ?? $productId;
}
private function createCriteria(string $pageId, Request $request): Criteria
{
$criteria = new Criteria([$pageId]);
$criteria->setTitle('product::cms-page');
$slots = $request->get('slots');
if (\is_string($slots)) {
$slots = explode('|', $slots);
}
if (!empty($slots) && \is_array($slots)) {
$criteria
->getAssociation('sections.blocks')
->addFilter(new EqualsAnyFilter('slots.id', $slots));
}
return $criteria;
}
}