custom/plugins/MillProductEan/src/Subscriber/PageSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Mill\ProductEan\Subscriber;
  3. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. /**
  6.  * Class PageSubscriber
  7.  *
  8.  * @package Mill\ProductEan\Subscriber
  9.  */
  10. class PageSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * {@inheritDoc}
  14.      */
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  19.         ];
  20.     }
  21.     /**
  22.      * Event-function to add the ean item prop
  23.      *
  24.      * @param ProductPageLoadedEvent $event
  25.      */
  26.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  27.     {
  28.         $page $event->getPage();
  29.         $product $page->getProduct();
  30.         $ean = (string) $product->getEan();
  31.         $page->assign(
  32.             [
  33.                 'millProductEan' => [
  34.                     'itemProp' => $this->getEanItemProp($ean)
  35.                 ]
  36.             ]
  37.         );
  38.     }
  39.     /**
  40.      * Helper-function to get the correct item prop
  41.      *
  42.      * @param string $ean
  43.      *
  44.      * @return string
  45.      */
  46.     private function getEanItemProp($ean): string
  47.     {
  48.         $length strlen($ean);
  49.         switch ($length) {
  50.             case 8:
  51.                 $itemprop 'gtin8';
  52.                 break;
  53.             case 12:
  54.                 $itemprop 'gtin12';
  55.                 break;
  56.             case 14:
  57.                 $itemprop 'gtin14';
  58.                 break;
  59.             default:
  60.                 $itemprop 'gtin13';
  61.                 break;
  62.         }
  63.         return $itemprop;
  64.     }
  65. }