custom/plugins/AcrisProductCustomerGroupCS/src/Core/Content/Product/SalesChannel/SalesChannelProductRepository.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductCustomerGroup\Core\Content\Product\SalesChannel;
  3. use Acris\ProductCustomerGroup\Components\BlockProductService;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  13. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  14. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. class SalesChannelProductRepository extends SalesChannelRepository
  18. {
  19.     private SalesChannelRepositoryInterface $parent;
  20.     private BlockProductService $blockProductService;
  21.     private SystemConfigService $configService;
  22.     public function __construct(
  23.         SalesChannelRepositoryInterface $parent,
  24.         BlockProductService $blockProductService,
  25.         SystemConfigService $configService
  26.     ) {
  27.         $this->parent $parent;
  28.         $this->blockProductService $blockProductService;
  29.         $this->configService $configService;
  30.     }
  31.     /**
  32.      * @throws InconsistentCriteriaIdsException
  33.      */
  34.     public function search(Criteria $criteriaSalesChannelContext $salesChannelContext): EntitySearchResult
  35.     {
  36.         $this->blockProductsForCustomerGroup($criteria$salesChannelContext);
  37.         return $this->parent->search($criteria$salesChannelContext);
  38.     }
  39.     public function aggregate(Criteria $criteriaSalesChannelContext $salesChannelContext): AggregationResultCollection
  40.     {
  41.         return $this->parent->aggregate($criteria$salesChannelContext);
  42.     }
  43.     public function searchIds(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  44.     {
  45.         $this->blockProductsForCustomerGroup($criteria$salesChannelContext);
  46.         return $this->parent->searchIds($criteria$salesChannelContext);
  47.     }
  48.     private function blockProductsForCustomerGroup(Criteria $criteriaSalesChannelContext $salesChannelContext): void
  49.     {
  50.         $blockedProductIds $this->blockProductService->getBlockedProductIdsForCustomerGroupId($salesChannelContext->getCurrentCustomerGroup()->getId(), $salesChannelContext->getContext());
  51.         $filters = [];
  52.         if($this->configService->get('AcrisProductCustomerGroupCS.config.blockProductsIfNoCustomerGroupAssigned'$salesChannelContext->getSalesChannel()->getId()) === BlockProductService::DEFAULT_PLUGIN_CONFIG_BLOCK_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED) {
  53.             $criteria->addAssociation('product.acrisBlockCustomerGroup');
  54.             $filters[] = new NotFilter(NotFilter::CONNECTION_AND, [
  55.                 new EqualsFilter('product.acrisBlockCustomerGroup.id'null)
  56.             ]);
  57.         }
  58.         if (!empty($blockedProductIds)) {
  59.             $filters[] = new NotFilter(NotFilter::CONNECTION_AND, [new EqualsAnyFilter('id'$blockedProductIds)]);
  60.         }
  61.         if (!empty($filters)) {
  62.             $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR$filters));
  63.         }
  64.     }
  65. }