custom/plugins/AlbisLease/src/Subscriber/ProductPageSubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Albis\AlbisLease\Subscriber;
  3. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  5. use Shopware\Core\System\CustomField\CustomFieldCollection;
  6. use Shopware\Core\System\CustomField\CustomFieldEntity;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Shopware\Core\Content\Product\ProductEvents;
  10. class ProductPageSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SystemConfigService
  14.      */
  15.     private $systemConfigService;
  16.     public function __construct(SystemConfigService $systemConfigService)
  17.     {
  18.         $this->systemConfigService $systemConfigService;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  24.         ];
  25.     }
  26.     public function onProductsLoaded(EntityLoadedEvent $event): void
  27.     {
  28.         $context $event->getContext();
  29.         $entities $event->getEntities();
  30.         if(!method_exists($context->getSource(), 'getSalesChannelId')) {
  31.             return;
  32.         }
  33.         $salesChannelId $context->getSource()->getSalesChannelId();
  34.         $pluginConfig $this->systemConfigService->get('AlbisLease.config'$salesChannelId);
  35.         
  36.         if(!$pluginConfig['detailPageDisplay']) {
  37.             return;
  38.         }
  39.         
  40.         /** @var SalesChannelProductEntity $entity */
  41.         foreach($entities as $entity) {
  42.             
  43.             $price $entity->getPrice()->first()->getNet();
  44.             
  45.             if($price >= 500) {
  46.                 $customFields array_merge($entity->getCustomFields(), [
  47.                     'albisLeasingAvailable' => true
  48.                 ]);
  49.                 $entity->setCustomFields($customFields);
  50.             }
  51.         }
  52.     }
  53. }