custom/plugins/ZeobvBundleProducts/src/Core/Subscriber/OrderLineItemQuantitySubscriber.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Zeobv\BundleProducts\Core\Subscriber;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. class OrderLineItemQuantitySubscriber implements EventSubscriberInterface
  13. {
  14.     protected EntityRepositoryInterface $orderLineItemRepository;
  15.     public function __construct(
  16.         EntityRepositoryInterface $orderLineItemRepository
  17.     ) {
  18.         $this->orderLineItemRepository $orderLineItemRepository;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             OrderEvents::ORDER_LINE_ITEM_WRITTEN_EVENT => 'onOrderLineItemWritten',
  24.         ];
  25.     }
  26.     public function onOrderLineItemWritten(EntityWrittenEvent $event): void
  27.     {
  28.         /** @var EntityWriteResult $writeResult */
  29.         foreach ($event->getWriteResults() as $writeResult) {
  30.             if (
  31.                 $writeResult->getOperation() !== EntityWriteResult::OPERATION_UPDATE
  32.                 || !$writeResult->hasPayload('quantity')
  33.                 || $writeResult->getProperty('quantity') <= 1
  34.             ) {
  35.                 continue;
  36.             }
  37.             /** @var array|string $id */
  38.             $id $writeResult->getPrimaryKey();
  39.             $id = !is_array($id) ? [$id] : $id;
  40.             $criteria = new Criteria();
  41.             $criteria->addFilter(new EqualsAnyFilter('parentId'$id));
  42.             $result $this->orderLineItemRepository->search($criteria$event->getContext());
  43.             if ($result->getTotal() <= 0) {
  44.                 continue;
  45.             }
  46.             $orderLineItemPatchData = [];
  47.             /** @var OrderLineItemEntity $item */
  48.             foreach ($result as $item) {
  49.                 $payload $item->getPayload();
  50.                 if (!is_array($payload) || !key_exists('quantity'$payload)) {
  51.                     continue 2;
  52.                 }
  53.                 $orderLineItemPatchData[] = [
  54.                     'id' => $item->getId(),
  55.                     'quantity' => $payload['quantity'] * $writeResult->getProperty('quantity')
  56.                 ];
  57.             }
  58.             if (empty($orderLineItemPatchData)) {
  59.                 continue;
  60.             }
  61.             $this->orderLineItemRepository->update($orderLineItemPatchData$event->getContext());
  62.         }
  63.     }
  64. }