custom/plugins/NxsDeliveryTimesDisplay/src/Subscriber/LineItemAddedSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nxs\NxsDeliveryTimesDisplay\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class LineItemAddedSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var EntityRepositoryInterface
  12.      */
  13.     private $entityRepository;
  14.     public function __construct(EntityRepositoryInterface $entityRepository)
  15.     {
  16.         $this->entityRepository $entityRepository;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             LineItemAddedEvent::class => 'onLineItemAdded'
  22.         ];
  23.     }
  24.     /**
  25.      * @param LineItemAddedEvent $event
  26.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  27.      */
  28.     public function onLineItemAdded(LineItemAddedEvent $event)
  29.     {
  30.         /** @var LineItem $lineItem */
  31.         foreach ($event->getCart()->getLineItems()->getElements() as $lineItem){
  32.             if ($lineItem->getType() === 'product' && $lineItem->getId() === $event->getLineItem()->getId()){
  33.                 $product $this->entityRepository->search(new Criteria([$lineItem->getId()]), $event->getContext()->getContext())->first();
  34.                 $nxsAdditionalDeliveryTime = isset($product->getCustomFields()['nxs_additional_delivery_time']) ? $product->getCustomFields()['nxs_additional_delivery_time'] : null;
  35.                 if (!empty($nxsAdditionalDeliveryTime)){
  36.                     $lineItem->assign(['nxs_additional_delivery_time' => $nxsAdditionalDeliveryTime]);
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }