vendor/store.shopware.com/trustedshops/src/Subscriber/FrontendSubscriber.php line 74

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TrustedShops\Subscriber;
  3. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Struct\ArrayEntity;
  11. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  12. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageCriteriaEvent;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Event\StorefrontRenderEvent;
  15. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  16. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class FrontendSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $systemConfigService;
  24.     private $productRepository;
  25.     private $trustedshopProductRatingRepository;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $shopReviewsApiUrl 'https://api.trustedshops.com/rest/public/v2/shops/{{tsId}}/reviews';
  30.     /**
  31.      * @var int
  32.      */
  33.     private $shopReviewsCheckInterval 3600;
  34.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $productRepositoryEntityRepositoryInterface $trustedshopProductRatingRepository)
  35.     {
  36.         $this->systemConfigService $systemConfigService;
  37.         $this->productRepository $productRepository;
  38.         $this->trustedshopProductRatingRepository $trustedshopProductRatingRepository;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         $listeners = [
  43.             StorefrontRenderEvent::class => 'onStoreFrontRender',
  44.             MinimalQuickViewPageCriteriaEvent::class => 'onMinimalQuickViewPageCriteria',
  45.             ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  46.             ProductPageLoadedEvent::class=>'onProductPageLoaded',
  47.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  48.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
  49.         ];
  50.         if( class_exists'Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent' ) ) {
  51.             $listeners\Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent::class ] = 'onLineItemAdded';
  52.         } else {
  53.             $listeners\Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent::class ] = 'onLineItemAdded';
  54.         }
  55.         return $listeners;
  56.     }
  57.     public function onStoreFrontRender(StorefrontRenderEvent $event): void
  58.     {
  59.         $config $this->systemConfigService->get'TrustedShops.config'$event->getSalesChannelContext()->getSalesChannel()->getId() );
  60.         $event->setParameter'trustedshops'$config );
  61.     }
  62.     public function onMinimalQuickViewPageCriteria(MinimalQuickViewPageCriteriaEvent $event): void
  63.     {
  64.         $event->getCriteria()->addAssociation('extensions.trustedshopsRatings');
  65.     }
  66.     public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
  67.     {
  68.         $event->getCriteria()->addAssociation('extensions.trustedshopsRatings');
  69.     }
  70.     public function onProductPageLoaded(ProductPageLoadedEvent $event){
  71.         $product $event->getPage()->getProduct();
  72.         if( ( $parentId $product->getParentId()) ) {
  73.             $parent $this->productRepository->search(new Criteria([$parentId]), $event->getContext())->first();
  74.             $sku = new ArrayEntity();
  75.             $sku->set('sku'$parent->getProductNumber());
  76.             $product->addExtension("parentProduct"$sku);
  77.         }
  78.     }
  79.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  80.     {
  81.         $event->getCriteria()->addAssociation('extensions.trustedshopsRatings');
  82.     }
  83.     public function onLineItemAdded($event): void
  84.     {
  85.         $lineItem $event->getLineItem();
  86.         if( $lineItem->getType() === 'product' ) {
  87.             $context Context::createDefaultContext();
  88.             /** @var ProductEntity $product */
  89.             $product $this->productRepository->search((new Criteria([$lineItem->getReferencedId()])),$context)->first();
  90.             if( ( $parentId $product->getParentId()) ) {
  91.                 /** @var ProductEntity $parent */
  92.                 $parent $this->productRepository->search((new Criteria([$parentId]))->addAssociation('cover'),$context)->first();
  93.                 $lineItem->setPayloadValue('parent',[
  94.                     'productId' => $parent->getId(),
  95.                     'productNumber' => $parent->getProductNumber(),
  96.                     'label' => $parent->getTranslation('name'),
  97.                     'coverUrl' => ( $parent->getCover() ? $parent->getCover()->getMedia()->getUrl() : null )
  98.                 ]);
  99.             }
  100.         }
  101.     }
  102.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event): void {
  103.         $page $event->getPage();
  104.         $page->assign(['tsDeliveryTime' => $this->getTrustedShopsDeliveryTime$page->getOrder() ) ] );
  105.     }
  106.     protected function getTrustedShopsDeliveryTimeOrderEntity $order )
  107.     {
  108.         $customAvailableDeliverTime = ( $this->systemConfigService->get'TrustedShops.config.tsAvailableProductDeliveryTime' ) === 'custom' );
  109.         $customUnavailableDeliverTime = ( $this->systemConfigService->get'TrustedShops.config.tsNotAvailableProductDeliveryTime' ) === 'custom' );
  110.         $availableDeliveryTimeDays $this->systemConfigService->get'TrustedShops.config.tsAvailableProductDeliveryTimeDays' );
  111.         $unavailableDeliveryTimeDays $this->systemConfigService->get'TrustedShops.config.tsNotAvailableProductDeliveryTimeDays' );
  112.         if( $this->orderContainsUnavailableProducts$order ) ) {
  113.             if( $customUnavailableDeliverTime && $unavailableDeliveryTimeDays ) {
  114.                 $deliveryTimeDays $unavailableDeliveryTimeDays;
  115.             } else {
  116.                 return $order->getDeliveries()->first()->getShippingDateLatest();
  117.             }
  118.         } else {
  119.             if( $customAvailableDeliverTime && $availableDeliveryTimeDays ) {
  120.                 $deliveryTimeDays $availableDeliveryTimeDays;
  121.             } else {
  122.                 return $order->getDeliveries()->first()->getShippingDateLatest();
  123.             }
  124.         }
  125.         switch (date('N')) {
  126.             case 5//freitag
  127.                 $deliveryTimeDays += 2;
  128.                 break;
  129.             case 6//samstag
  130.                 $deliveryTimeDays += 1;
  131.                 break;
  132.         }
  133.         if( date'N'strtotime'+' $deliveryTimeDays 'days' ) ) === ) {
  134.             $deliveryTimeDays++;
  135.         }
  136.         return (new \DateTime())->add(new \DateInterval('P' $deliveryTimeDays 'D'));
  137.     }
  138.     protected function orderContainsUnavailableProductsOrderEntity $order ): bool
  139.     {
  140.         $context Context::createDefaultContext();
  141.         /** @var OrderLineItemEntity $lineItem */
  142.         foreach( $order->getLineItems() as $lineItem ) {
  143.             if( $lineItem->getType() === 'product' ) {
  144.                 /** @var ProductEntity $product */
  145.                 $product $this->productRepository->search((new Criteria([$lineItem->getProductId()])),$context)->first();
  146.                 if( !$product->getAvailable() ) {
  147.                     return true;
  148.                 }
  149.             }
  150.         }
  151.         return false;
  152.     }
  153. }