<?php declare(strict_types=1);
namespace EnnoDigitalProducts\Subscriber;
use EnnoDigitalProducts\Service\DigitalProductDataService;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
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\Core\Framework\Context;
use Doctrine\DBAL\Connection;
class OrderLineItemLoaded implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
private $digitalProductDataservice;
private $salesChannelRepository;
private $container;
private $salesChannelId;
private $salesChannel;
private $pluginConfig;
public function __construct(SystemConfigService $systemConfigService, DigitalProductDataService $digitalProductDataservice, EntityRepositoryInterface $salesChannelRepository, $container)
{
$this->systemConfigService = $systemConfigService;
$this->digitalProductDataservice = $digitalProductDataservice;
$this->salesChannelRepository = $salesChannelRepository;
$this->container = $container;
}
public static function getSubscribedEvents(): array
{
return [
OrderEvents::ORDER_LINE_ITEM_LOADED_EVENT => 'onOrderLineItemLoaded'
];
}
private function ennoInit(EntityLoadedEvent $event): void
{
$lineItemIds = $event->getIds();
if ( !(is_array($lineItemIds) && !empty($lineItemIds)) ) return;
$lineItemId = $lineItemIds[0];
$salesChannelId = $this->container->get(Connection::class)->fetchOne(
"SELECT LOWER(HEX(sales_channel_id)) FROM `order` WHERE id = (SELECT order_id FROM `order_line_item` WHERE id = UNHEX('{$lineItemId}') LIMIT 1) LIMIT 1"
);
if ( !$salesChannelId ) return;
if ( $this->salesChannelId === $salesChannelId ) return;
$this->salesChannelId = $salesChannelId;
$this->salesChannel = $this->salesChannelRepository->search(new Criteria([$salesChannelId]), Context::createDefaultContext())->get($salesChannelId);
$this->pluginConfig = $this->systemConfigService->get('EnnoDigitalProducts.config', $salesChannelId);
}
public function onOrderLineItemLoaded(EntityLoadedEvent $event): void
{
$this->ennoInit($event);
if( !is_array($this->pluginConfig) ) return;
if( empty($this->pluginConfig) ) return;
if( !array_key_exists('active', $this->pluginConfig)) return;
if( !$this->pluginConfig["active"] ) return;
/** @var ProductEntity $productEntity */
foreach ($event->getEntities() as $entity)
{
if($entity->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) continue;
$digitalData = $this->digitalProductDataservice->getProductData($event->getContext(), $entity->getProductId());
if($digitalData) $entity->addExtension('ennoDigitalProducts', $digitalData);
}
}
}