custom/plugins/WbfkITScope/src/Subscriber/UpdatePurchasePriceOfBundleOnChildPurchasePriceUpdate.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\ITScope\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Exception;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Wbfk\ITScope\Service\CheapestBundlePurchasePriceUpdaterService;
  11. class UpdatePurchasePriceOfBundleOnChildPurchasePriceUpdate implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private readonly CheapestBundlePurchasePriceUpdaterService $cheapestBundlePurchasePriceUpdaterService,
  15.         private readonly Connection $connection,
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWritten',
  22.         ];
  23.     }
  24.     /**
  25.      * @throws \Doctrine\DBAL\Driver\Exception
  26.      * @throws Exception
  27.      */
  28.     public function onProductWritten(EntityWrittenEvent $event): void
  29.     {
  30.         $productIds $this->findPurchasePriceUpdatedProductIds($event->getPayloads());
  31.         if (empty($productIds)) {
  32.             return;
  33.         }
  34.         $bundleParentProductIds $this->findBundleParentProductIds($productIds);
  35.         if (empty($bundleParentProductIds)) {
  36.             return;
  37.         }
  38.         foreach ($bundleParentProductIds as $bundleParentProductId) {
  39.             $this->cheapestBundlePurchasePriceUpdaterService->updateBestPurchasePriceForBundleProductId($bundleParentProductId);
  40.         }
  41.     }
  42.     public function findPurchasePriceUpdatedProductIds(array $payloads): array
  43.     {
  44.         return array_filter(array_map(static function ($payload) {
  45.             return isset($payload['purchasePrices']) ? $payload['id'] : false;
  46.         }, $payloads));
  47.     }
  48.     /**
  49.      * @throws Exception
  50.      * @throws \Doctrine\DBAL\Driver\Exception
  51.      */
  52.     private function findBundleParentProductIds(array $childProductIds): array
  53.     {
  54.         $childProductIdsHex implode(","array_map(fn($childProductId) => '0x'.$childProductId$childProductIds));
  55.         $liveVersionIdHex '0x'.Defaults::LIVE_VERSION;
  56.         $parentProductIdQuery = <<<SQL
  57.             SELECT DISTINCT LOWER(HEX(product_id)) AS product_id
  58.             FROM wbfk_bundle_product
  59.             WHERE child_product_id IN ($childProductIdsHex)
  60.               AND product_version_id = $liveVersionIdHex
  61.             SQL;
  62.         $bundleProductRows $this->connection->executeQuery($parentProductIdQuery)->fetchAllAssociative();
  63.         return array_map(fn($bundleProductRow) => $bundleProductRow['product_id'], $bundleProductRows);
  64.     }
  65. }