<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddSkontoFromCustomerToOrder implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'addDeliveryTimeDataToConvertedCart',
];
}
public function addDeliveryTimeDataToConvertedCart(CartConvertedEvent $event): void
{
$convertedCart = $event->getConvertedCart();
$cartCustomFields = $convertedCart['customFields'] ?? [];
$customerCustomFields = $event->getSalesChannelContext()->getCustomer()->getCustomFields();
if (!$customerCustomFields) {
return;
}
$skontoCustomFields = [
'wbfk_invoice_discount',
'wbfk_invoice_discount_days',
'wbfk_invoice_payment_target'
];
foreach ($skontoCustomFields as $key) {
if (!isset($customerCustomFields[$key]) || $customerCustomFields[$key] === '') {
continue;
}
$cartCustomFields[$key] = $customerCustomFields[$key];
}
$convertedCart['customFields'] = $cartCustomFields;
$event->setConvertedCart($convertedCart);
}
}