custom/plugins/HuebertAddOrderAttributes/src/Subscriber/OrderCreateSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HuebertAddOrderAttributes\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class OrderCreateSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface
  13.      */
  14.     protected $orderRepository;
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     protected $pluginConfig;
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     protected $productRepository;
  23.     public function __construct(
  24.         EntityRepositoryInterface $orderRepository,
  25.         SystemConfigService $pluginConfig,
  26.         EntityRepositoryInterface $productRepository
  27.     )
  28.     {
  29.         $this->orderRepository $orderRepository;
  30.         $this->pluginConfig $pluginConfig;
  31.         $this->productRepository $productRepository;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             CheckoutOrderPlacedEvent::class => 'onWrittenOrder'
  37.         ];
  38.     }
  39.     public function onWrittenOrder(CheckoutOrderPlacedEvent $event){
  40.         $salesChannelId $event->getSalesChannelId();
  41.         $showInCustomerComment $this->pluginConfig->get('HuebertAddOrderAttributes.config.showInCustomerComment'$salesChannelId);
  42.         $lineItems $event->getOrder()->getLineItems();
  43.         $comment $event->getOrder()->getCustomerComment();
  44.         $currencySimbol $event->getOrder()->getCurrency()->getSymbol();
  45.         if($showInCustomerComment) {
  46.             foreach ($lineItems as $lineItem) {
  47.                 $critera = new Criteria();
  48.                 $critera->addFilter(new EqualsFilter('id'$lineItem->getProductId()));
  49.                 $productName $this->productRepository->search($critera$event->getContext())->first();
  50.                 if($productName) {
  51.                     if(isset($productName->getTranslated()['name'])) {
  52.                         $productName $productName->getTranslated()['name'];
  53.                     } else {
  54.                         $productName $productName->getName();
  55.                     }
  56.                 }
  57.                 if (array_key_exists('huebert_attributes'$lineItem->getPayload())) {
  58.                     $attributes $lineItem->getPayload()['huebert_attributes'];
  59.                     $comment .= PHP_EOL.$productName PHP_EOL;
  60.                     foreach ($attributes as $attribute) {
  61.                         foreach ($attribute as $attr) {
  62.                             if(is_array($attr)) {
  63.                                 foreach ($attr as $a) {
  64.                                     if(is_array($a)) {
  65.                                         if (array_key_exists('value'$a)) {
  66.                                         if (is_array($a['value'])) {
  67.                                             if (array_key_exists('attributes_with_prices'$a['value'])) {
  68.                                                 foreach ($a['value']['attributes_with_prices'] as $key => $value) {
  69.                                                     $comment .= PHP_EOL $key ': ' PHP_EOL;
  70.                                                     foreach ($a['value']['attributes_with_prices'][$key] as $k => $v) {
  71.                                                         $comment .= PHP_EOL $k ': ' $v['price'] . ' ' $currencySimbol PHP_EOL;
  72.                                                     }
  73.                                                 }
  74.                                             }
  75.                                         } else {
  76.                                             $comment .= PHP_EOL $a['name'] . ': ' $a['value'] . PHP_EOL;
  77.                                         }
  78.                                     }
  79.                                 }
  80.                             }
  81.                                 if(isset($attr['value']) && !is_array($attr['value'])){
  82.                                     $comment .= PHP_EOL $attr['name'] . ': ' $attr['value'] . PHP_EOL;
  83.                                 }
  84.                         }
  85.                         }
  86.                     }
  87.                 }
  88.             }
  89.             $this->orderRepository->update([[
  90.                 'id' => $event->getOrder()->getId(),
  91.                 'customerComment' => $comment,
  92.             ]], $event->getContext());
  93.         }
  94.     }
  95. }