<?php
namespace EnnoDigitalProducts\Subscriber;
use EnnoDigitalProducts\Service\DigitalProductDataService;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
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 Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LineItemAdded implements EventSubscriberInterface
{
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()
{
return [
BeforeLineItemAddedEvent::class => 'onLineItemAdded'
];
}
private function ennoInit()
{
$salesChannelId = null;
try
{
$requestStack = $this->container->get('request_stack');
if ( $requestStack && method_exists($requestStack, 'getCurrentRequest') )
{
$currentRequest = $requestStack->getCurrentRequest();
if ($currentRequest && method_exists($currentRequest, 'get') )
{
$salesChannelId = $currentRequest->get("sw-sales-channel-id");
}
}
}
catch (\Exception $e) { }
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 onLineItemAdded(BeforeLineItemAddedEvent $event)
{
$this->ennoInit();
if( !is_array($this->pluginConfig) ) return;
if( empty($this->pluginConfig) ) return;
if( !array_key_exists('active', $this->pluginConfig)) return;
if( !$this->pluginConfig["active"] ) return;
$lineItem = $event->getLineItem();
if($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) return;
$digitalData = $this->digitalProductDataservice->getProductData(Context::createDefaultContext(), $lineItem->getId(), false, ['settings'])->getVars();
if(is_array($digitalData) && is_array($digitalData['settings']))
{
$lineItem->setPayloadValue('ennoIsDigital', $digitalData['settings']['isDigital']);
}
}
}