custom/plugins/DreiwmBrandstetterPlugin/src/Subscriber/ProductSubscriber.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiwmBrandstetterPlugin\Subscriber;
  3. use DateTime;
  4. use Exception;
  5. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\Struct\ArrayEntity;
  10. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductSubscriber implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
  18.         ];
  19.     }
  20.     /**
  21.      * Wird aufgerufen, wenn ein Produkt geladen wird.
  22.      * Fügt die Erweiterung `seasonalAvailability` zum Produkt hinzu.
  23.      * @param EntityLoadedEvent $event
  24.      * @throws Exception
  25.      */
  26.     public function onProductLoaded(EntityLoadedEvent $event): void
  27.     {
  28.         foreach ($event->getEntities() as $productEntity) {
  29.             // hole die CustomFields aus dem Produkt
  30.             $productAvailableFrom $productEntity->getTranslated()['customFields']['product_available_from'] ?? null;
  31.             $productAvailableUntil $productEntity->getTranslated()['customFields']['product_available_until'] ?? null;
  32.             $seasonalAvailability $this->getProductValidTimeRangeListing($productAvailableFrom,
  33.                 $productAvailableUntil);
  34.             if ($seasonalAvailability) {
  35.                 // füge die Erweiterung zum Produkt hinzu
  36.                 $productEntity->addExtension('seasonalAvailability', new ArrayEntity($seasonalAvailability));
  37.             }
  38.         }
  39.     }
  40.     /**
  41.      * Gibt den Zeitraum zurück, in dem das Produkt gültig ist.
  42.      * Wenn es keine Einschränkungen gibt, wird null zurückgegeben.
  43.      * @param string|null $productValidFrom
  44.      * @param string|null $productValidUntil
  45.      * @return array|null
  46.      * @throws Exception
  47.      */
  48.     public function getProductValidTimeRangeListing(
  49.         null|string $productValidFrom,
  50.         null|string $productValidUntil
  51.     ): ?array {
  52.         $currentYear date('Y');
  53.         $currentDate = new DateTime();
  54.         $season "Jetzt für kurze Zeit";
  55.         $currentlyNotAvailable "Nur zur Saison erhältlich";
  56.         if ($productValidFrom) {
  57.             $validFromDate = new DateTime($productValidFrom);
  58.         } else {
  59.             $validFromDate null;
  60.         }
  61.         if ($productValidUntil) {
  62.             $validUntilDate = new DateTime($productValidUntil);
  63.         } else {
  64.             $validUntilDate null;
  65.         }
  66.         // Case 3: von DD.MM.YYYY bis DD.MM.YYYY
  67.         if ($validFromDate && $validUntilDate) {
  68.             if ($currentDate >= $validFromDate && $currentDate <= $validUntilDate) {
  69.                 return [
  70.                     'message' => $season,
  71.                     'season' => true
  72.                 ];
  73.             } elseif ($currentDate $validFromDate) {
  74.                 // Wenn `validFromDate` und `validUntilDate` im gleichen Jahr liegen
  75.                 if ($validFromDate->format('Y') == $validUntilDate->format('Y')) {
  76.                     // wenn beide Daten im aktuellen Jahr liegen
  77.                     if ($validFromDate->format('Y') == $currentYear) {
  78.                         return
  79.                             [
  80.                                 'message' => "ab " $validFromDate->format('d.m.') . " bis " $validUntilDate->format('d.m.Y'),
  81.                                 'season' => false
  82.                             ];
  83.                     } else {
  84.                         // wenn beide Daten nicht im aktuellen Jahr liegen
  85.                         return [
  86.                             'message' => "ab " $validFromDate->format('d.m.Y') . " bis " $validUntilDate->format('d.m.Y'),
  87.                             'season' => false
  88.                         ];
  89.                     }
  90.                 } else {
  91.                     // wenn `validFromDate` im aktuellen Jahr und `validUntilDate` im nächsten Jahr liegt
  92.                     if ($validFromDate->format('Y') == $currentYear && $validUntilDate->format('Y') > $currentYear) {
  93.                         return [
  94.                             'message' => "ab " $validFromDate->format('d.m.') . " bis " $validUntilDate->format('d.m.Y'),
  95.                             'season' => false
  96.                         ];
  97.                     } else {
  98.                         // wenn `validFromDate` und `validUntilDate` in unterschiedlichen Jahren liegen, beide in zukünftigen Jahren
  99.                         return [
  100.                             'message' => "ab " $validFromDate->format('d.m.Y') . " bis " $validUntilDate->format('d.m.Y'),
  101.                             'season' => false
  102.                         ];
  103.                     }
  104.                 }
  105.             } else {
  106.                 return [
  107.                     'message' => $currentlyNotAvailable,
  108.                     'season' => false
  109.                 ];
  110.             }
  111.         }
  112.         // Case 4: gesperrt (frei von DD.MM.YYYY bis DD.MM.YYYY)
  113.         if ($validFromDate && $validUntilDate && $currentDate <= $validUntilDate) {
  114.             $fromYear $validFromDate->format('Y') != $currentYear $validFromDate->format('.Y') : '';
  115.             $untilYear $validUntilDate->format('Y') != $currentYear $validUntilDate->format('.Y') : '';
  116.             return [
  117.                 'message' => $validFromDate->format('d.m.') . $fromYear " bis " $validUntilDate->format('d.m.') . $untilYear,
  118.                 'season' => false
  119.             ];
  120.         }
  121.         // Case 5: ab DD.MM.YYYY
  122.         if ($validFromDate && !$validUntilDate) {
  123.             if ($currentDate >= $validFromDate) {
  124.                 return null;
  125.             } else {
  126.                 return [
  127.                     'message' => "ab " $validFromDate->format('d.m.Y'),
  128.                     'season' => false
  129.                 ];
  130.             }
  131.         }
  132.         // Case 6: bis DD.MM.YYYY
  133.         if (!$validFromDate && $validUntilDate) {
  134.             if ($currentDate <= $validUntilDate) {
  135.                 return null;
  136.             } else {
  137.                 return [
  138.                     'message' => $currentlyNotAvailable,
  139.                     'season' => true
  140.                 ];
  141.             }
  142.         }
  143.         // Case 7: gesperrt (frei ab DD.MM.YYYY)
  144.         if ($validFromDate && $currentDate >= $validFromDate) {
  145.             return null;
  146.         } else {
  147.             if ($validFromDate) {
  148.                 return [
  149.                     'message' => "ab " $validFromDate->format('d.m.Y'),
  150.                     'season' => false
  151.                 ];
  152.             }
  153.         }
  154.         return null;
  155.     }
  156. }