custom/plugins/OkeonlineKejeRemoveProductsHotfix/src/Core/Subscribers/RemoveOrderLineLinksBeforeProductDeleteEventSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Okeonline\KejeRemoveProductsHotfix\Core\Subscribers;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemDefinition;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\SetNullOnDeleteCommand;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class RemoveOrderLineLinksBeforeProductDeleteEventSubscriber implements EventSubscriberInterface
  11. {
  12.     /** @var Connection $connection */
  13.     private $connection;
  14.     public function __construct(
  15.         Connection $connection
  16.     )
  17.     {
  18.         $this->connection $connection;
  19.     }
  20.    
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             PreWriteValidationEvent::class => ['removeOrderLineItemLinks'0]
  25.         ];
  26.     }
  27.     public function removeOrderLineItemLinks(PreWriteValidationEvent $event)
  28.     {
  29.         // get and check main command
  30.         if( ! $mainCommand $this->checkIfMainCommandIsProductDeleteCommand($event->getCommands()))
  31.             return;
  32.         $productPrimaryKeys $mainCommand->getPrimaryKey();
  33.         // get the key (and check if situation exists) of the setNullOnDeleteCommand for OrderLineItems on productDeletion
  34.         if( ! $removeArrayKey $this->checkIfOrderlineCascadeDeleteIsSet($event->getCommands()))
  35.             return;
  36.         dump($removeArrayKey);
  37.         // unset the key for the OrderLine-command, preventing the query that can be enourmous and problematic
  38.         unset($event->getCommands()[$removeArrayKey]);
  39.         // manually execute the setNullOnDeleteCommand
  40.         $this->connection->executeUpdate(
  41.             'UPDATE `order_line_item` SET product_id = null, product_version_id = null WHERE product_id = :deletedProductId;',
  42.             [
  43.                 'deletedProductId' => $productPrimaryKeys['id'],
  44.                 'deletedProductVersionId' => $productPrimaryKeys['version_id'],
  45.             ]
  46.         );
  47.     }
  48.     private function checkIfMainCommandIsProductDeleteCommand(array $commands)
  49.     {
  50.         /** @var DeleteCommand $command */
  51.         foreach($commands as $command)
  52.         {
  53.             if($command instanceof DeleteCommand && $command->getDefinition() instanceof ProductDefinition)
  54.                 return $command;
  55.         }
  56.         return false;
  57.     }
  58.     private function checkIfOrderlineCascadeDeleteIsSet(array $commands)
  59.     {
  60.         /** @var SetNullOnDeleteCommand $command */
  61.         foreach($commands as $key => $command)
  62.         {
  63.             if($command instanceof SetNullOnDeleteCommand && $command->getDefinition() instanceof OrderLineItemDefinition)
  64.                 return $key;
  65.         }
  66.         return false;
  67.     }
  68. }