Skip to content

Commit

Permalink
Merge pull request #244 from BitBagCommerce/op-225-product-option-pop…
Browse files Browse the repository at this point in the history
…ulate

OP-225: Product option populate
  • Loading branch information
GracjanJozefczyk committed May 22, 2024
2 parents 53dfc79 + 5945fcb commit 13409ce
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
25 changes: 18 additions & 7 deletions src/EventListener/ResourceIndexListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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]);
}
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/PropertyBuilder/OptionTaxonsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Repository/ProductVariantRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
}
2 changes: 2 additions & 0 deletions src/Repository/ProductVariantRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
interface ProductVariantRepositoryInterface
{
public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ?ProductVariantInterface;

public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array;
}
5 changes: 5 additions & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
</argument>
</argument>
<argument type="service" id="sylius.repository.product_attribute"/>
<argument type="service" id="sylius.repository.product_option"/>
<tag name="kernel.event_listener" event="sylius.product_attribute.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_attribute.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_attribute.post_delete" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.option.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.option.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.option.post_delete" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product.post_delete" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_delete" method="updateIndex" />
</service>
<service id="bitbag_sylius_elasticsearch_plugin.event_listener.product_taxon_index" class="BitBag\SyliusElasticsearchPlugin\EventListener\ProductTaxonIndexListener">
<argument type="service" id="bitbag.sylius_elasticsearch_plugin.refresher.resource" />
Expand Down

0 comments on commit 13409ce

Please sign in to comment.