<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2022
*
***/
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @author Daniel Walter <daniel.walter@dkd.de>
*/
class ProductLoadedSerialNumberSubscriber implements EventSubscriberInterface
{
private ?string $serialNumber = null;
/**
* @return string|null
*/
public function getSerialNumber(): ?string {
return $this->serialNumber;
}
/**
* @param string|null $serialNumber
*/
public function setSerialNumber( ?string $serialNumber ): void {
$this->serialNumber = $serialNumber;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
];
}
public function onProductsLoaded(EntityLoadedEvent $event)
{
foreach($event->getEntities() as $entity) {
if($this->serialNumber) {
$entity->addExtension("customProduct", new ArrayStruct(["serialnumber"=> $this->getSerialNumber()]));
}
}
}
}