custom/plugins/BssCheckoutCustomField/src/Subscriber/OrderPlacedEventSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Bss\CheckoutCustomField\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. class OrderPlacedEventSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityRepositoryInterface
  15.      */
  16.     private $orderRepository;
  17.     /**
  18.      * @var RequestStack
  19.      */
  20.     private $request;
  21.     /**
  22.      * @var SystemConfigService
  23.      */
  24.     protected $systemConfig;
  25.     public function __construct(
  26.         SystemConfigService $systemConfig,
  27.         RequestStack $request,
  28.         EntityRepositoryInterface $orderRepository
  29.     )
  30.     {
  31.         $this->systemConfig $systemConfig;
  32.         $this->request $request;
  33.         $this->orderRepository $orderRepository;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             CheckoutOrderPlacedEvent::class => 'orderPlaced',
  39.             MailBeforeValidateEvent::class => 'sendMail'
  40.         ];
  41.     }
  42.     public function sendMail(MailBeforeValidateEvent $event): void
  43.     {
  44.         if ($this->systemConfig->get('BssCheckoutCustomField.config.active')) {
  45.             $templateData $event->getTemplateData();
  46.             $orderNumber 0;
  47.             if (isset($templateData['order'])) {
  48.                 if (is_object($templateData['order'])) {
  49.                     $orderNumber $templateData['order']->getOrderNumber();
  50.                 } else {
  51.                     if (isset($templateData['order']['orderNumber'])) {
  52.                         $orderNumber $templateData['order']['orderNumber'];
  53.                     }
  54.                 }
  55.             }
  56.             $criteria = new Criteria();
  57.             $criteria->addFilter(new EqualsFilter('orderNumber'$orderNumber));
  58.             $order $this->orderRepository->search($criteria$event->getContext())->first();
  59.             if (!$order) {
  60.                 return;
  61.             }
  62.             
  63.             $customFields $order->getCustomFields();
  64.             if (isset($customFields['bssCustomFields']) && $customFields['bssCustomFields']) {
  65.                 $templateData['bssCustomFields'] = $customFields['bssCustomFields'];
  66.             }
  67.             $event->setTemplateData($templateData);
  68.         }
  69.     }
  70.     public function orderPlaced(CheckoutOrderPlacedEvent $event): void
  71.     {
  72.         if ($this->systemConfig->get('BssCheckoutCustomField.config.active')) {
  73.             $currentRequest $this->request->getCurrentRequest();
  74.             $requests $currentRequest->request;
  75.             $data = [];
  76.             $index 0;
  77.             foreach ($requests as $key => $request) {
  78.                 if ($request != "" && strpos($key'_bsscf_') !== false) {
  79.                     $newKeys explode("_bsscf_"$key);
  80.                     $label $this->getKeyLevel($newKeys1);
  81.                     $title $this->getKeyLevel($newKeys2);
  82.                     $type $this->getKeyLevel($newKeys3);
  83.                     !isset($data[$label]) ? $data[$label] = [] : null;
  84.                     !isset($data[$label][$index]) ? $data[$label][$index] = [] : null;
  85.                     $data[$label][$index]['data'] = $request;
  86.                     $data[$label][$index]['type'] = $type;
  87.                     $data[$label][$index]['title'] = $title;
  88.                     $index++;
  89.                 }
  90.             }
  91.             if (!empty($data)) {
  92.                 $this->orderRepository->update([
  93.                     [
  94.                         'id' => $event->getOrder()->getId(),
  95.                         'customFields' => [
  96.                             'bssCustomFields' => $data,
  97.                         ]
  98.                     ]
  99.                 ], $event->getContext());
  100.             }
  101.         }
  102.     }
  103.     public function getKeyLevel($newKeys$level)
  104.     {
  105.         $keyLevel '';
  106.         foreach ($newKeys as $newKey) {
  107.             if (strpos($newKey'bsscflabel_') !== false && $level == 1) {
  108.                 $keyLevel str_replace('bsscflabel_'''$newKey);
  109.             }
  110.             if (strpos($newKey'bsscflobalf_') !== false && $level == 2) {
  111.                 $keyLevel str_replace('bsscflobalf_'''$newKey);
  112.             }
  113.             if (strpos($newKey'bsscfname_') !== false && $level == 3) {
  114.                 $keyLevel str_replace('bsscfname_'''$newKey);
  115.             }
  116.         }
  117.         return $keyLevel;
  118.     }
  119. }