custom/plugins/b2bsellerscore/addons/B2bOffer/Subscriber/ProductDeletionSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace B2bOffer\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Product\ProductDefinition;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\BeforeDeleteEvent;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ProductDeletionSubscriber implements EventSubscriberInterface
  11. {
  12.     private Connection $connection;
  13.     private SystemConfigService $configService;
  14.     public function __construct(
  15.         Connection $connection,
  16.         SystemConfigService $configService
  17.     )
  18.     {
  19.         $this->connection $connection;
  20.         $this->configService $configService;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             BeforeDeleteEvent::class => 'convertOfferProductItemsBeforeDeletion'
  26.         ];
  27.     }
  28.     public function convertOfferProductItemsBeforeDeletion(BeforeDeleteEvent $event)
  29.     {
  30.         $offerStatusId $this->configService->get('B2bOffer.config.defaultInvalidOfferStatusId');
  31.         if(empty($offerStatusId) || !Uuid::isValid($offerStatusId)) {
  32.             return;
  33.         }
  34.         foreach ($event->getCommands() as $command) {
  35.             if($command->getEntityName() !== ProductDefinition::ENTITY_NAME) {
  36.                 continue;
  37.             }
  38.             $primaryKeys $command->getPrimaryKey();
  39.             if(isset($primaryKeys['version_id']) && Uuid::fromBytesToHex($primaryKeys['version_id']) !== Defaults::LIVE_VERSION) {
  40.                 continue;
  41.             }
  42.             if(!isset($primaryKeys['id'])) {
  43.                 continue;
  44.             }
  45.             $this->connection->executeStatement("
  46.                 UPDATE `b2b_offer_item`
  47.                 LEFT JOIN `b2b_offer` ON `b2b_offer_item`.`offer_id` = `b2b_offer`.`id`
  48.                 SET `b2b_offer_item`.`type` = 'custom', 
  49.                     `b2b_offer_item`.`label` = CONCAT(`b2b_offer_item`.`label`, ' (deleted)'),
  50.                     `b2b_offer_item`.`product_id` = NULL,
  51.                     `b2b_offer`.`status_id` = :statusId
  52.                 WHERE `b2b_offer_item`.`type` = 'product' AND `b2b_offer_item`.`product_id` = :productId
  53.               ", ['productId' => $primaryKeys['id'], 'statusId' => Uuid::fromHexToBytes($offerStatusId)]);
  54.         }
  55.     }
  56. }