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

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