custom/plugins/MojeBambinoExtensions/src/Subscriber/ProductPageSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace MojeBambino\Extensions\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Doctrine\DBAL\Connection;
  6. use MojeBambino\Extensions\Struct\Dopiski;
  7. class ProductPageSubscriber implements EventSubscriberInterface {
  8.     
  9.     private Connection $connection;
  10.     
  11.     public function __construct(Connection $connection){
  12.         $this->connection $connection;
  13.     }
  14.     
  15.     public static function getSubscribedEvents(): array{
  16.         return [ProductPageLoadedEvent::class => 'onProductPageLoaded'];
  17.     }
  18.     
  19.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void{
  20.         $page $event->getPage();
  21.         $product $page->getProduct();
  22.         //echo "<pre>"; print_r($product); echo "</pre>";
  23.         $kod $product->getProductNumber();
  24.         $dopiski = new Dopiski();
  25.         
  26.         $q $this->connection->createQueryBuilder();
  27.         $q->select(['d.*'])->from('dopiski_kody''dk');
  28.         $q->join('dk''dopiski''d''dk.Id=d.Id');
  29.         $q->where('dk.kod= :kod');
  30.         $q->setParameter('kod'$kod);
  31.         $st $q->execute();
  32.         $dop $st->fetchAll(\PDO::FETCH_ASSOC);
  33.         $dopiski->setDopiski($dop);
  34.         
  35.         $q6 $this->connection->createQueryBuilder();
  36.         $q6->select(['*'])->from('wzrost_ceny')->where('kod= :kod')->setMaxResults(1);
  37.         $q6->setParameter('kod'$kod);
  38.         $st6 $q6->execute();
  39.         $d6 $st6->fetchAll(\PDO::FETCH_ASSOC);
  40.         if(count($d6)>0){
  41.             foreach($d6 as $dop){
  42.                 $dopiski->setWzrostCeny($dop['data']);
  43.             }
  44.         }
  45.         else{
  46.             $dopiski->setWzrostCeny(count($d6));    
  47.         }
  48.         
  49.         $q7 $this->connection->createQueryBuilder();
  50.         $q7->select(['*'])->from('cena_do_daty')->where('kod= :kod')->andWhere('data>="'.date("Y-m-d").'"')->setMaxResults(1);
  51.         $q7->setParameter('kod'$kod);
  52.         //echo $q7->getSQL(); 
  53.         $st7 $q7->execute();
  54.         $d7 $st7->fetchAll(\PDO::FETCH_ASSOC);
  55.         if(count($d7)>0){
  56.             foreach($d7 as $dop){
  57.                 $dopiski->setCenaDoDaty($dop['data']);
  58.             }
  59.         }
  60.         else{
  61.             $dopiski->setCenaDoDaty(count($d7));
  62.         }
  63.         
  64.         $q8 $this->connection->createQueryBuilder();
  65.         $q8->select(['*'])->from('omnibus_cena')->where('kod= :kod')->setMaxResults(1);
  66.         $q8->setParameter('kod'$kod);
  67.         $st8 $q8->execute();
  68.         $d8 $st8->fetchAll(\PDO::FETCH_ASSOC);
  69.         if(count($d8)>0){
  70.             foreach($d8 as $dop){
  71.                 $dopiski->setOmnibusCena(str_replace("."","$dop['cena'])." zł");
  72.             }
  73.         }
  74.         else{
  75.             $dopiski->setOmnibusCena(count($d8));
  76.         }
  77.         
  78.         $q9 $this->connection->createQueryBuilder();
  79.         $q9->select(['*'])->from('omnibus_info')->setMaxResults(1);
  80.         $st9 $q9->execute();
  81.         $d9 $st9->fetchAll(\PDO::FETCH_ASSOC);
  82.         if(count($d9)>0){
  83.             foreach($d9 as $dop){
  84.                 $dopiski->setOmnibusInfo($dop['info']);
  85.             }
  86.         }
  87.         else{
  88.             $dopiski->setOmnibusInfo(count($d9));
  89.         }
  90.         
  91.         $product->addExtension('dopiski'$dopiski);
  92.     }
  93. }