<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 27 lip 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Arrangements\Subscriber;
use Crehler\Arrangements\Entity\Arrangement\ArrangementEntity;
use Crehler\Arrangements\Service\ArrangementService;
use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
use Monolog\Logger;
use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean;
use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductListingResultSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly ArrangementService $arrangementService,
private readonly ProductStatusStorefrontService $productStatusService,
private bool $skip = false
)
{
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LISTING_RESULT => 'handleListingLoaded',
ProductPageLoadedEvent::class => 'handleProductLoaded',
'arrangement_product.loaded' => 'handleProductArrangementLoaded',
ProductIndexerEvent::class =>['onProductIndexer', 1],
];
}
public function handleProductArrangementLoaded(EntityLoadedEvent $event): void
{
foreach ($event->getEntities() as $arrangementsProduct) {
$product = $arrangementsProduct->getProduct();
$product->addExtension('productStatus', $this->productStatusService->getProductStatus($product, $event->getContext()));
}
}
public function onProductIndexer($event)
{
$this->skip = true;
}
public function handleProductLoaded(ProductPageLoadedEvent $event): void
{
if ($event->getContext()->getScope() === Context::CRUD_API_SCOPE ||
$event->getContext()->getScope() === Context::SYSTEM_SCOPE ||
$this->skip
) {
return;
}
/** @var ArrayStruct $foreignKeys */
$foreignKeys = $event->getPage()->getProduct()->getExtension('foreignKeys');
if (null !== $foreignKeys->offsetGet('arrangementId')) {
$arrangement = $this->arrangementService->getArrangementById(
$foreignKeys->offsetGet('arrangementId'),
$event->getSalesChannelContext(),
$event->getRequest()
);
$event->getPage()->getProduct()->addExtension('arrangement', $arrangement);
/** @var ArrangementEntity $arrangement */
$arrangement = $event->getPage()->getProduct()->getExtension('arrangement');
$arrangement->setMainProduct($event->getPage()->getProduct());
}
}
public function handleListingLoaded(ProductListingResultEvent $event): void
{
if ($event->getContext()->getScope() === Context::CRUD_API_SCOPE) {
return;
}
/** @var SalesChannelProductEntity $product */
foreach ($event->getResult()->getElements() as $product) {
/** @var ArrayStruct $foreignKeys */
$foreignKeys = $product->getExtension('foreignKeys');
if (null !== $foreignKeys->offsetGet('arrangementId')) {
$arrangement = $this->arrangementService->getArrangementById(
$foreignKeys->offsetGet('arrangementId'),
$event->getSalesChannelContext(),
$event->getRequest()
);
$product->addExtension('arrangement', $arrangement);
}
}
}
}