<?php declare(strict_types=1);
namespace Huebert\AccessoriesDirectly\Subscriber;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Product\Configurator\ProductPageConfiguratorLoader;
use Shopware\Storefront\Page\Product\CrossSelling\CrossSellingLoadedEvent;
use Shopware\Storefront\Page\Product\CrossSelling\CrossSellingProductCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CrossSellingSubscriber implements EventSubscriberInterface
{
protected $optionRepository;
/**
* @var ProductPageConfiguratorLoader
*/
private $configuratorLoader;
public function __construct(EntityRepositoryInterface $optionRepository, ProductPageConfiguratorLoader $configuratorLoader)
{
$this->optionRepository = $optionRepository;
$this->configuratorLoader = $configuratorLoader;
}
public static function getSubscribedEvents()
{
return [
CrossSellingLoadedEvent::class => "onCrossSellingLoaded",
ProductCrossSellingsLoadedEvent::class => "onProductCrossSellingLoaded"
];
}
//legacy function for < 6.3.2.0
public function onCrossSellingLoaded(CrossSellingLoadedEvent $crossSellingLoadedEvent)
{
$elements = $crossSellingLoadedEvent->getCrossSellingResult()->getElements();
foreach ($elements as $element) {
$products = $element->getProducts();
foreach ($products as $product) {
if ($product->getOptionIds()) {
$parts = [];
foreach ($product->getOptionIds() as $optionId) {
$criteria = New Criteria();
$criteria
->addFilter(new EqualsFilter('id', $optionId))
->addAssociation('group');
$option = $this->optionRepository->search($criteria, $crossSellingLoadedEvent->getContext())->first();
$parts[] = [
'group' => $option->getGroup()->getTranslation('name'),
'option' => $option->getTranslation('name')
];
}
$product->setVariation(
$parts
);
}
}
}
}
public function onProductCrossSellingLoaded(ProductCrossSellingsLoadedEvent $crossSellingLoadedEvent)
{
$elements = $crossSellingLoadedEvent->getCrossSellings();
foreach ($elements as $element) {
$products = $element->getProducts();
foreach ($products as $product) {
$_productConfigurator = $this->configuratorLoader->load($product, $crossSellingLoadedEvent->getSalesChannelContext());
if ($product->getOptionIds() && count($product->getVariation()) === 0) {
$parts = [];
foreach ($product->getOptionIds() as $optionId) {
$criteria = New Criteria();
$criteria
->addFilter(new EqualsFilter('id', $optionId))
->addAssociation('group');
$option = $this->optionRepository->search($criteria, $crossSellingLoadedEvent->getContext())->first();
if ($option) {
$groupName = null;
if ($option->getGroup()) {
if ($option->getGroup()->getTranslation('name')) {
$groupName = $option->getGroup()->getTranslation('name');
}
}
$optionName = null;
if ($option->getTranslation('name')) {
$optionName = $option->getTranslation('name');
}
$parts[] = [
'group' => $groupName,
'option' => $optionName
];
}
}
if (count($parts) > 0) {
$product->setVariation(
$parts
);
}
}
$product->assign(
[ 'productConfigurator' => $_productConfigurator,]
);
}
}
}
}