custom/plugins/MadArticleBuyTime/src/Subscriber/ProductSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadArticleBuyTime\Subscriber;
  4. use MadArticleBuyTime\MadArticleBuyTime;
  5. use MadArticleBuyTime\Service\MadCustomFieldService;
  6. use MadArticleBuyTime\Struct\BuyTimeStruct;
  7. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  11. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  12. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ProductSubscriber  implements EventSubscriberInterface
  16. {
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  21.             ProductListingResultEvent::class => 'onProductListingResult',
  22.             ProductSearchResultEvent::class => 'onProductListingResult',
  23.         ];
  24.     }
  25.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  26.     {
  27.         $product $event->getPage()->getProduct();
  28.         $this->addBuyTimeExtensionToProduct($product);
  29.     }
  30.     public function onProductListingResult(ProductListingResultEvent $event): void
  31.     {
  32.         foreach ($event->getResult() as $product) {
  33.             $this->addBuyTimeExtensionToProduct($product);
  34.         }
  35.     }
  36.     private function addBuyTimeExtensionToProduct(SalesChannelProductEntity $product): void
  37.     {
  38.         if ($product->hasExtension(MadArticleBuyTime::BUYTIME_PRODUCT_EXTENSION_KEY)) {
  39.             return;
  40.         }
  41.         $customFields $product->getCustomFields();
  42.         if (isset($customFields[MadCustomFieldService::CUSTOM_FIELD_BUYTIME_START])
  43.             && isset($customFields[MadCustomFieldService::CUSTOM_FIELDS_BUYTIME_END])
  44.         ) {
  45.             $start \DateTimeImmutable::createFromFormat(
  46.                 \DateTimeInterface::ATOM,
  47.                 $customFields[MadCustomFieldService::CUSTOM_FIELD_BUYTIME_START]
  48.             );
  49.             $end \DateTimeImmutable::createFromFormat(
  50.                 \DateTimeInterface::ATOM,
  51.                 $customFields[MadCustomFieldService::CUSTOM_FIELDS_BUYTIME_END]
  52.             );
  53.             if ($start && $end) {
  54.                 $buyTime = new BuyTimeStruct($start$end);
  55.                 $product->addExtension(MadArticleBuyTime::BUYTIME_PRODUCT_EXTENSION_KEY$buyTime);
  56.             }
  57.         }
  58.     }
  59. }