diff --git a/src/EventListener/ResourceIndexListener.php b/src/EventListener/ResourceIndexListener.php
index 64ca853a..7fac2671 100644
--- a/src/EventListener/ResourceIndexListener.php
+++ b/src/EventListener/ResourceIndexListener.php
@@ -13,8 +13,10 @@
namespace BitBag\SyliusElasticsearchPlugin\EventListener;
use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface;
-use Sylius\Component\Core\Model\Product;
+use Sylius\Component\Core\Model\ProductInterface;
+use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Product\Model\ProductAttribute;
+use Sylius\Component\Product\Model\ProductOption;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -28,14 +30,18 @@ final class ResourceIndexListener implements ResourceIndexListenerInterface
private RepositoryInterface $attributeRepository;
+ private RepositoryInterface $optionRepository;
+
public function __construct(
ResourceRefresherInterface $resourceRefresher,
array $persistersMap,
- RepositoryInterface $attributeRepository
+ RepositoryInterface $attributeRepository,
+ RepositoryInterface $optionRepository
) {
$this->resourceRefresher = $resourceRefresher;
$this->persistersMap = $persistersMap;
$this->attributeRepository = $attributeRepository;
+ $this->optionRepository = $optionRepository;
}
public function updateIndex(GenericEvent $event): void
@@ -54,11 +60,16 @@ public function updateIndex(GenericEvent $event): void
$this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]);
}
- if ($resource instanceof Product
- && ProductAttribute::class === $config[self::MODEL_KEY]
- ) {
- foreach ($this->attributeRepository->findAll() as $attribute) {
- $this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]);
+ if ($resource instanceof ProductInterface || $resource instanceof ProductVariantInterface) {
+ if (ProductAttribute::class === $config[self::MODEL_KEY]) {
+ foreach ($this->attributeRepository->findAll() as $attribute) {
+ $this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]);
+ }
+ }
+ if (ProductOption::class === $config[self::MODEL_KEY]) {
+ foreach ($this->optionRepository->findAll() as $option) {
+ $this->resourceRefresher->refresh($option, $config[self::SERVICE_ID_KEY]);
+ }
}
}
}
diff --git a/src/PropertyBuilder/OptionTaxonsBuilder.php b/src/PropertyBuilder/OptionTaxonsBuilder.php
index 4dd9b092..08a7a711 100644
--- a/src/PropertyBuilder/OptionTaxonsBuilder.php
+++ b/src/PropertyBuilder/OptionTaxonsBuilder.php
@@ -15,7 +15,6 @@
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface;
use BitBag\SyliusElasticsearchPlugin\Repository\ProductVariantRepositoryInterface;
use FOS\ElasticaBundle\Event\PostTransformEvent;
-use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Sylius\Component\Product\Model\ProductOptionValueInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
@@ -57,23 +56,20 @@ public function consumeEvent(PostTransformEvent $event): void
}
$document = $event->getDocument();
- $optionValues = $this->productOptionValueRepository->findAll();
+ $optionValues = $this->productOptionValueRepository->findBy(['option' => $documentProductOption]);
$taxons = [];
/** @var ProductOptionValueInterface $optionValue */
foreach ($optionValues as $optionValue) {
$option = $optionValue->getOption();
- $productVariant = $this->productVariantRepository->findOneByOptionValue($optionValue);
+ $productVariants = $this->productVariantRepository->findByOptionValue($optionValue);
- if (null === $productVariant) {
- continue;
- }
-
- /** @var ProductInterface $product */
- $product = $productVariant->getProduct();
+ foreach ($productVariants as $productVariant) {
+ $product = $productVariant->getProduct();
- if ($documentProductOption === $option && $product->isEnabled()) {
- $taxons = array_merge($taxons, $this->productTaxonsMapper->mapToUniqueCodes($product));
+ if ($documentProductOption === $option && $product->isEnabled()) {
+ $taxons = array_merge($taxons, $this->productTaxonsMapper->mapToUniqueCodes($product));
+ }
}
}
diff --git a/src/Repository/ProductVariantRepository.php b/src/Repository/ProductVariantRepository.php
index 9fe016d1..7e4d3249 100644
--- a/src/Repository/ProductVariantRepository.php
+++ b/src/Repository/ProductVariantRepository.php
@@ -36,4 +36,14 @@ public function findOneByOptionValue(ProductOptionValueInterface $productOptionV
->getOneOrNullResult()
;
}
+
+ public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array
+ {
+ return $this->baseProductVariantRepository->createQueryBuilder('o')
+ ->where(':optionValue MEMBER OF o.optionValues')
+ ->setParameter('optionValue', $productOptionValue)
+ ->getQuery()
+ ->getResult()
+ ;
+ }
}
diff --git a/src/Repository/ProductVariantRepositoryInterface.php b/src/Repository/ProductVariantRepositoryInterface.php
index 464bf4ce..9f42ebe9 100644
--- a/src/Repository/ProductVariantRepositoryInterface.php
+++ b/src/Repository/ProductVariantRepositoryInterface.php
@@ -18,4 +18,6 @@
interface ProductVariantRepositoryInterface
{
public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ?ProductVariantInterface;
+
+ public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array;
}
diff --git a/src/Resources/config/services/event_listener.xml b/src/Resources/config/services/event_listener.xml
index 1c84b5bd..875a0a33 100644
--- a/src/Resources/config/services/event_listener.xml
+++ b/src/Resources/config/services/event_listener.xml
@@ -20,14 +20,19 @@
+
+
+
+
+