custom/static-plugins/SamsonCustomer/src/Subscriber/ProductLoadedSerialNumberSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2022
  11.  *
  12.  ***/
  13. use Shopware\Core\Content\Product\ProductEvents;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  15. use Shopware\Core\Framework\Struct\ArrayStruct;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18.  * @author Daniel Walter <daniel.walter@dkd.de>
  19.  */
  20. class ProductLoadedSerialNumberSubscriber implements EventSubscriberInterface
  21. {
  22.     private ?string $serialNumber null;
  23.     /**
  24.      * @return string|null
  25.      */
  26.     public function getSerialNumber(): ?string {
  27.         return $this->serialNumber;
  28.     }
  29.     /**
  30.      * @param string|null $serialNumber
  31.      */
  32.     public function setSerialNumber( ?string $serialNumber ): void {
  33.         $this->serialNumber $serialNumber;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
  39.         ];
  40.     }
  41.     public function onProductsLoaded(EntityLoadedEvent $event)
  42.     {
  43.         foreach($event->getEntities() as $entity) {
  44.             if($this->serialNumber) {
  45.                 $entity->addExtension("customProduct", new ArrayStruct(["serialnumber"=> $this->getSerialNumber()]));
  46.             }
  47.         }
  48.     }
  49. }