custom/plugins/TTTrustedShops/src/Subscriber/TrustedShopsSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TTTrustedShops\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use DreiscSeoPro\Subscriber\Installment\RichSnippet\RichSnippetSubscriber;
  9. class TrustedShopsSubscriber implements EventSubscriberInterface
  10. {
  11.     private $productReviewsApiUrl 'https://cdn1.api.trustedshops.com/shops/{{tsId}}/products/public/v1/feed.json';
  12.     /**
  13.      * @var SystemConfigService
  14.      */
  15.     private $systemConfigService;
  16.     /**
  17.      * @var EntityRepositoryInterface
  18.      */
  19.     private $productRepository;
  20.     public function __construct(EntityRepositoryInterface $productRepositorySystemConfigService $systemConfigService)
  21.     {
  22.         $this->productRepository $productRepository;
  23.         $this->systemConfigService $systemConfigService;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             ProductPageLoadedEvent::class => 'addSnippetReviewRating',
  29.         ];
  30.     }
  31.     /**
  32.      * @param ProductPageLoadedEvent $event
  33.      * @throws InconsistentCriteriaIdsException
  34.      */
  35.     public function addSnippetReviewRating(ProductPageLoadedEvent $event)
  36.     {
  37.         $page $event->getPage();
  38.         $product $page->getProduct();
  39.         $richSnippetData $page->getExtension(RichSnippetSubscriber::DREISC_SEO_INSTALLMENT_RICH_SNIPPET_DATA);
  40.         if($richSnippetData) {
  41.             $ld $richSnippetData->getLdJson();
  42.             $domain 'TrustedShops.config.';
  43.             $tsId $this->systemConfigService->get$domain 'tsId' );
  44.             $apiUrl str_replace'{{tsId}}'$tsId$this->productReviewsApiUrl );
  45.             $ch curl_init();
  46.             curl_setopt($chCURLOPT_HTTPHEADER, [ 'Accept: application/json' ] );
  47.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  48.             curl_setopt($chCURLOPT_URL$apiUrl);
  49.             $result curl_exec($ch);
  50.             curl_close($ch);
  51.             $shopReviews json_decode$result );
  52.             if ($shopReviews && $shopReviews->response && $shopReviews->response->data->shop->products) {
  53.                 foreach ($shopReviews->response->data->shop->products as $_product) {
  54.                     if ($product->getProductNumber() == $_product->sku) {
  55.                         $this->addAggregateRatingAndReviews($ld$_product);
  56.                     }
  57.                 }
  58.             }
  59.             //AM-760 Snippetanpassung für Hauptbild auf den SERPS
  60.             $this->fixMediaSnippets($ld$product);
  61.             $richSnippetData->setLdJson($ld);
  62.             if(!empty($richSnippetData->getLdJson())) {
  63.                 $page->addExtension(
  64.                     RichSnippetSubscriber::DREISC_SEO_INSTALLMENT_RICH_SNIPPET_DATA,
  65.                     $richSnippetData
  66.                 );
  67.             }
  68.         }
  69.     }
  70.     
  71.     private function fixMediaSnippets(array &$ld$_product) {
  72.         if(isset($ld[0]['image']) && count($ld[0]['image']) > 1) {
  73.             $images $ld[0]['image'];
  74.             //keep only image is a cover of product if product have a cover
  75.             foreach($images as $key => $image) {
  76.                 if(!empty($_product->getCover())) {
  77.                     if($image != $_product->getCover()->getMedia()->getUrl()) {
  78.                         unset($ld[0]['image'][$key]);
  79.                     }
  80.                 }
  81.             }
  82.             //keep a first image if product have not a cover
  83.             if(count($ld[0]['image']) > 1) {
  84.                 array_slice($ld[0]['image'],0,1);
  85.             }
  86.         }
  87.     }
  88.     private function addAggregateRatingAndReviews(array &$ld$_product)
  89.     {
  90.         /** Add the aggregate rating */
  91.         $ld[0]['aggregateRating'] = [
  92.             '@type' => 'AggregateRating',
  93.             'ratingValue' => $_product->qualityIndicators->reviewIndicator->overallMark,
  94.             'bestRating'  => '5',
  95.             'ratingCount' => $_product->qualityIndicators->reviewIndicator->totalReviewCount
  96.         ];
  97.         /** Add the reviews */
  98.         $reviews = [];
  99.         foreach($_product->productReviews as $productReview) {
  100.             $datePublished $productReview->creationDate;
  101.             $review = [
  102.                 '@type' => 'Review',
  103.                 'datePublished' => $datePublished,
  104.                 'name' => $_product->name,
  105.                 'description' => $productReview->comment,
  106.                 'author' => [
  107.                     '@type' => 'Person',
  108.                     'name' => 'unknown'
  109.                 ]
  110.             ];
  111.             $reviews[] = $review;
  112.         }
  113.         $ld[0]['review'] = $reviews;
  114.     }
  115. }