<?php
declare(strict_types=1);
namespace HelmaTheme\Subscriber;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\Event\CartSavedEvent;
use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
use Shopware\Core\Checkout\Document\Event\DocumentTemplateRendererParameterEvent;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
use Shopware\Core\Content\Property\PropertyGroupCollection;
use Shopware\Core\Content\Property\PropertyGroupEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\PlatformRequest;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
use Symfony\Component\DependencyInjection\Container;
use Shopware\Core\Checkout\Customer\CustomerCollection;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Content\Media\MediaCollection;
use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
use Shopware\Storefront\Event\RouteRequest\RouteRequestEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\Validator\Constraints\Length;
class OnCartSavedEvent implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $propertyRepository;
/**
* @var EntityRepositoryInterface
*/
private $groupRepository;
private $container;
/**
* @var EntityRepositoryInterface
*/
private $customerRepository;
/**
* @var Environment
*/
private $twig;
/**
* @var EntityRepositoryInterface
*/
private $mediaRepository;
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(
EntityRepositoryInterface $propertyRepository,
EntityRepositoryInterface $groupRepository,
Container $container,
EntityRepositoryInterface $customerRepository,
Environment $environment,
EntityRepositoryInterface $mediaRepository
) {
$this->propertyRepository = $propertyRepository;
$this->groupRepository = $groupRepository;
$this->container = $container;
$this->customerRepository = $customerRepository;
$this->twig = $environment;
$this->mediaRepository = $mediaRepository;
}
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
CartSavedEvent::class => 'on_CartSavedEvent'
];
}
public function on_CartSavedEvent(CartSavedEvent $event): void
{
$cart = $event->getCart();
$lineItems = $cart->getLineItems();
// Get teasers
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'Gewerk'));
/** @var PropertyGroupCollection<PropertyGroupEntity> $groups */
$groups = $this->groupRepository->search($criteria, Context::createDefaultContext())->getEntities();
$groupsArray = [];
/** @var PropertyGroupEntity $group */
foreach ($groups as $group) {
$groupsArray[] = $group;
}
if (empty($groupsArray)) {
return;
} else {
$groupId = $groupsArray[0]->getId();
}
$teasersArray = [];
$newAdded = false;
$count = 0;
foreach ($lineItems as $lineItem) {
if ($lineItem->getPayload() && !empty($lineItem->getPayload()['propertyIds'])) {
foreach ($lineItem->getPayload()['propertyIds'] as $propertyId) {
// Get teasers
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $propertyId));
$criteria->addFilter(new EqualsFilter('groupId', $groupId));
/** @var PropertyGroupOptionCollection<PropertyGroupOptionEntity> $teasers */
$teasers = $this->propertyRepository->search($criteria, Context::createDefaultContext())->getEntities();
/** @var PropertyGroupOptionEntity $teaser */
foreach ($teasers as $teaser) {
if ($teasersArray && $teasersArray[0]) {
$count = 0;
foreach ($teasersArray as $item) {
if ($item->getId() == $teaser->getId()) {
$count++;
}
}
if ($count == 0) {
$teasersArray[] = $teaser;
}
} else {
$teasersArray[] = $teaser;
}
}
}
}
}
usort($teasersArray, array($this, "sort_objects_by_total"));
$newLineItems = new LineItemCollection();
$hasProp = false;
foreach ($teasersArray as $item) {
foreach ($lineItems as $lineItem) {
if ($lineItem->getPayload() && !empty($lineItem->getPayload()['propertyIds'])) {
foreach ($lineItem->getPayload()['propertyIds'] as $propertyId) {
if ($item->getId() == $propertyId) {
$newLineItems->add($lineItem);
$newAdded = true;
}
}
}
}
}
foreach ($lineItems as $lineItem) {
$hasProp = false;
if ($lineItem->getPayload() && !empty($lineItem->getPayload()['propertyIds'])) {
foreach ($lineItem->getPayload()['propertyIds'] as $propertyId) {
foreach ($teasersArray as $item) {
if ($item->getId() == $propertyId) {
$hasProp = true;
}
}
}
}
if ($hasProp == false) {
$newLineItems->add($lineItem);
$newAdded = true;
}
}
if ($newAdded && $newLineItems) {
$cart->setLineItems($newLineItems);
$token = $cart->getToken();
$connection = $this->container->get(Connection::class);
$sql = "UPDATE cart SET cart = :cart WHERE token = '$token'";
$connection->executeStatement($sql, ['cart' => $this->serializeCart($cart)]);
}
}
private function sort_objects_by_total($a, $b)
{
if ($a->getName() == $b->getName()) {
return 0;
}
return ($a->getName() < $b->getName()) ? -1 : 1;
}
private function sort_objects_by_position($a, $b)
{
if ($a->getPosition() == $b->getPosition()) {
return 0;
}
return ($a->getPosition() < $b->getPosition()) ? -1 : 1;
}
private function serializeCart(Cart $cart): string
{
$errors = $cart->getErrors();
$data = $cart->getData();
$cart->setErrors(new ErrorCollection());
$cart->setData(null);
$serializedCart = \serialize($cart);
$cart->setErrors($errors);
$cart->setData($data);
return $serializedCart;
}
}