custom/plugins/SisiBlog6/src/Storefront/Page/Blog/Products/ProductsLoader.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sisi\Blog6\Storefront\Page\Blog\Products;
  4. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  5. use Shopware\Core\Content\Product\ProductCollection;
  6. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  10. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. class ProductsLoader
  14. {
  15.     /**
  16.      * @var SalesChannelRepositoryInterface
  17.      */
  18.     private $productRepository;
  19.     /**
  20.      * @var SystemConfigService
  21.      */
  22.     private $systemConfigService;
  23.     public function __construct(
  24.         SalesChannelRepositoryInterface $productRepository,
  25.         SystemConfigService $systemConfigService
  26.     ) {
  27.         $this->productRepository $productRepository;
  28.         $this->systemConfigService $systemConfigService;
  29.     }
  30.     /**
  31.      * @param ProductCollection<\Shopware\Core\Content\Product\ProductEntity>|null $products
  32.      * @return ProductCollection<\Shopware\Core\Content\Product\ProductEntity>|null
  33.      */
  34.     public function load(?ProductCollection $productsSalesChannelContext $context): ?ProductCollection
  35.     {
  36.         $productsList = new ProductCollection();
  37.         if ($products) {
  38.             $assignedProductsIds $products->getIds();
  39.             $filter = new ProductAvailableFilter(
  40.                 $context->getSalesChannel()->getId(),
  41.                 ProductVisibilityDefinition::VISIBILITY_LINK
  42.             );
  43.             $criteria = new Criteria($assignedProductsIds);
  44.             $criteria->addAssociation('visibilities');
  45.             $criteria->addFilter($filter);
  46.             if (!count($assignedProductsIds)) {
  47.                 return null;
  48.             }
  49.             $criteria $this->handleAvailableStock($criteria$context);
  50.             /**
  51.              * @var ProductCollection<\Shopware\Core\Content\Product\ProductEntity>|null $searchResult
  52.              */
  53.             $searchResult $this->productRepository->search($criteria$context);
  54.             foreach ($products->getElements() as $element) {
  55.                 if ($searchResult->has($element->getId())) {
  56.                     $productsList->add($searchResult->get($element->getId()));
  57.                 }
  58.             }
  59.         }
  60.         return $productsList;
  61.     }
  62.     private function handleAvailableStock(Criteria $criteriaSalesChannelContext $context): Criteria
  63.     {
  64.         $salesChannelId $context->getSalesChannel()->getId();
  65.         $hide $this->systemConfigService->get('core.listing.hideCloseoutProductsWhenOutOfStock'$salesChannelId);
  66.         if (!$hide) {
  67.             return $criteria;
  68.         }
  69.         $criteria->addFilter(
  70.             new NotFilter(
  71.                 NotFilter::CONNECTION_AND,
  72.                 [
  73.                     new EqualsFilter('product.isCloseout'true),
  74.                     new EqualsFilter('product.available'false),
  75.                 ]
  76.             )
  77.         );
  78.         return $criteria;
  79.     }
  80. }