Skip to content

Commit 81344ff

Browse files
[Indexer] Fix product suppression propagation
1 parent 0215a07 commit 81344ff

3 files changed

Lines changed: 86 additions & 30 deletions

File tree

src/Indexer/AbstractIndexer.php

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,58 @@ public function __construct(
3939
}
4040

4141
public function reindex(array $documentIdsToReindex = []): void
42+
{
43+
$metadata = new Metadata($this->getEntityType());
44+
45+
foreach ($this->getActiveLocalizedCatalogs() as [$channel, $locale, $localizedCatalog]) {
46+
if ([] === $documentIdsToReindex) {
47+
$index = $this->indexOperation->createIndex($metadata, $localizedCatalog);
48+
} else {
49+
$index = $this->indexOperation->getIndexByName($metadata, $localizedCatalog);
50+
}
51+
52+
$batchSize = $this->getBatchSize($this->getEntityType(), $channel);
53+
$bulk = [];
54+
/** @var array $document */
55+
foreach ($this->getDocumentsToIndex($channel, $locale, $documentIdsToReindex) as $document) {
56+
if (0 === \count($document)) {
57+
continue;
58+
}
59+
/** @var array{id: int|string} $document */
60+
$bulk[$document['id']] = json_encode($document);
61+
if (\count($bulk) >= $batchSize) {
62+
$this->indexOperation->executeBulk($index, $bulk);
63+
$bulk = [];
64+
}
65+
}
66+
if (\count($bulk) > 0) {
67+
$this->indexOperation->executeBulk($index, $bulk);
68+
}
69+
70+
if ([] === $documentIdsToReindex) {
71+
$this->indexOperation->refreshIndex($index);
72+
$this->indexOperation->installIndex($index);
73+
}
74+
}
75+
}
76+
77+
public function remove(array $documentIdsToRemove): void
78+
{
79+
$metadata = new Metadata($this->getEntityType());
80+
81+
foreach ($this->getActiveLocalizedCatalogs() as [, , $localizedCatalog]) {
82+
$index = $this->indexOperation->getIndexByName($metadata, $localizedCatalog);
83+
$this->indexOperation->deleteBulk($index, $documentIdsToRemove);
84+
}
85+
}
86+
87+
/**
88+
* @return iterable<array{0: GallyChannelInterface, 1: LocaleInterface, 2: LocalizedCatalog}>
89+
*/
90+
private function getActiveLocalizedCatalogs(): iterable
4291
{
4392
/** @var ChannelInterface[] $channels */
4493
$channels = $this->channelRepository->findAll();
45-
$metadata = new Metadata($this->getEntityType());
4694

4795
foreach ($channels as $channel) {
4896
if (($channel instanceof GallyChannelInterface) && $channel->getGallyActive()) {
@@ -53,34 +101,7 @@ public function reindex(array $documentIdsToReindex = []): void
53101
throw new \InvalidArgumentException('No localized catalog found for channel ' . $channel->getCode() . ' and locale ' . $locale->getCode() . '. Try to synchronize your structure.');
54102
}
55103

56-
if ([] === $documentIdsToReindex) {
57-
$index = $this->indexOperation->createIndex($metadata, $localizedCatalog);
58-
} else {
59-
$index = $this->indexOperation->getIndexByName($metadata, $localizedCatalog);
60-
}
61-
62-
$batchSize = $this->getBatchSize($this->getEntityType(), $channel);
63-
$bulk = [];
64-
/** @var array $document */
65-
foreach ($this->getDocumentsToIndex($channel, $locale, $documentIdsToReindex) as $document) {
66-
if (0 === \count($document)) {
67-
continue;
68-
}
69-
/** @var array{id: int|string} $document */
70-
$bulk[$document['id']] = json_encode($document);
71-
if (\count($bulk) >= $batchSize) {
72-
$this->indexOperation->executeBulk($index, $bulk);
73-
$bulk = [];
74-
}
75-
}
76-
if (\count($bulk) > 0) {
77-
$this->indexOperation->executeBulk($index, $bulk);
78-
}
79-
80-
if ([] === $documentIdsToReindex) {
81-
$this->indexOperation->refreshIndex($index);
82-
$this->indexOperation->installIndex($index);
83-
}
104+
yield [$channel, $locale, $localizedCatalog];
84105
}
85106
}
86107
}

src/Indexer/Subscriber/ProductSubscriber.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@
2222

2323
class ProductSubscriber implements EventSubscriberInterface
2424
{
25+
/** @var array<int, int|string> */
26+
private array $idsPendingDeletion = [];
27+
2528
public function __construct(private ProductIndexer $productIndexer)
2629
{
2730
}
2831

29-
public static function getSubscribedEvents()
32+
public static function getSubscribedEvents(): array
3033
{
3134
return [
3235
'sylius.product.post_update' => 'onProductUpdate',
3336
'sylius.product.post_create' => 'onProductUpdate',
37+
'sylius.product.pre_delete' => 'onProductPreDelete',
38+
'sylius.product.post_delete' => 'onProductDelete',
3439
'sylius.product_variant.post_update' => 'onVariantUpdate',
3540
'sylius.product_variant.post_create' => 'onVariantUpdate',
41+
'sylius.product_variant.post_delete' => 'onVariantUpdate',
3642
];
3743
}
3844

@@ -44,6 +50,32 @@ public function onProductUpdate(GenericEvent $event): void
4450
}
4551
}
4652

53+
/**
54+
* Doctrine nulls out the identifier once the entity is actually removed, so the id
55+
* has to be captured before deletion (pre_delete) to still be usable in post_delete.
56+
*/
57+
public function onProductPreDelete(GenericEvent $event): void
58+
{
59+
$product = $event->getSubject();
60+
if ($product instanceof ProductInterface && null !== $product->getId()) {
61+
/** @var int|string $id */
62+
$id = $product->getId();
63+
$this->idsPendingDeletion[spl_object_id($product)] = $id;
64+
}
65+
}
66+
67+
public function onProductDelete(GenericEvent $event): void
68+
{
69+
$product = $event->getSubject();
70+
if ($product instanceof ProductInterface) {
71+
$productId = $this->idsPendingDeletion[spl_object_id($product)] ?? null;
72+
unset($this->idsPendingDeletion[spl_object_id($product)]);
73+
if (null !== $productId) {
74+
$this->productIndexer->remove([(string) $productId]);
75+
}
76+
}
77+
}
78+
4779
public function onVariantUpdate(GenericEvent $event): void
4880
{
4981
$variant = $event->getSubject();

src/Resources/views/shop/product/index/content/body/sidebar/filters.html.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@
2929
</form>
3030
</div>
3131
</div>
32+
{% elseif app.request.attributes.has('slug') %}
33+
{# Gally disabled: fall back to Sylius' own native taxonomy component and its default template. #}
34+
{{ component('sylius_shop:product:show:taxonomy') }}
3235
{% endif %}

0 commit comments

Comments
 (0)