custom/plugins/MaxiaVariantsTable6/src/Service/ConfigService.php line 70

Open in your IDE?
  1. <?php
  2. namespace Maxia\MaxiaVariantsTable6\Service;
  3. use Maxia\MaxiaVariantsTable6\Core\Content\VariantsTable\PluginConfig;
  4. use Maxia\MaxiaVariantsTable6\Core\Content\VariantsTable\VariantsTableEntity;
  5. use Maxia\MaxiaVariantsTable6\Migration\Migration1631801584VariantsTable;
  6. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. class ConfigService
  13. {
  14.     /** @var string */
  15.     private $namespace;
  16.     /** @var SystemConfigService */
  17.     private $systemConfig;
  18.     /** @var PluginConfig */
  19.     private $baseConfig;
  20.     /** @var SalesChannelRepositoryInterface */
  21.     private $productRepository;
  22.     /** @var EntityRepositoryInterface */
  23.     private $variantsTableRepository;
  24.     public function __construct(
  25.         string $namespace,
  26.         SystemConfigService $systemConfig,
  27.         SalesChannelRepositoryInterface $productRepository,
  28.         EntityRepositoryInterface $variantsTableRepository
  29.     ) {
  30.         $this->namespace $namespace;
  31.         $this->systemConfig $systemConfig;
  32.         $this->productRepository $productRepository;
  33.         $this->variantsTableRepository $variantsTableRepository;
  34.     }
  35.     public function getPluginConfig($context): PluginConfig
  36.     {
  37.         if ($this->baseConfig === null) {
  38.             $config $this->systemConfig->get(
  39.                 $this->namespace,
  40.                 $context instanceof SalesChannelContext
  41.                     $context->getSalesChannel()->getId()
  42.                     : $context
  43.             );
  44.             $struct = new PluginConfig();
  45.             $struct->assign($config);
  46.             $this->baseConfig $struct;
  47.         }
  48.         return $this->baseConfig;
  49.     }
  50.     /**
  51.      * Returns the table config for a product with fallback to the default config.
  52.      *
  53.      * @param SalesChannelProductEntity $product
  54.      * @param SalesChannelContext $salesChannelContext
  55.      * @return VariantsTableEntity|null
  56.      */
  57.     public function getProductConfig(SalesChannelProductEntity $productSalesChannelContext $salesChannelContext): ?VariantsTableEntity
  58.     {
  59.         $customFields $product->getTranslation('customFields');
  60.         if (isset($customFields['maxia_variants_table_config_id'])
  61.             && !empty($customFields['maxia_variants_table_config_id']))
  62.         {
  63.             $configId $customFields['maxia_variants_table_config_id'];
  64.         } else {
  65.             $configId Migration1631801584VariantsTable::DEFAULT_CONFIG_ID;
  66.         }
  67.         $criteria = new Criteria();
  68.         $criteria->setIds([$configId]);
  69.         $criteria->setLimit(1);
  70.         $results $this->variantsTableRepository->search($criteria$salesChannelContext->getContext());
  71.         if (!$results->count()) {
  72.             $criteria->setIds([Migration1631801584VariantsTable::DEFAULT_CONFIG_ID]);
  73.             $results $this->variantsTableRepository->search($criteria$salesChannelContext->getContext());
  74.         }
  75.         if ($results->count()) {
  76.             /** @var VariantsTableEntity $entity */
  77.             $entity $results->first();
  78.             $entity->setHidePrices($this->shouldHidePrices($salesChannelContext));
  79.             if ($entity->isHidePrices()) {
  80.                 $entity->setShowBuyAllButton(false);
  81.             }
  82.             return $entity;
  83.         }
  84.         return null;
  85.     }
  86.     public function shouldHidePrices(SalesChannelContext $salesChannelContext): bool
  87.     {
  88.         $config $this->getPluginConfig($salesChannelContext);
  89.         if ($config->isHidePricesForGuests() && $salesChannelContext->getCustomer() === null) {
  90.             return true;
  91.         }
  92.         if ($config->getHidePricesForCustomerGroups() && $salesChannelContext->getCurrentCustomerGroup()) {
  93.             if (in_array($salesChannelContext->getCurrentCustomerGroup()->getId(), $config->getHidePricesForCustomerGroups())) {
  94.                 return true;
  95.             }
  96.         }
  97.         return false;
  98.     }
  99. }