custom/plugins/ProcWegmannTheme/src/Subscriber/ProductSubscriber.php line 105

Open in your IDE?
  1. <?php
  2. namespace Proclane\WegmannTheme\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Core\System\CustomField\CustomFieldEntity;
  10. use Shopware\Core\System\Language\LanguageEntity;
  11. use Shopware\Core\System\Locale\LocaleEntity;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Monolog\Handler\StreamHandler;
  17. use Monolog\Logger;
  18. use Psr\Log\LoggerInterface;
  19. class ProductSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var LoggerInterface
  23.      */
  24.     private $loggerInterface;
  25.     private $technicalNameOfCustomFieldSet 'custom_product_data';
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private EntityRepositoryInterface $languageRepository;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private EntityRepositoryInterface $customFieldRepository;
  34.     /**
  35.      * @var SystemConfigService
  36.      */
  37.     private SystemConfigService $systemConfigService;
  38.     /**
  39.      * Blacklist of technical properties which should not be displayed in storefront
  40.      *
  41.      * @var array|string[]
  42.      */
  43.     private array $technicalPropertiesBlacklist = [
  44. //    'custom_product_data_Verpackungstyp1',
  45. //    'custom_product_data_GlobalerMaterialstatus',
  46. //    'custom_product_data_SAPProdukthirarchie',
  47. //    'custom_product_data_Rundungsmenge1',
  48. //    'custom_product_data_Vertriebseinheit1',
  49.     ];
  50.     /**
  51.      * @var array
  52.      */
  53.     private array $customFields;
  54.     /**
  55.      * @var string
  56.      */
  57.     private string $localeCode;
  58.     /**
  59.      * @param EntityRepositoryInterface $languageRepository
  60.      * @param EntityRepositoryInterface $customFieldRepository
  61.      * @param SystemConfigService $systemConfigService
  62.      */
  63.     public function __construct(
  64.         EntityRepositoryInterface $languageRepository,
  65.         EntityRepositoryInterface $customFieldRepository,
  66.         SystemConfigService       $systemConfigService)
  67.     {
  68.         $this->languageRepository $languageRepository;
  69.         $this->customFieldRepository $customFieldRepository;
  70.         $this->systemConfigService $systemConfigService;
  71.         $this->loggerInterface = new Logger('ProcErrorLog');
  72.         try {
  73.             $this->loggerInterface->pushHandler(new StreamHandler(__DIR__ '/../../../../../var/log/proc_error_log' date("Y-m-d") . '.log'));
  74.         } catch (Exception $e) {
  75.         }
  76.     }
  77.     /**
  78.      * @return string[]
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [
  83.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  84.         ];
  85.     }
  86.     /**
  87.      * @param ProductPageLoadedEvent $event
  88.      */
  89.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
  90.     {
  91.         $this->generateTechnicalPropertiesBlackList();
  92.         $product $event->getPage()->getProduct();
  93.         $this->customFields $product->getCustomFields();
  94.         $arr_customFieldNames = [];
  95.         foreach ($this->customFields as $cfName => $cfValue) {
  96.             if (str_contains($cfName$this->technicalNameOfCustomFieldSet)
  97.                 && !in_array($cfName$this->technicalPropertiesBlacklist)) {
  98.                 $arr_customFieldNames[] = $cfName;
  99.             } else {
  100.                 unset($this->customFields[$cfName]);
  101.             }
  102.         }
  103.         $criteria = new Criteria();
  104.         $criteria->addFilter(new EqualsAnyFilter('name'$arr_customFieldNames));
  105.         /** @var CustomFieldEntity $customFieldEntities */
  106.         $customFieldEntities $this->customFieldRepository->search($criteria$event->getContext())->getElements();
  107.         $localeCodeDefault  $this->getLocaleCode($event->getContext()->getLanguageId(), $event->getContext());
  108.         $arr_finalCustomFieldsForStorefront = [];
  109.         /** @var CustomFieldEntity $customField */
  110.         foreach ($customFieldEntities as $customFieldEntity) {
  111.             $value '';
  112.             $localeCode null;
  113.             if ($customFieldEntity->getType() == 'select') {
  114.                 $value $this->getSelectFieldValues($customFieldEntity$localeCodeDefault);
  115.             } else if (($customFieldEntity->getType() == 'checkbox') && ($this->customFields[$customFieldEntity->getName()] == false)){
  116.                 continue;
  117.             } else {
  118.                 $value $this->customFields[$customFieldEntity->getName()];
  119.             }
  120.             if (!isset($customFieldEntity->getConfig()['label'][$localeCodeDefault])) {
  121.                 $localeCode "en-GB";
  122.                 if (!isset($customFieldEntity->getConfig()['label'][$localeCode])) {
  123.                     $localeCode "de-DE";
  124.                 }
  125.             }
  126.             if(!empty($value)) {
  127.                 $arr_finalCustomFieldsForStorefront[] = [
  128.                     'name' => $customFieldEntity->getConfig()['label'][$localeCode ?? $localeCodeDefault],
  129.                     'value' => $value
  130.                 ];
  131.             }
  132.         }
  133.         $extensionStruct = new ArrayStruct(['fields' => $arr_finalCustomFieldsForStorefront]);
  134.         $event->getPage()->addExtension("productTechnicalInformation"$extensionStruct);
  135.     }
  136.     private function getSelectFieldValues(CustomFieldEntity $customFieldEntity$localeCode)
  137.     {
  138.         $value '';
  139.         $customFieldConfig $customFieldEntity->getConfig();
  140.         $customFieldName $customFieldEntity->getName();
  141.         $customFieldValue $this->customFields[$customFieldName];
  142.         if (array_key_exists('options'$customFieldConfig) && !empty($customFieldConfig['options'])) {
  143.             for ($i 0$i count($customFieldConfig['options']); $i++) {
  144.                 if ($customFieldEntity->getConfig()['componentName'] == 'sw-multi-select' && is_array($this->customFields[$customFieldName])) {
  145.                     for ($j 0$j count($this->customFields[$customFieldName]); $j++) {
  146.                         if ($customFieldConfig['options'][$i]['value'] == $customFieldValue[$j]) {
  147.                             if (!empty($customFieldConfig['options'][$i]['label'][$localeCode])) {
  148.                                 $value .= $customFieldConfig['options'][$i]['label'][$localeCode] . ', ';
  149.                             } else {
  150.                                 $value .= $customFieldConfig['options'][$i]['label']['de-DE'] . ', ';
  151.                             }
  152.                         }
  153.                     }
  154.                 } elseif ($customFieldEntity->getConfig()['componentName'] == 'sw-multi-select' && !is_array($this->customFields[$customFieldName])) {
  155.                     $this->loggerInterface->info('Fehler im customField: ' print_r($this->customFields[$customFieldName], true));
  156.                 } else {
  157.                     if ($customFieldConfig['options'][$i]['value'] == $customFieldValue) {
  158.                         if (!empty($customFieldConfig['options'][$i]['label'][$localeCode])) {
  159.                             $value $customFieldConfig['options'][$i]['label'][$localeCode] . ', ';
  160.                         } else {
  161.                             $value .= $customFieldConfig['options'][$i]['label']['de-DE'] . ', ';
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.         $value substr($value0, -2);
  168.         
  169.         return $value;
  170.     }
  171.     /**
  172.      * Generate the blacklist of properties that should not be shown in the storefront
  173.      *
  174.      * @return void
  175.      */
  176.     private function generateTechnicalPropertiesBlackList()
  177.     {
  178.         $pluginConfig $this->systemConfigService->get('ProclaneWegmannTheme.config.customFieldsToHideInSpecifications');
  179.         $criteria = new Criteria();
  180.         $criteria->addFilter(new EqualsAnyFilter('id'$pluginConfig));
  181.         $customFieldEntities $this->customFieldRepository->search($criteriaContext::createDefaultContext())->getElements();
  182.         /** @var CustomFieldEntity $entity */
  183.         foreach ($customFieldEntities as $entity) {
  184.             $this->technicalPropertiesBlacklist[] = $entity->getName();
  185.         }
  186.     }
  187.     /**
  188.      * Get locale code from database
  189.      *
  190.      * @param string $languageId
  191.      * @param Context $context
  192.      * @return string
  193.      */
  194.     private function getLocaleCode(string $languageIdContext $context): string
  195.     {
  196.         $criteria = new Criteria();
  197.         $criteria->addFilter(new EqualsFilter('id'$languageId));
  198.         $criteria->addAssociation('locale');
  199.         /** @var LanguageEntity $languageEntity */
  200.         $languageEntity $this->languageRepository->search($criteria$context)->first();
  201.         return $languageEntity->getLocale()->getCode();
  202.     }
  203. }