src/PentagastBundle/Subscriber/OrderLineSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * valantic CEC Deutschland GmbH
  4.  * Copyright (c) 2022.
  5.  *
  6.  * All rights reserved.
  7.  *
  8.  * @see https://www.valantic.com/
  9.  *
  10.  * @author andrei
  11.  * Date: 21.11.2022 14:24
  12.  */
  13. namespace Valantic\Bundle\PentagastBundle\Subscriber;
  14. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  15. use Shopware\Core\Checkout\Order\OrderEvents;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Valantic\Bundle\PentagastBundle\Struct\PurchasePrice;
  19. class OrderLineSubscriber implements EventSubscriberInterface
  20. {
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             OrderEvents::ORDER_LINE_ITEM_LOADED_EVENT => ['onOrderLineItemLoaded', -250],
  25.         ];
  26.     }
  27.     public function onOrderLineItemLoaded(EntityLoadedEvent $event): void
  28.     {
  29.         $eventEntities $event->getEntities();
  30.         /** @var OrderLineItemEntity $orderLineItem */
  31.         foreach ($eventEntities as $orderLineItem) {
  32.             $purchasePriceValue 0;
  33.             $targetOrderProductLine $orderLineItem;
  34.             if ($orderLineItem->getType() === 'customized-products') {
  35.                 $filterResult array_filter($eventEntities, function (OrderLineItemEntity $lineItem) use ($orderLineItem) {
  36.                     return $lineItem->getType() === 'product' && $lineItem->getParentId() === $orderLineItem->getId();
  37.                 });
  38.                 if ($filterResult) {
  39.                     $targetOrderProductLine array_values($filterResult)[0];
  40.                 }
  41.             }
  42.             if ($targetOrderProductLine) {
  43.                 $targetPayload $targetOrderProductLine->getPayload();
  44.                 $purchasePrices json_decode($targetPayload['purchasePrices'] ?? ''true);
  45.                 if ($purchasePrices) {
  46.                     $purchasePriceValue $purchasePrices['calculatedPrice'] ?? 0;
  47.                 }
  48.             }
  49.             $purchasePrice = new PurchasePrice();
  50.             $purchasePrice->setValue($purchasePriceValue);
  51.             /** @psalm-suppress DeprecatedMethod */
  52.             $orderLineItem->addExtension('purchasePrice'$purchasePrice);
  53.         }
  54.     }
  55. }