<?php
namespace Ks\KsAdditions\Subscribers;
use Ks\KsAdditions\KsAdditions;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MinAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $customFieldRepository;
private EntityRepositoryInterface $productRepository;
public function __construct(
EntityRepositoryInterface $customFieldRepository,
EntityRepositoryInterface $productRepository
) {
$this->customFieldRepository = $customFieldRepository;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => ['onProductPageLoaded', 1]
];
}
/**
* @param ProductPageLoadedEvent $event
*/
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$product = $event->getPage()->getProduct();
if ($product->getParentId()) {
$criteria = new Criteria();
$criteria
->addFilter(new EqualsFilter('product.parentId', $product->getParentId()))
->addFilter(new EqualsFilter('product.options.groupId', '8cedf9077a45432f9e26b43379f6bd37'));
$criteria
->addAssociation('options')
->addAssociation('options.group');
$criteria->addAggregation(
new MinAggregation('min-fitting', 'product.customFields.' . KsAdditions::ADDVISOR_PASSUNG_MIN),
new MaxAggregation('max-fitting', 'product.customFields.' . KsAdditions::ADDVISOR_PASSUNG_MAX),
);
$criteria->addSorting(new FieldSorting('product.stock', FieldSorting::DESCENDING));
$variants = $this->productRepository->search($criteria, $event->getContext());
$productVariantOptions = [];
foreach ($variants as $variant) {
foreach ($variant->getOptions() as $option) {
if (
$option->getGroupId() == '8cedf9077a45432f9e26b43379f6bd37'
and !isset($productVariantOptions[$option->getGroupId() . '-' . $option->getId()])
) {
if (isset($variant->getCustomFields()['addvisor_passung_min']) || isset($variant->getCustomFields()['addvisor_passung_max'])) {
$productVariantOptions[$option->getGroupId() . '-' . $option->getId()] = [
'groupId' => $option->getGroupId(),
'optionId' => $option->getId(),
'title' => $option->getName(),
'fittingMin' => $variant->getCustomFields()['addvisor_passung_min'],
'fittingMax' => $variant->getCustomFields()['addvisor_passung_max'],
//'stock' => $variant->getStock(),
];
} else {
return;
}
}
}
}
/*usort($productVariantOptions, function ($a, $b) {
return $b['stock'] - $a['stock'];
});*/
$event->getPage()->getProduct()->assign(
[
'ksProductFitting' => [
'min' => $variants->getAggregations()->get('min-fitting')->getVars()['min'],
'max' => $variants->getAggregations()->get('max-fitting')->getVars()['max'],
'variants' => $productVariantOptions
]
]
);
}
}
}