custom/plugins/b2bsellerscore/src/Components/Checkout/Subscriber/LineItemCommentSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Checkout\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class LineItemCommentSubscriber implements EventSubscriberInterface
  7. {
  8.     private RequestStack $requestStack;
  9.     public function __construct(RequestStack $requestStack)
  10.     {
  11.         $this->requestStack $requestStack;
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             BeforeLineItemAddedEvent::class => 'addLineItemComment'
  17.         ];
  18.     }
  19.     public function addLineItemComment(BeforeLineItemAddedEvent $event)
  20.     {
  21.         $request $this->requestStack->getCurrentRequest();
  22.         if (empty($request)) {
  23.             return;
  24.         }
  25.         $lineItems $request->get('items'null);
  26.         if(empty($items)) {
  27.             $lineItems $request->get('lineItems'null);
  28.         }
  29.         if(empty($lineItems)) {
  30.             return;
  31.         }
  32.         $id $event->getLineItem()->getReferencedId();
  33.         if(empty($lineItems[$id])) {
  34.             return;
  35.         }
  36.         $lineItem $lineItems[$id];
  37.         $comment $lineItem['comment'] ?? null;
  38.         if(empty($comment)) {
  39.             return;
  40.         }
  41.         $lineItem $event->getLineItem();
  42.         if($event->isMerged()) {
  43.             $lineItem $event->getCart()->getLineItems()->get($lineItem->getId());
  44.         }
  45.         $payload $lineItem->getPayload();
  46.         $payload['comment'] = $comment;
  47.         $lineItem->setPayload($payload);
  48.     }
  49. }