<?php
namespace CioBudget\Subscriber;
use CioBudget\Service\BudgetLoaderService;
use CioBudget\Service\SessionService;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BudgetShiftOrderPlacedCustomFieldSubscriber implements EventSubscriberInterface
{
public const CUSTOM_FIELD_BUDGET_NAME_SAVE_ON_ORDER = 'custom_field_budget_name_save_on_order';
private EntityRepositoryInterface $paymentMethodRepository;
private BudgetLoaderService $budgetLoaderService;
private SessionService $sessionService;
private EntityRepositoryInterface $orderRepository;
public function __construct(
EntityRepositoryInterface $paymentMethodRepository,
BudgetLoaderService $budgetLoaderService,
SessionService $sessionService,
EntityRepositoryInterface $orderRepository
)
{
$this->paymentMethodRepository = $paymentMethodRepository;
$this->budgetLoaderService = $budgetLoaderService;
$this->sessionService = $sessionService;
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents(): array
{
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onCheckoutFinishPageLoaded'
];
}
public function onCheckoutFinishPageLoaded(EntityWrittenEvent $event)
{
// if context is admin do nothing
if ($event->getContext()->getSource() instanceof AdminApiSource) {
return;
}
$writeResults = $event->getWriteResults();
if (!empty($writeResults) && isset($writeResults[0]) && isset($writeResults[0]->getPayload()['id'])) {
$orderId = $writeResults[0]->getPayload()['id'];
// Überprüfen, ob $orderId nicht leer ist
if (!empty($orderId)) {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('id', $orderId))
->addAssociation('transactions');
$order = $this->orderRepository->search($criteria, $event->getContext())->first();
$paymentMethods = $order->getTransactions()->getPaymentMethodIds();
foreach ($paymentMethods as $paymentMethod) {
/** @var PaymentMethodEntity $paymentMethod */
if ($paymentMethod === $this->getBudgetPaymentMethodId()) {
$budget = $this->budgetLoaderService->getBudgetById($this->sessionService->getBudgetIdFromSession());
if (!is_null($budget)) {
$customFields = $order->getCustomFields() ?? [];
if (!array_key_exists(self::CUSTOM_FIELD_BUDGET_NAME_SAVE_ON_ORDER, $customFields)) {
$customFields[self::CUSTOM_FIELD_BUDGET_NAME_SAVE_ON_ORDER] = $budget->getName();
$data = [
[
'id' => $order->getId(),
'customFields' => $customFields
]
];
$this->orderRepository->upsert($data, $event->getContext());
}
}
}
}
}
}
}
private function getBudgetPaymentMethodId(): ?string
{
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('name', 'budget'));
return $this->paymentMethodRepository->searchIds($paymentCriteria, Context::createDefaultContext())->firstId();
}
}