custom/plugins/ChespackTheme/src/Subscriber/ProductSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ChespackTheme\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Shopware\Core\Content\Product\ProductEvents;
  10. class ProductSubscriber implements EventSubscriberInterface
  11. {
  12.     private EntityRepositoryInterface $categoryRepository;
  13.     private EntityRepositoryInterface $mediaRepository;
  14.     public function __construct(
  15.         EntityRepositoryInterface $categoryRepository,
  16.         EntityRepositoryInterface $mediaRepository
  17.     ) {
  18.         $this->categoryRepository $categoryRepository;
  19.         $this->mediaRepository $mediaRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
  25.         ];
  26.     }
  27.     public function onProductLoaded(EntityLoadedEvent $event): void
  28.     {
  29.         // loop through all loaded product
  30.         /** @var ProductEntity $productEntity */
  31.         foreach ($event->getEntities() as $productEntity) {
  32.             $customFields $productEntity->getCustomFields();
  33.             // loop through each product's custom fields
  34.             if($customFields) {
  35.                 foreach ($customFields as $name => $value) {
  36.                     if ($name !== 'related_resource_categories' || empty($value)) {
  37.                         continue;
  38.                     }
  39.                     $context $event->getContext();
  40.                     $relatedCategories = [];
  41.                     foreach ($value as $key => $categoryID) {
  42.                         // search the entity via the repository here
  43.                         /** @var CategoryEntity $categoryEntity */
  44.                     
  45.                         /*value may come in as a String, Array or an Object type */
  46.                         $categoryid $categoryID;
  47.                         $objectType gettype($categoryID);
  48.                         if ($objectType=="array"){
  49.                             $categoryid $categoryID['id'];
  50.                         } elseif ($objectType == 'object'){
  51.                             $categoryid $categoryID->getId();
  52.                         }
  53.                     
  54.                         $category $this->categoryRepository
  55.                             ->search(new Criteria([$categoryid]), $context)->first();
  56.                         if ($category) {
  57.                             //Now get the media associated with the category
  58.                             $media $this->mediaRepository
  59.                                ->search(new Criteria([$category->getMediaId()]), $context)->first();
  60.                             $category->setMedia($media);
  61.                             $relatedCategories[] = $category;
  62.                         }
  63.                     }
  64.                     $customFields[$name] = $relatedCategories;
  65.                 }
  66.                 $productEntity->setCustomFields($customFields);
  67.             }
  68.         }
  69.     }
  70. }