<?php declare(strict_types=1);
namespace MoorlOrderNotice\Subscriber;
use Composer\IO\NullIO;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
class StorefrontSubscriber implements EventSubscriberInterface {
private $systemConfigService;
private $orderRepository;
private $connection;
private $cartService;
public function __construct(
CartService $cartService,
SystemConfigService $systemConfigService,
EntityRepositoryInterface $orderRepository,
Connection $connection
) {
$this->cartService = $cartService;
$this->systemConfigService = $systemConfigService;
$this->orderRepository = $orderRepository;
$this->connection = $connection;
}
public static function getSubscribedEvents(): array {
return [
CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent',
];
}
public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event): void {
$session = new Session();
$order = $event->getOrder();
if (!$order) {
return;
}
$notice = $session->get('moorl-order-notice.notice');
$to_continous = $session->get('moorl-order-notice.to_continous');
$libreka_download_links = $session->get('moorl-order-notice.libreka_download_links');
$order_target_group_number = $session->get('moorl-order-notice.order_target_group_number');
$advertising_permission = $session->get('moorl-order-notice.advertising_permission');
$hochschul_kontingent_check = $session->get('moorl-order-notice.hochschul_kontingent_check');
$bestellinformationen = $session->get('moorl-order-notice.bestellinformationen');
if ($bestellinformationen != null) {
$orderProducts = $event->getOrder()->getLineItems();
$documentProductInOrder = false;
/**
* @var OrderLineItemEntity $product
*/
foreach ($orderProducts as $product) {
if ($bestellinformationen->Bestellnummer == $product->getPayload()['productNumber']) {
$documentProductInOrder = true;
break;
}
}
if (!$documentProductInOrder) {
$bestellinformationen = null;
}
}
if (!isset($bestellinformationen->Titel)) {
$bestellinformationen = (object) [
'Titel' => '',
'Bestellnummer' => '',
'NextUrl' => '',
'DokumentenId' => '',
];
}
if ($notice == 'test@die.de') {
die();
}
if (
$notice ||
$notice != '' ||
$to_continous ||
$libreka_download_links ||
$order_target_group_number ||
$advertising_permission ||
$hochschul_kontingent_check ||
isset($bestellinformationen->DokumentenId)
) {
$session->set('moorl-order-notice.notice', null);
$session->set('moorl-order-notice.to_continous', null);
// $session->set('moorl-order-notice.order_target_group_number', null);
$session->set('moorl-order-notice.bestellinformationen', null);
// '&titel=' . $_SESSION['bestellinformationen']->Titel .
// '&product_nr=' . $_SESSION['bestellinformationen']->Bestellnummer .
// '&next_url=' . $_SESSION['bestellinformationen']->NextUrl .
// '&document_id=' . $_SESSION['bestellinformationen']->DokumentenId;
$this->orderRepository->update(
[
[
'id' => $order->getId(),
'customFields' => [
'order_notice_notice' => $notice,
'order_products_to_continous' => $to_continous,
'order_products_libreka_download_links' => $libreka_download_links,
'order_target_group_number' => $order_target_group_number,
'order_notice_allow_spam' => $advertising_permission,
'order_notice_hochschul_kontingent_check' => $hochschul_kontingent_check, //Hochschulkontigent
'order_document_title' => $bestellinformationen->Titel,
'order_document_number' => $bestellinformationen->Bestellnummer,
'order_document_nexturl' => $bestellinformationen->NextUrl,
'order_document_id' => $bestellinformationen->DokumentenId,
],
],
],
$event->getContext(),
);
}
}
}