<?php declare(strict_types=1);
namespace Acris\SuppliesUpselling\Storefront\Subscriber;
use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingEntity;
use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElement;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class ProductQuantitySubscriber implements EventSubscriberInterface
{
const DEFAULT_CROSS_SELLING_PRODUCT_LIST_TYPE = 'productList';
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $repository;
public function __construct(SystemConfigService $systemConfigService, EntityRepositoryInterface $repository)
{
$this->systemConfigService = $systemConfigService;
$this->repository = $repository;
}
public static function getSubscribedEvents(): array
{
return [
ProductCrossSellingsLoadedEvent::class => 'onCrossSellingLoaded',
];
}
public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event)
{
$crossSellings = $event->getCrossSellings()->getElements();
$productStreamArray = [];
$customQuantityArray = [];
$assignedProducts = [];
if ($crossSellings) {
/** @var CrossSellingElement $crossSelling */
foreach ($crossSellings as $crossSelling) {
if ($crossSelling->getCrossSelling()->getType() === self::DEFAULT_CROSS_SELLING_PRODUCT_LIST_TYPE) {
if (!empty($crossSelling->getCrossSelling()) && !empty($crossSelling->getCrossSelling()->getAssignedProducts()) && $crossSelling->getCrossSelling()->getAssignedProducts()->count() > 0) {
foreach ($crossSelling->getCrossSelling()->getAssignedProducts()->getElements() as $assignedProduct) {
if (!empty($assignedProduct) && !empty($assignedProduct->getExtensions()) && $assignedProduct->hasExtension('acrisProductCrossSellingAssignedProduct') && !empty($assignedProduct->getExtension('acrisProductCrossSellingAssignedProduct')) && !empty($assignedProduct->getExtension('acrisProductCrossSellingAssignedProduct')->getQuantity())) {
$customQuantityArray[$assignedProduct->getProductId()] = $assignedProduct->getExtension('acrisProductCrossSellingAssignedProduct')->getQuantity();
} else {
$customQuantityArray[$assignedProduct->getProductId()] = 1;
}
}
}
} else {
$productStreamId = $crossSelling->getCrossSelling()->getProductStreamId();
if ($productStreamId) {
$productStreamFilterRepository = $this->repository->search((new Criteria())->addFilter(new EqualsFilter('productStreamId', $productStreamId))->addFilter(new EqualsFilter('field', 'productNumber'))->addFilter(new EqualsFilter('type', 'equals')), $event->getContext());
if ($productStreamFilterRepository){
foreach($productStreamFilterRepository as $productStream){
if($productStream){
$productStreamArray[] = $productStream;
}
}
}
}
}
}
}
if($productStreamArray){
foreach ($productStreamArray as $product) {
if (!empty($product->getCustomFields())) {
if ($product->getCustomFields()["acris_accessory_quantity"] != 0) {
$customQuantityArray[$product->getValue()] = $product->getCustomFields()["acris_accessory_quantity"];
} else {
$customQuantityArray[$product->getValue()] = 1;
}
} else {
$customQuantityArray[$product->getValue()] = 1;
}
}
}
if (!empty($customQuantityArray)) {
$event->getCrossSellings()->addExtension('acris_accessory_quantity', new ArrayEntity([
$customQuantityArray
]));
}
}
}