custom/plugins/MetalementsTheme/src/Subscriber/OrderCompletedSubscriber.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MetalementsTheme\Subscriber;
  3. use Shopware\Core\Checkout\Order\OrderEvents;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. class OrderCompletedSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface
  13.      */
  14.     private $productRepository;
  15.     public function __construct(EntityRepositoryInterface $productRepository) {
  16.         $this->productRepository $productRepository;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             OrderStateMachineStateChangeEvent::class => 'onOrderCompleted'
  22.         ];
  23.     }
  24.     public function onOrderCompleted(OrderStateMachineStateChangeEvent $event): void
  25.     {
  26.         if ($event->getToPlace()->getTechnicalName() !== 'completed') {
  27.             return;
  28.         }
  29.         $order $event->getOrder();
  30.         $context $event->getContext();
  31.         foreach ($order->getLineItems() as $lineItem) {
  32.             $customFields $lineItem->getCustomFields();
  33.             if ($customFields && isset($customFields['wct_custom_field_single_parts'])) {
  34.                 $singleParts explode(';'$customFields['wct_custom_field_single_parts']);
  35.                 foreach ($singleParts as $singlePart) {
  36.                     if (strlen($singlePart) > && substr_count($singlePart':') == 2) {
  37.                         $singlePartParts explode(':'$singlePart);
  38.                         $singlePartProductNumber $singlePartParts[0];
  39.                         $singlePartRequired intval($singlePartParts[1]);
  40.                         $singlePartAmount intval($singlePartParts[2]);
  41.                         if ($singlePartRequired == 1) {
  42.                             $criteria = new Criteria();
  43.                             $criteria->addFilter(new EqualsFilter('productNumber'$singlePartProductNumber));
  44.                             $product $this->productRepository->search($criteria$context)->first();
  45.                             if ($product !== null) {
  46.                                 $currentStock $product->getStock();
  47.                                 $newStock max($currentStock $singlePartAmount0);
  48.                                 $updateData = [
  49.                                     'id' => $product->getId(),
  50.                                     'stock' => $newStock
  51.                                 ];
  52.                                 $this->productRepository->update([$updateData], $context);
  53.                             }
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }