custom/plugins/SwagCustomizedProducts/src/Core/Content/Product/ProductWrittenSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CustomizedProducts\Core\Content\Product;
  8. use Shopware\Core\Content\Product\ProductDefinition;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Swag\CustomizedProducts\Migration\Migration1565933910TemplateProduct;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class ProductWrittenSubscriber implements EventSubscriberInterface
  19. {
  20.     public const SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_INHERITED_CUSTOM_FIELD 'swagCustomizedProductsTemplateInherited';
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $productRepository;
  25.     public function __construct(EntityRepositoryInterface $productRepository)
  26.     {
  27.         $this->productRepository $productRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             ProductEvents::PRODUCT_WRITTEN_EVENT => [
  33.                 ['onProductVariantWrittenInitializeCustomFields'],
  34.                 ['onTemplateAssignmentInheritVariants'],
  35.             ],
  36.         ];
  37.     }
  38.     public function onProductVariantWrittenInitializeCustomFields(EntityWrittenEvent $event): void
  39.     {
  40.         if ($event->getEntityName() !== ProductDefinition::ENTITY_NAME) {
  41.             return;
  42.         }
  43.         $updateData = [];
  44.         $fetchedParentIds = [];
  45.         foreach ($event->getPayloads() as $payload) {
  46.             if (!\array_key_exists('id'$payload)) {
  47.                 continue;
  48.             }
  49.             if (!\array_key_exists('parentId'$payload) || $payload['parentId'] === null) {
  50.                 continue;
  51.             }
  52.             // Exit if a template is explicitly written to a variant to differ from the parent
  53.             if (\array_key_exists('swagCustomizedProductsTemplateId'$payload)) {
  54.                 continue;
  55.             }
  56.             if (!$this->parentHasTemplateAssociated($payload['parentId'], $fetchedParentIds$event->getContext())) {
  57.                 continue;
  58.             }
  59.             $updateData[] = [
  60.                 'id' => $payload['id'],
  61.                 'customFields' => [
  62.                     self::SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_INHERITED_CUSTOM_FIELD => true,
  63.                 ],
  64.             ];
  65.         }
  66.         if ($updateData === []) {
  67.             return;
  68.         }
  69.         $this->productRepository->update($updateData$event->getContext());
  70.     }
  71.     public function onTemplateAssignmentInheritVariants(EntityWrittenEvent $event): void
  72.     {
  73.         if ($event->getEntityName() !== ProductDefinition::ENTITY_NAME) {
  74.             return;
  75.         }
  76.         foreach ($event->getPayloads() as $payload) {
  77.             if (!\array_key_exists('id'$payload)) {
  78.                 continue;
  79.             }
  80.             if (\array_key_exists('parentId'$payload)) {
  81.                 continue;
  82.             }
  83.             if (!\array_key_exists('swagCustomizedProductsTemplateId'$payload)) {
  84.                 continue;
  85.             }
  86.             $this->inheritExistingVariants($payload['id'], $event->getContext());
  87.         }
  88.     }
  89.     private function parentHasTemplateAssociated(string $parentId, array $fetchedParentIdsContext $context): bool
  90.     {
  91.         if (\array_key_exists($parentId$fetchedParentIds)) {
  92.             return $fetchedParentIds[$parentId];
  93.         }
  94.         $fetchedParentIds[$parentId] = false;
  95.         $criteria = new Criteria([$parentId]);
  96.         $criteria->setLimit(1);
  97.         $criteria->addAssociation(Migration1565933910TemplateProduct::PRODUCT_TEMPLATE_INHERITANCE_COLUMN);
  98.         $product $this->productRepository->search($criteria$context)->first();
  99.         if (!($product instanceof ProductEntity)) {
  100.             return false;
  101.         }
  102.         if (!$product->hasExtension(Migration1565933910TemplateProduct::PRODUCT_TEMPLATE_INHERITANCE_COLUMN)) {
  103.             return false;
  104.         }
  105.         $fetchedParentIds[$parentId] = true;
  106.         return true;
  107.     }
  108.     private function inheritExistingVariants(string $idContext $context): void
  109.     {
  110.         $criteria = new Criteria();
  111.         $criteria->addFilter(
  112.             new EqualsFilter('parentId'$id)
  113.         );
  114.         $variants $this->productRepository->search($criteria$context);
  115.         $updateData = [];
  116.         /** @var ProductEntity $variant */
  117.         foreach ($variants as $variant) {
  118.             $customFields $variant->getCustomFields();
  119.             if (\is_array($customFields)
  120.                 && \array_key_exists(self::SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_INHERITED_CUSTOM_FIELD$customFields)
  121.             ) {
  122.                 continue;
  123.             }
  124.             $updateData[] = [
  125.                 'id' => $variant->getId(),
  126.                 'customFields' => [
  127.                     self::SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_INHERITED_CUSTOM_FIELD => true,
  128.                 ],
  129.             ];
  130.         }
  131.         if ($updateData === []) {
  132.             return;
  133.         }
  134.         $this->productRepository->update($updateData$context);
  135.     }
  136. }