custom/plugins/ApoShop/src/Subscribers/ProductSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ApoShop\Subscribers;
  4. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\Content\Media\MediaEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaCollection;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. class ProductSubscriber implements EventSubscriberInterface {
  15.     private $mediaRepository;
  16.     private $defaultMediaId null;
  17.     public function __construct(
  18.         EntityRepositoryInterface $mediaRepository
  19.     )
  20.     {
  21.         $this->mediaRepository $mediaRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array {
  24.         return [
  25.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
  26.         ];
  27.     }
  28.     public function onProductsLoaded(EntityLoadedEvent $event) {
  29.         //return;
  30.         foreach ($event->getEntities() as $entity) {
  31.             $defaultMediaId null;
  32.             $customFields $entity->getCustomFields();
  33.             if (empty($customFields['pzn'])) {
  34.                 continue;
  35.             }
  36.             try{
  37.                 $defaultMediaId $this->_getDefaultMediaId();
  38.             }
  39.             catch(\Exception $e){
  40.                 continue;
  41.             }
  42.             if( !$defaultMediaId ){
  43.                 continue;
  44.             }
  45.             $pzn $customFields['pzn'];
  46.             $url 'https://cdn8.apopixx.de/400/web_schraeg/243/' $pzn '.jpg';
  47.             $pathInfo pathinfo($url);
  48.             $media = new MediaEntity();
  49.             $media->setId($defaultMediaId);
  50.             $media->setUrl($url);
  51.             $media->setMimeType(sprintf('image/%s'$pathInfo['extension']));
  52.             $media->setFileExtension($pathInfo['extension']);
  53.             $media->setFileName($pathInfo['filename']);
  54.             $productMediaEntity = new ProductMediaEntity();
  55.             $productMediaEntity->setId(Uuid::randomHex());
  56.             $productMediaEntity->setMedia($media);
  57.             $productMediaEntity->setPosition(0);
  58.             $mediaCollection = new ProductMediaCollection([$productMediaEntity]);
  59.             $entity->setMedia($mediaCollection);
  60.             if ($entity->getCover() === null) {
  61.                 $entity->setCover($productMediaEntity);
  62.             } else {
  63.                 $entity->getCover()->setMedia($productMediaEntity->getMedia());
  64.             }
  65.         }
  66.     }
  67.     private function _getDefaultMediaId(){
  68.         if( $this->defaultMediaId === null ){
  69.             try{
  70.                 $criteria = new Criteria();
  71.                 $criteria->setLimit(1);
  72.                 $defaultMediaObj $this->mediaRepository->search(
  73.                     $criteria,
  74.                     \Shopware\Core\Framework\Context::createDefaultContext()
  75.                 )->first();
  76.                 if( $defaultMediaObj && $defaultMediaObj->getId() ){
  77.                     $this->defaultMediaId $defaultMediaObj->getId();
  78.             }
  79.                 else{
  80.                     $this->defaultMediaId false;
  81.                 }
  82.             }
  83.             catch(\Exception $e){
  84.             }
  85.         }
  86.         return $this->defaultMediaId;
  87.     }
  88. }