custom/plugins/CustomContentPdp/src/Subscriber/MySubscriber.php line 98

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Vitec\CustomContentPdp\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
  8. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Struct\ArrayEntity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  14. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  15. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  16. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  17. use Shopware\Core\Checkout\Cart\Event\CartSavedEvent;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  20. use Doctrine\DBAL\Connection;
  21. use Shopware\Core\Framework\Uuid\Uuid;
  22. use Shopware\Core\Defaults;
  23. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  24. use Shopware\Core\Content\Category\CategoryEntity;
  25. use DreiscSeoPro\Core\Content\Category\CategoryRepository;
  26. use Shopware\Core\Content\Category\CategoryDefinition;
  27. use Shopware\Core\Content\Product\ProductEntity;
  28. use Shopware\Core\Framework\Context;
  29. //~ use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  30. class MySubscriber implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @var EntityRepositoryInterface
  34.      */
  35.     private $productRepository;
  36.     private $categoryRepository;
  37.     private RequestStack $requestStack;
  38.     private LineItemFactoryRegistry $factory;
  39.     private CartService $cartService;
  40.     private $connection;
  41.     public function __construct(
  42.         EntityRepositoryInterface $productRepository,
  43.         RequestStack $requestStack,
  44.         LineItemFactoryRegistry $factory
  45.         CartService $cartService,
  46.         Connection $connection,
  47.         EntityRepositoryInterface   $categoryRepository
  48.     )
  49.     {
  50.         $this->productRepository $productRepository;
  51.         $this->categoryRepository $categoryRepository;
  52.         $this->requestStack $requestStack;
  53.         $this->factory $factory;
  54.         $this->cartService $cartService;
  55.         $this->connection $connection;
  56.     }
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  60.          return [
  61.              ProductPageLoadedEvent::class => 'onProductPageLoaded',
  62.              
  63.          ];
  64.     }
  65.     // public function getCategoryById(string $categoryId, Context $context): ?CategoryEntity
  66.     // {
  67.     //     // Fetch the category by its ID
  68.     //     $criteria = new Criteria([$categoryId]);
  69.     //     $criteria->addAssociations(['media', 'translations','customFields']);
  70.     //     return $this->categoryRepository->search($criteria, $context)->first();
  71.     // }
  72.     public function getCategoryById(string $categoryIdContext $context $languageId string): ?CategoryEntity
  73.     {
  74.         // Define the criteria with custom fields associations
  75.         $criteria = (new Criteria([$categoryId]))->addAssociations([
  76.             //~ 'media',
  77.             //~ 'translations',
  78.             'customFields'// Replace 'customFields' with the actual name of your custom field association
  79.         ]);
  80.         //~ $criteria->addFilter(new EqualsFilter('category_translation.language_id', $languageId));
  81.         // Fetch the category by its ID along with custom fields
  82.         return $this->categoryRepository->search($criteria$context)->first();
  83.     }
  84.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  85.     {
  86.         
  87.         $_SESSION['product_id_insurance_add'] = 0;
  88.         $Context $event->getContext();
  89.         $page $event->getPage();
  90.         $category_value = [];
  91.         $product $page->getProduct();
  92.         $categories $product->getCategoryTree();
  93.         $arrCustomFields $product->getCustomFields();
  94.         $context1 Context::createDefaultContext(); 
  95.         $context Context::createDefaultContext(); 
  96.         $languageId $event->getSalesChannelContext()->getSalesChannel()->getLanguageId();
  97.         foreach ($categories as $category) {
  98.             $categorydetail $this->getCategoryById($category$context1,$languageId);
  99.             // dd($categorydetail instanceof CategoryEntity);
  100.             if ($categorydetail instanceof CategoryEntity) {
  101.             }
  102.             
  103.             if($categorydetail->customFields) {
  104.                 $category_description[] = $categorydetail->customFields;
  105.                 foreach ($category_description as $transaction) {
  106.                     if(!empty($transaction['custom_category_description'])) {
  107.                     
  108.                         $category_value[] = $transaction['custom_category_description'];
  109.                     }
  110.                 }
  111.             }
  112.             
  113.             
  114.         }
  115.         if(!empty($category_value)) {
  116.             $catCustomConetentArray = array("mactradePDP" => [$category_value[0]],'totalcount' => count($category_value));
  117.             $event->getPage()->addExtension('mactradePDPPageValue', new ArrayEntity($catCustomConetentArray));
  118.         }
  119.            $criteria = (new Criteria($event->getPage()->getProduct()->getCategoryIds()));
  120.         $categories $this->categoryRepository->search($criteria$context1);
  121.         $catNamearray=[];
  122.         $catNamearrayb=[];
  123.         foreach($categories as $k => $cat) {
  124.             $catNamearray[] = $cat->getName();
  125.             $catNamearrayb[] = $cat->getTranslated()['breadcrumb'];
  126.         }
  127.         $catArray 
  128.         ['Mac & MacBook'    => '4'
  129.         ,'MacBook Air'    => '13'
  130.         ,'MacBook Pro'    => '14'
  131.         ,'MacBook'    => '5'
  132.         ,'Mac mini'    => '15'
  133.         ,'Mac Studio'    => '380'
  134.         ,'iMac'    => '2'
  135.         ,'iMac Pro'    => '11'
  136.         ,'PowerMac'    => '12'
  137.         ,'Mac Pro'    => '16'
  138.         ,'Apple'    => '128'
  139.         ,'iPad & Tablet'    => '150'
  140.         ,'iPad Pro'    => '316'
  141.         ,'iPad Air'    => '318'
  142.         ,'iPad'    => '317'
  143.         ,'iPad mini'    => '319'
  144.         ,'TV & Musik'    => '13'
  145.         ,'Watch'    => '73'
  146.         ,'Zubehör'    => '74'
  147.         ,'iPhone & Handy'    => '18'
  148.         ,'Apple Tv'    => '321'
  149.         ,'Apple Watch'    => '102'
  150.         ];
  151.         $catArraytext 
  152.         [
  153.         'für Ihr altes MacBook Air'    => '13'
  154.         ,'für Ihr altes MacBook Pro'    => '14'
  155.         ,'für Ihren alten Mac mini'    => '15'
  156.         ,'für Ihren alten Mac Studio'    => '380'
  157.         ,'für Ihren alten iMac'    => '2'
  158.         ,'für Ihren alten Mac Pro'    => '16'
  159.         ,'für Ihr altes iPad Pro'    => '316'
  160.         ,'für Ihr altes iPad Air'    => '318'
  161.         ,'für Ihr altes iPad'    => '317'
  162.         ,'für Ihr altes iPad mini'    => '319'
  163.         ,'für Ihr altes iPhone'    => '18'
  164.         ,'für Ihren alten Apple TV'    => '321'
  165.         ,'für Ihre alte Apple Watch '    => '102'
  166.         ];
  167.         $foundKey null;
  168.         foreach($catNamearray as $catName) {
  169.         //~ $catName = $categories->getTranslated()['name'];
  170.         $catName strtolower($catName);
  171.         foreach ($catArray as $key => $value) {
  172.             if($catName == strtolower('iPhone')) {
  173.                 $foundKey '18';
  174.                 break;
  175.             }
  176.             if($catName == strtolower('MacBook Pro') || $catName == strtolower('14“ MacBook Pro') || $catName == strtolower('16“ MacBook Pro')) {
  177.                 $foundKey '14';
  178.                 break;
  179.             }
  180.             if($catName == strtolower('iPad Air')) {
  181.                 $foundKey '318';
  182.                 break;
  183.             }
  184.             if($catName == strtolower('iPad mini')) {
  185.                 $foundKey '319';
  186.                 break;
  187.             }
  188.             if($catName == strtolower('iPad')) {
  189.                 $foundKey '317';
  190.                 break;
  191.             }
  192.             if ($catName == $key) {
  193.                 $foundKey $catArray[$key];
  194.                 break;
  195.             }
  196.         }
  197.         }
  198.         if($foundKey == null) {
  199.             $breads array_values($catNamearrayb);
  200.             foreach($breads as $k => $bread) {
  201.             //~ $remove1 = array_shift($bread); 
  202.             foreach($bread as $k => $catgory) {
  203.                 $catgory strtolower($catgory);
  204.                 foreach ($catArray as $key => $value) {
  205.                     $pattern "/$catgory/i";
  206.                     if($catgory == strtolower('iPhone')) {
  207.                         $foundKey '18';
  208.                         break;
  209.                     }else if($catgory == strtolower('watch')) {
  210.                         $foundKey '102';
  211.                         break;
  212.                     }else if($catgory == strtolower('iPad Air')) {
  213.                         $foundKey '318';
  214.                         break;
  215.                     }
  216.                     //~ else if ($catgory == 'mac') {
  217.                         //~ $foundKey = '4';
  218.                         //~ break;
  219.                     //~ }
  220.                     else if ($catgory == strtolower($key)) {
  221.                         $foundKey $catArray[$key];
  222.                         break;
  223.                     }
  224.                     else if (strpos($patternstrtolower($key)) !== false) {
  225.                     }
  226.                 }
  227.                 }
  228.             }
  229.         }
  230.         $catArraytext array_flip($catArraytext);
  231.         $newArray = [];
  232.         if ($foundKey !== null) {
  233.             $newArray['keyApi'] = $foundKey;
  234.             $newArray['keyApiText'] = $catArraytext[$foundKey];
  235.         } else {
  236.             //~ $newArray['keyApi'] = '18';
  237.             //~ $newArray['keyApiText'] = $catArraytext[18];
  238.             //~ return;
  239.         }
  240.  
  241.         if($newArray) {
  242.             $event->getPage()->addExtension('amMactradeap1', new ArrayEntity($newArray));
  243.         }
  244.     }
  245.     public function onProductsLoaded(EntityLoadedEvent $event)
  246.     {
  247.         dd($event);
  248.     }
  249. }