vendor/shopware/core/Content/Product/SalesChannel/Detail/AvailableCombinationLoader.php line 53

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Detail;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. #[Package('inventory')]
  10. class AvailableCombinationLoader extends AbstractAvailableCombinationLoader
  11. {
  12.     /**
  13.      * @internal
  14.      */
  15.     public function __construct(private readonly Connection $connection)
  16.     {
  17.     }
  18.     public function getDecorated(): AbstractAvailableCombinationLoader
  19.     {
  20.         throw new DecorationPatternException(self::class);
  21.     }
  22.     public function load(string $productIdContext $contextstring $salesChannelId): AvailableCombinationResult
  23.     {
  24.         $query $this->connection->createQueryBuilder();
  25.         $query->from('product');
  26.         $query->leftJoin('product''product''parent''product.parent_id = parent.id');
  27.         $query->andWhere('product.parent_id = :id');
  28.         $query->andWhere('product.version_id = :versionId');
  29.         $query->andWhere('IFNULL(product.active, parent.active) = :active');
  30.         $query->andWhere('product.option_ids IS NOT NULL');
  31.         $query->setParameter('id'Uuid::fromHexToBytes($productId));
  32.         $query->setParameter('versionId'Uuid::fromHexToBytes($context->getVersionId()));
  33.         $query->setParameter('active'true);
  34.         $query->innerJoin('product''product_visibility''visibilities''product.visibilities = visibilities.product_id');
  35.         $query->andWhere('visibilities.sales_channel_id = :salesChannelId');
  36.         $query->setParameter('salesChannelId'Uuid::fromHexToBytes($salesChannelId));
  37.         $query->select([
  38.             'LOWER(HEX(product.id))',
  39.             'product.option_ids as options',
  40.             'product.product_number as productNumber',
  41.             'product.available',
  42.         ]);
  43.         $combinations $query->executeQuery()->fetchAllAssociative();
  44.         $combinations FetchModeHelper::groupUnique($combinations);
  45.         $result = new AvailableCombinationResult();
  46.         foreach ($combinations as $combination) {
  47.             try {
  48.                 $options json_decode((string) $combination['options'], true512\JSON_THROW_ON_ERROR);
  49.             } catch (\JsonException) {
  50.                 continue;
  51.             }
  52.             $available = (bool) $combination['available'];
  53.             $result->addCombination($options$available);
  54.         }
  55.         return $result;
  56.     }
  57. }