Skip to content

Commit 13409ce

Browse files
Merge pull request #244 from BitBagCommerce/op-225-product-option-populate
OP-225: Product option populate
2 parents 53dfc79 + 5945fcb commit 13409ce

File tree

5 files changed

+42
-18
lines changed

5 files changed

+42
-18
lines changed

src/EventListener/ResourceIndexListener.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
namespace BitBag\SyliusElasticsearchPlugin\EventListener;
1414

1515
use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface;
16-
use Sylius\Component\Core\Model\Product;
16+
use Sylius\Component\Core\Model\ProductInterface;
17+
use Sylius\Component\Core\Model\ProductVariantInterface;
1718
use Sylius\Component\Product\Model\ProductAttribute;
19+
use Sylius\Component\Product\Model\ProductOption;
1820
use Sylius\Component\Resource\Model\ResourceInterface;
1921
use Sylius\Component\Resource\Repository\RepositoryInterface;
2022
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -28,14 +30,18 @@ final class ResourceIndexListener implements ResourceIndexListenerInterface
2830

2931
private RepositoryInterface $attributeRepository;
3032

33+
private RepositoryInterface $optionRepository;
34+
3135
public function __construct(
3236
ResourceRefresherInterface $resourceRefresher,
3337
array $persistersMap,
34-
RepositoryInterface $attributeRepository
38+
RepositoryInterface $attributeRepository,
39+
RepositoryInterface $optionRepository
3540
) {
3641
$this->resourceRefresher = $resourceRefresher;
3742
$this->persistersMap = $persistersMap;
3843
$this->attributeRepository = $attributeRepository;
44+
$this->optionRepository = $optionRepository;
3945
}
4046

4147
public function updateIndex(GenericEvent $event): void
@@ -54,11 +60,16 @@ public function updateIndex(GenericEvent $event): void
5460
$this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]);
5561
}
5662

57-
if ($resource instanceof Product
58-
&& ProductAttribute::class === $config[self::MODEL_KEY]
59-
) {
60-
foreach ($this->attributeRepository->findAll() as $attribute) {
61-
$this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]);
63+
if ($resource instanceof ProductInterface || $resource instanceof ProductVariantInterface) {
64+
if (ProductAttribute::class === $config[self::MODEL_KEY]) {
65+
foreach ($this->attributeRepository->findAll() as $attribute) {
66+
$this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]);
67+
}
68+
}
69+
if (ProductOption::class === $config[self::MODEL_KEY]) {
70+
foreach ($this->optionRepository->findAll() as $option) {
71+
$this->resourceRefresher->refresh($option, $config[self::SERVICE_ID_KEY]);
72+
}
6273
}
6374
}
6475
}

src/PropertyBuilder/OptionTaxonsBuilder.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
1616
use BitBag\SyliusElasticsearchPlugin\Repository\ProductVariantRepositoryInterface;
1717
use FOS\ElasticaBundle\Event\PostTransformEvent;
18-
use Sylius\Component\Core\Model\ProductInterface;
1918
use Sylius\Component\Product\Model\ProductOptionInterface;
2019
use Sylius\Component\Product\Model\ProductOptionValueInterface;
2120
use Sylius\Component\Resource\Repository\RepositoryInterface;
@@ -57,23 +56,20 @@ public function consumeEvent(PostTransformEvent $event): void
5756
}
5857

5958
$document = $event->getDocument();
60-
$optionValues = $this->productOptionValueRepository->findAll();
59+
$optionValues = $this->productOptionValueRepository->findBy(['option' => $documentProductOption]);
6160
$taxons = [];
6261

6362
/** @var ProductOptionValueInterface $optionValue */
6463
foreach ($optionValues as $optionValue) {
6564
$option = $optionValue->getOption();
66-
$productVariant = $this->productVariantRepository->findOneByOptionValue($optionValue);
65+
$productVariants = $this->productVariantRepository->findByOptionValue($optionValue);
6766

68-
if (null === $productVariant) {
69-
continue;
70-
}
71-
72-
/** @var ProductInterface $product */
73-
$product = $productVariant->getProduct();
67+
foreach ($productVariants as $productVariant) {
68+
$product = $productVariant->getProduct();
7469

75-
if ($documentProductOption === $option && $product->isEnabled()) {
76-
$taxons = array_merge($taxons, $this->productTaxonsMapper->mapToUniqueCodes($product));
70+
if ($documentProductOption === $option && $product->isEnabled()) {
71+
$taxons = array_merge($taxons, $this->productTaxonsMapper->mapToUniqueCodes($product));
72+
}
7773
}
7874
}
7975

src/Repository/ProductVariantRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ public function findOneByOptionValue(ProductOptionValueInterface $productOptionV
3636
->getOneOrNullResult()
3737
;
3838
}
39+
40+
public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array
41+
{
42+
return $this->baseProductVariantRepository->createQueryBuilder('o')
43+
->where(':optionValue MEMBER OF o.optionValues')
44+
->setParameter('optionValue', $productOptionValue)
45+
->getQuery()
46+
->getResult()
47+
;
48+
}
3949
}

src/Repository/ProductVariantRepositoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
interface ProductVariantRepositoryInterface
1919
{
2020
public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ?ProductVariantInterface;
21+
22+
public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array;
2123
}

src/Resources/config/services/event_listener.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
</argument>
2121
</argument>
2222
<argument type="service" id="sylius.repository.product_attribute"/>
23+
<argument type="service" id="sylius.repository.product_option"/>
2324
<tag name="kernel.event_listener" event="sylius.product_attribute.post_create" method="updateIndex" />
2425
<tag name="kernel.event_listener" event="sylius.product_attribute.post_update" method="updateIndex" />
26+
<tag name="kernel.event_listener" event="sylius.product_attribute.post_delete" method="updateIndex" />
2527
<tag name="kernel.event_listener" event="sylius.option.post_create" method="updateIndex" />
2628
<tag name="kernel.event_listener" event="sylius.option.post_update" method="updateIndex" />
29+
<tag name="kernel.event_listener" event="sylius.option.post_delete" method="updateIndex" />
2730
<tag name="kernel.event_listener" event="sylius.product.post_create" method="updateIndex" />
2831
<tag name="kernel.event_listener" event="sylius.product.post_update" method="updateIndex" />
32+
<tag name="kernel.event_listener" event="sylius.product.post_delete" method="updateIndex" />
2933
<tag name="kernel.event_listener" event="sylius.product_variant.post_create" method="updateIndex" />
3034
<tag name="kernel.event_listener" event="sylius.product_variant.post_update" method="updateIndex" />
35+
<tag name="kernel.event_listener" event="sylius.product_variant.post_delete" method="updateIndex" />
3136
</service>
3237
<service id="bitbag_sylius_elasticsearch_plugin.event_listener.product_taxon_index" class="BitBag\SyliusElasticsearchPlugin\EventListener\ProductTaxonIndexListener">
3338
<argument type="service" id="bitbag.sylius_elasticsearch_plugin.refresher.resource" />

0 commit comments

Comments
 (0)