<?php
declare(strict_types=1);
namespace MetalementsTheme\Subscriber;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Content\Media\DataAbstractionLayer\MediaRepositoryDecorator;
class ProductVariantenMedia implements EventSubscriberInterface
{
private $productRepository;
private MediaRepositoryDecorator $mediaRepository;
public function __construct(EntityRepositoryInterface $productRepository, MediaRepositoryDecorator $mediaRepository)
{
$this->productRepository = $productRepository;
$this->mediaRepository = $mediaRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$product = $event->getPage()->getProduct();
$parentProduct = $product->getParent();
if ($parentProduct !== null) {
$product = $parentProduct;
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter(
'parentId',
$product->getParentId()
));
$criteria->addAssociation('cover.media');
$criteria->addAssociation('options.group');
// retrieve all product variants
$products = $this->productRepository->search($criteria, $event->getContext());
$matchingVariants = $products->getEntities()->getElements();
$productColor = null;
$debug = [];
$options_value = null;
$allOptions = [];
// find which options are selected, i.e. which 'Eigenschaften' does the current product have.
$NummberOfOptions = 0;
foreach ($product->getOptions() as $option) {
if (strpos($option->getGroup()->name, 'Design') == false) {
$options_value[] = $option->name;
$NummberOfOptions = $NummberOfOptions + 1;
}
}
//die('<pre>' . print_r($NummberOfOptions, true) . '</pre>');
// echo "<pre>";
// print_r($debug);
// die();
// break;
$matchingVariants = $products->getEntities()->getElements();
$filteredVariants = null;
// then filter variants based on the selected 'Eigenschaften'
$NummberOfOptionsThatMatch = 0;
foreach ($matchingVariants as $variantProduct) {
$NummberOfOptionsThatMatch = 0;
$allOptions[] = 'Product:';
foreach ($variantProduct->getOptions() as $option) {
if(in_array($option->name, $options_value)) {
$NummberOfOptionsThatMatch = $NummberOfOptionsThatMatch + 1;
}
$allOptions[] = $option->name;
}
if ($NummberOfOptionsThatMatch == $NummberOfOptions) {
$filteredVariants[] = $variantProduct;
}
}
foreach ($filteredVariants as $variant) {
$coverId = $variant->getCover()->getMediaId();
if ($coverId) {
$mediaCriteria = new Criteria();
$mediaCriteria->addFilter(new EqualsFilter('id', $coverId));
$mediaEntity = $this->mediaRepository->search($mediaCriteria, $event->getContext())->first();
if ($mediaEntity) {
$mediaEntity->addExtension('debugVariant', $variant);
$variant->addExtension('variantMedia', $mediaEntity);
}
}
}
$array = ['wct_product_varianten' => $filteredVariants];
$event->getPage()->assign($array);
$event->getPage()->assign(['all_options' => $allOptions]);
}
}