custom/plugins/TdsMerwareDiscounts/src/Core/Exchange/Import/Product/Subscriber/ProductsWrittenSubscriber.php line 80

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tds\MerwareDiscounts\Core\Exchange\Import\Product\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Tds\Merware\Core\Exchange\Import\Event\Product\ProductsWrittenEvent;
  11. use Tds\Merware\Core\Exchange\Import\Struct\Product\ProductStruct;
  12. use Tds\Merware\Service\Core\LanguageMappingService;
  13. /**
  14.  * Class ProductsWrittenSubscriber
  15.  *
  16.  * @package Tds\MerwareDiscounts\Core\Exchange\Import\Product\Subscriber
  17.  */
  18. class ProductsWrittenSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $systemConfigService;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $productRepository;
  28.     /**
  29.      * @var LanguageMappingService
  30.      */
  31.     private $languageMappingService;
  32.     /**
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             ProductsWrittenEvent::class => 'onProductsWritten'
  39.         ];
  40.     }
  41.     /**
  42.      * ProductsWrittenSubscriber constructor
  43.      *
  44.      * @param SystemConfigService $systemConfigService
  45.      * @param EntityRepositoryInterface $productRepository
  46.      */
  47.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $productRepositoryLanguageMappingService $languageMappingService)
  48.     {
  49.         $this->systemConfigService $systemConfigService;
  50.         $this->productRepository $productRepository;
  51.         $this->languageMappingService $languageMappingService;
  52.     }
  53.     /**
  54.      * Event function to catch saved products.
  55.      *
  56.      * @param ProductsWrittenEvent $event
  57.      *
  58.      * @throws InconsistentCriteriaIdsException
  59.      */
  60.     public function onProductsWritten(ProductsWrittenEvent $event)
  61.     {
  62.         $productStructs $event->getProductStructs();
  63.         if (!empty($productStructs)) {
  64.             $this->updateDiscountableProducts($event->getContext(), $productStructs);
  65.         }
  66.     }
  67.     /**
  68.      * Helper-function to update discountable products.
  69.      *
  70.      * @param Context $context
  71.      * @param ProductStruct[] $productStructs
  72.      */
  73.     private function updateDiscountableProducts($context$productStructs)
  74.     {
  75.         $requests $this->buildDiscountRequests($context$productStructs);
  76.         if (!empty($requests)) {
  77.             $this->productRepository->update($requests$context);
  78.         }
  79.     }
  80.     /**
  81.      * Helper-function to build valid DAL requests to update custom field data.
  82.      *
  83.      * @param Context $context
  84.      * @param ProductStruct[] $productStructs
  85.      *
  86.      * @return array
  87.      */
  88.     private function buildDiscountRequests($context$productStructs)
  89.     {
  90.         $requests = [];
  91.         foreach ($productStructs as $productStruct) {
  92.             /**
  93.              * required step to avoid external custom field overrides!
  94.              */
  95.             $customFields $this->getProductCustomFields($context$productStruct->getUuid());
  96.             $customFields['tds_merware_product_discount_activate'] = (bool) $productStruct->getDiscountable();
  97.             $customFieldTranslations = [];
  98.             $translations $productStruct->getTranslations();
  99.             if (!empty($translations)) {
  100.                 foreach ($translations as $translation) {
  101.                     $isoCode $this->languageMappingService->getIsoByLanguageCode($translation->getCode());
  102.                     if (!empty($isoCode)) {
  103.                         $customFieldTranslations[$isoCode] = [
  104.                             'customFields' => $customFields
  105.                         ];
  106.                     }
  107.                 }
  108.             }
  109.             $requests[] = [
  110.                 'id' => $productStruct->getUuid(),
  111.                 'customFields' => $customFields,
  112.                 'translations' => $customFieldTranslations
  113.             ];
  114.         }
  115.         return $requests;
  116.     }
  117.     /**
  118.      * Helper-function to get product custom fields.
  119.      *
  120.      * @param Context $context
  121.      * @param string $productId
  122.      *
  123.      * @return array|null
  124.      */
  125.     private function getProductCustomFields($context$productId)
  126.     {
  127.         $customFields = [];
  128.         $criteria = new Criteria([$productId]);
  129.         $result $this->productRepository->search($criteria$context)->first();
  130.         /**
  131.          * @var ProductEntity $result
  132.          */
  133.         if (!empty($result)) {
  134.             $customFields $result->getCustomFields();
  135.         }
  136.         return $customFields;
  137.     }
  138. }