vendor/store.shopware.com/dreiscseopro/src/Subscriber/ProductEvents/ProductLoadedSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Subscriber\ProductEvents;
  3. use DreiscSeoPro\Core\Seo\LiveTemplate\LiveTemplateConverter;
  4. use PHPUnit\Exception;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Category\CategoryEvents;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductLoadedSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var LiveTemplateConverter
  16.      */
  17.     private $liveTemplateConverter;
  18.     /**
  19.      * @param LiveTemplateConverter $liveTemplateTranslator
  20.      */
  21.     public function __construct(LiveTemplateConverter $liveTemplateTranslator)
  22.     {
  23.         $this->liveTemplateConverter $liveTemplateTranslator;
  24.     }
  25.     /**
  26.      * @return array|void
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
  32.         ];
  33.     }
  34.     /**
  35.      * @param EntityLoadedEvent $entityLoadedEvent
  36.      */
  37.     public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent): void
  38.     {
  39.         /** @var ProductEntity $productEntity */
  40.         $productEntity current($entityLoadedEvent->getEntities());
  41.         /** Abort, if empty */
  42.         if (false === $productEntity) {
  43.             return;
  44.         }
  45.         $this->provideCustomFields($productEntity);
  46.         /** At this point the seo live template will be converted */
  47.         $this->liveTemplateConverter->translateProductEntity($productEntity$entityLoadedEvent->getContext());
  48.     }
  49.     private function provideCustomFields(ProductEntity $productEntity)
  50.     {
  51.         $customFields $productEntity->getCustomFields();
  52.         if (null === $customFields) {
  53.             $customFields = [];
  54.         }
  55.         $customFieldsKeys = [
  56.             'dreisc_seo_rich_snippet_item_condition' => null,
  57.             'dreisc_seo_rich_snippet_availability' => null,
  58.             'dreisc_seo_rich_snippet_custom_sku' => null,
  59.             'dreisc_seo_rich_snippet_custom_mpn' => null,
  60.             'dreisc_seo_rich_snippet_price_valid_until_date' => null,
  61.             'dreisc_seo_facebook_title' => null,
  62.             'dreisc_seo_facebook_description' => null,
  63.             'dreisc_seo_facebook_image' => null,
  64.             'dreisc_seo_twitter_description' => null,
  65.             'dreisc_seo_twitter_image' => null,
  66.             'dreisc_seo_robots_tag' => null,
  67.             'dreisc_seo_canonical_link_reference' => [],
  68.             'dreisc_seo_canonical_link_type' => [],
  69.         ];
  70.         foreach($customFieldsKeys as $customFieldsKey => $type) {
  71.             if(empty($customFields[$customFieldsKey])) {
  72.                 $customFields[$customFieldsKey] = $type;
  73.             }
  74.         }
  75.         $productEntity->setCustomFields($customFields);
  76.     }
  77. }