custom/plugins/WbfkExtensions/src/Subscriber/AddSkontoFromCustomerToOrder.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class AddSkontoFromCustomerToOrder implements EventSubscriberInterface
  7. {
  8.     public static function getSubscribedEvents(): array
  9.     {
  10.         return [
  11.             CartConvertedEvent::class => 'addDeliveryTimeDataToConvertedCart',
  12.         ];
  13.     }
  14.     public function addDeliveryTimeDataToConvertedCart(CartConvertedEvent $event): void
  15.     {
  16.         $convertedCart $event->getConvertedCart();
  17.         $cartCustomFields $convertedCart['customFields'] ?? [];
  18.         $customerCustomFields $event->getSalesChannelContext()->getCustomer()->getCustomFields();
  19.         if (!$customerCustomFields) {
  20.             return;
  21.         }
  22.         $skontoCustomFields = [
  23.             'wbfk_invoice_discount',
  24.             'wbfk_invoice_discount_days',
  25.             'wbfk_invoice_payment_target'
  26.         ];
  27.         foreach ($skontoCustomFields as $key) {
  28.             if (!isset($customerCustomFields[$key]) || $customerCustomFields[$key] === '') {
  29.                 continue;
  30.             }
  31.             $cartCustomFields[$key] = $customerCustomFields[$key];
  32.         }
  33.         $convertedCart['customFields'] = $cartCustomFields;
  34.         $event->setConvertedCart($convertedCart);
  35.     }
  36. }