custom/plugins/FourtwosixThemeCustomization/src/Subscriber/ProductSoldOutSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixThemeCustomization\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. class ProductSoldOutSubscriber implements EventSubscriberInterface
  12. {
  13.     private Connection $connection;
  14.     public function __construct(
  15.         Connection $connection,
  16.     ) {
  17.         $this->connection $connection;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  23.         ];
  24.     }
  25.     public function onProductsLoaded(EntityLoadedEvent $event): void
  26.     {
  27.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  28.             return;
  29.         }
  30.         $products $event->getEntities();
  31.         /** @var ProductEntity $product */
  32.         foreach ($products as $product) {
  33.             $soldOutPDP $product->getAvailableStock() <= && $product->getIsCloseout();
  34.             $soldOutPLP $soldOutPDP;
  35.             $parentId null;
  36.             if ($product->getChildCount()) {
  37.                 $parentId $product->getId();
  38.             } elseif ($product->getParentId()) {
  39.                 $parentId $product->getParentId();
  40.             }
  41.             if ($parentId) {
  42.                 $variants $this->getVariants($parentId);
  43.                 foreach ($variants as $variant) {
  44.                     if ($variant['is_closeout'] === null) {
  45.                         $variant['is_closeout'] = $variant['parent_is_closeout'];
  46.                     }
  47.                     $soldOutPLP $variant['available_stock'] <= && $variant['is_closeout'] > 0;
  48.                     if (!$soldOutPLP) {
  49.                         break;
  50.                     }
  51.                 }
  52.             }
  53.             $this->setSoldOut($product$soldOutPLP$soldOutPDP);
  54.         }
  55.     }
  56.     private function getVariants(string $parentId): array
  57.     {
  58.         $query "SELECT p.available_stock, p.is_closeout, pp.is_closeout as 'parent_is_closeout'
  59.                     FROM  `product` p 
  60.                     INNER JOIN `product` pp ON p.parent_id = pp.id
  61.                     WHERE p.parent_id = ?";
  62.         return $this->connection->executeQuery($query, [Uuid::fromHexToBytes($parentId)])->fetchAllAssociative();
  63.     }
  64.     private function setSoldOut(ProductEntity $productbool $soldOutPLPbool $soldOutPDP): void
  65.     {
  66.         $extensions $product->getExtensions();
  67.         $extensions['isSoldOutPLP'] = $soldOutPLP;
  68.         $extensions['isSoldOutPDP'] = $soldOutPDP;
  69.         $product->setExtensions($extensions);
  70.     }
  71. }