custom/plugins/dupp_sync4_api/src/Subscriber/ProductLoadedSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dupp\Sync4Api\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEvents;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  5. use Shopware\Core\Framework\Struct\ArrayEntity;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ProductLoadedSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var string
  12.      */
  13.     private $pluginName 'Sync4ApiExport';
  14.     /**
  15.      * @var string
  16.      */
  17.     private $customFieldName 'sync4_api_visual_order';
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ProductPageLoadedEvent::class => 'onProductLoaded'
  22.         ];
  23.     }
  24.     public function onProductLoaded(ProductPageLoadedEvent $event): void
  25.     {
  26.         $config = [];
  27.         $page $event->getPage();
  28.         $product $page->getProduct();
  29.         $customFields $product->getCustomFields();
  30.         if (!$customFields) {
  31.             return;
  32.         }
  33.         // loop through each product's custom fields
  34.         foreach($customFields as $name => $value) {
  35.             if ($name !== $this->customFieldName || empty($value)) {
  36.                 continue;
  37.             }
  38.             $config['visualOrder'] = json_decode($value);
  39.         }
  40.         $event->getContext()->addExtension($this->pluginName, new ArrayEntity($config));
  41.     }
  42. }