<?php
namespace Maxia\MaxiaVariantsTable6\Service;
use Maxia\MaxiaVariantsTable6\Core\Content\VariantsTable\PluginConfig;
use Maxia\MaxiaVariantsTable6\Core\Content\VariantsTable\VariantsTableEntity;
use Maxia\MaxiaVariantsTable6\Migration\Migration1631801584VariantsTable;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class ConfigService
{
/** @var string */
private $namespace;
/** @var SystemConfigService */
private $systemConfig;
/** @var PluginConfig */
private $baseConfig;
/** @var SalesChannelRepositoryInterface */
private $productRepository;
/** @var EntityRepositoryInterface */
private $variantsTableRepository;
public function __construct(
string $namespace,
SystemConfigService $systemConfig,
SalesChannelRepositoryInterface $productRepository,
EntityRepositoryInterface $variantsTableRepository
) {
$this->namespace = $namespace;
$this->systemConfig = $systemConfig;
$this->productRepository = $productRepository;
$this->variantsTableRepository = $variantsTableRepository;
}
public function getPluginConfig($context): PluginConfig
{
if ($this->baseConfig === null) {
$config = $this->systemConfig->get(
$this->namespace,
$context instanceof SalesChannelContext
? $context->getSalesChannel()->getId()
: $context
);
$struct = new PluginConfig();
$struct->assign($config);
$this->baseConfig = $struct;
}
return $this->baseConfig;
}
/**
* Returns the table config for a product with fallback to the default config.
*
* @param SalesChannelProductEntity $product
* @param SalesChannelContext $salesChannelContext
* @return VariantsTableEntity|null
*/
public function getProductConfig(SalesChannelProductEntity $product, SalesChannelContext $salesChannelContext): ?VariantsTableEntity
{
$customFields = $product->getTranslation('customFields');
if (isset($customFields['maxia_variants_table_config_id'])
&& !empty($customFields['maxia_variants_table_config_id']))
{
$configId = $customFields['maxia_variants_table_config_id'];
} else {
$configId = Migration1631801584VariantsTable::DEFAULT_CONFIG_ID;
}
$criteria = new Criteria();
$criteria->setIds([$configId]);
$criteria->setLimit(1);
$results = $this->variantsTableRepository->search($criteria, $salesChannelContext->getContext());
if (!$results->count()) {
$criteria->setIds([Migration1631801584VariantsTable::DEFAULT_CONFIG_ID]);
$results = $this->variantsTableRepository->search($criteria, $salesChannelContext->getContext());
}
if ($results->count()) {
/** @var VariantsTableEntity $entity */
$entity = $results->first();
$entity->setHidePrices($this->shouldHidePrices($salesChannelContext));
if ($entity->isHidePrices()) {
$entity->setShowBuyAllButton(false);
}
return $entity;
}
return null;
}
public function shouldHidePrices(SalesChannelContext $salesChannelContext): bool
{
$config = $this->getPluginConfig($salesChannelContext);
if ($config->isHidePricesForGuests() && $salesChannelContext->getCustomer() === null) {
return true;
}
if ($config->getHidePricesForCustomerGroups() && $salesChannelContext->getCurrentCustomerGroup()) {
if (in_array($salesChannelContext->getCurrentCustomerGroup()->getId(), $config->getHidePricesForCustomerGroups())) {
return true;
}
}
return false;
}
}