custom/static-plugins/HbHAlphaplanCustoms/src/Subscriber/ProductSubscriber.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HbHAlphaplanCustoms\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Core\Content\Product\ProductEvents;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class ProductSubscriber implements EventSubscriberInterface
  15. {
  16.     public EntityRepository $cvsKProductRepository;
  17.     public function __construct(EntityRepository $cvsKProductRepository)
  18.     {
  19.         $this->cvsKProductRepository $cvsKProductRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  25.             KernelEvents::REQUEST => 'onKernelRequest'
  26.         ];
  27.     }
  28.     public function onKernelRequest(RequestEvent $event)
  29.     {
  30.         $request $event->getRequest();
  31.         $controller $request->attributes->get('_controller');
  32.         $params explode('::',$controller);
  33.         if($request->getMethod() === "POST" && stripos($params[0], 'SyncController') !== false){
  34.             $content json_decode($request->getContent(), true);
  35.             if(
  36.                 isset($content[0]['payload'][0]['manufacturerId']) &&
  37.                 $content[0]['payload'][0]['manufacturerId'] === "0" &&
  38.                 $content[0]['entity'] === 'product' &&
  39.                 $content[0]['action'] === 'upsert' &&
  40.                 $content[0]['key'] === 'write'
  41.             ){
  42.                 $content[0]['payload'][0]['manufacturerId'] = null;
  43.                 $request->initialize(
  44.                     $request->query->all(),
  45.                     $request->request->all(),
  46.                     $request->attributes->all(),
  47.                     $request->cookies->all(),
  48.                     $request->files->all(),
  49.                     $request->server->all(),
  50.                     json_encode($content)
  51.                 );
  52.             }
  53.         }
  54.     }
  55.     public function onProductsLoaded(EntityLoadedEvent $event): void
  56.     {
  57.         /** @var ProductEntity $productEntity */
  58.         foreach ($event->getEntities() as $productEntity) {
  59.             $data $this->getData($productEntity->getId(), $event->getContext());
  60.             if($data !== null){
  61.                 $productEntity->addExtension('cvs_product_extension', new ArrayEntity(['K_ArzneimittelPflichtText' => $data->getKArzneimittelPflichtText()]));
  62.             }
  63.         }
  64.     }
  65.     private function getData($productIdContext $context){
  66.         $criteria = new Criteria();
  67.         $criteria->addFilter(new EqualsFilter("productId"$productId));
  68.         $result $this->cvsKProductRepository->search($criteria$context);
  69.         return $result->getEntities()->first();
  70.     }
  71. }