custom/plugins/BrandCrockWhatsappChat/src/Subscriber/MySubscriber.php line 97

Open in your IDE?
  1. <?php
  2. /**
  3.  * To plugin Subscriber file.
  4.  *
  5.  * Copyright (C) BrandCrock GmbH. All rights reserved
  6.  *
  7.  * If you have found this script useful a small
  8.  * recommendation as well as a comment on our
  9.  * home page(https://brandcrock.com/)
  10.  * would be greatly appreciated.
  11.  *
  12.  * @author BrandCrock GmbH
  13.  * @package BrandCrockWhatsappChat
  14.  */
  15. declare(strict_types=1);
  16. namespace Brandcrock\BrandCrockWhatsappChat\Subscriber;
  17. use Shopware\Core\Framework\Struct\ArrayEntity;
  18. use Shopware\Core\System\SystemConfig\SystemConfigService;
  19. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  20. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  21. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  22. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  23. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  24. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  25. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  26. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  27. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  28. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  29. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  30. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  31. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  32. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  33. use Shopware\Core\Framework\Context;
  34. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  35. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  36. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  37. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  38. class MySubscriber implements EventSubscriberInterface
  39. {
  40.     private $systemConfigService;
  41.     private $userRespository;
  42.     /**
  43.      * @var string
  44.      */
  45.     protected $shopVersion;
  46.     /**
  47.      * @param $systemConfigService SystemConfigService
  48.      */
  49.     public function __construct(SystemConfigService $systemConfigService,EntityRepository $userRespositorystring $shopVersion)
  50.     {
  51.         $this->systemConfigService $systemConfigService;
  52.         $this->userRespository $userRespository;
  53.         $this->shopVersion            $shopVersion;
  54.     }
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return[
  58.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  59.             NavigationPageLoadedEvent::class => 'onProductsLoadedDetail',
  60.             SearchPageLoadedEvent::class => 'onSearchPageLoadedEvent',
  61.             AccountProfilePageLoadedEvent::class => 'onAccountProfilePageLoadedEvent',
  62.             AccountPaymentMethodPageLoadedEvent::class => 'onAccountPaymentMethodPageLoadedEvent',
  63.             AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoadedEvent',
  64.             AccountOrderPageLoadedEvent::class => 'onAccountOrderPageLoadedEvent',
  65.             AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoadedEventt',
  66.             AddressDetailPageLoadedEvent::class => 'onAddressDetailPageLoadedEvent',
  67.             AddressListingPageLoadedEvent::class => 'onAddressListingPageLoadedEvent',
  68.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
  69.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
  70.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
  71.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoadedEvent'
  72.         ];
  73.     }
  74.     //on listing
  75.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  76.     {
  77.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  78.         $systemConfig $this->getAssignedConfig($salechannelId);
  79.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  80.     }
  81.     //on detail page
  82.     public function onProductsLoadedDetail(NavigationPageLoadedEvent $event): void
  83.     {
  84.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  85.         $systemConfig $this->getAssignedConfig($salechannelId);
  86.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  87.     }
  88.     //on search result page
  89.     public function onSearchPageLoadedEvent(SearchPageLoadedEvent $event): void
  90.     {
  91.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  92.         $systemConfig $this->getAssignedConfig($salechannelId);
  93.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  94.     }
  95.     //on account profile page
  96.     public function onAccountProfilePageLoadedEvent(AccountProfilePageLoadedEvent $event): void
  97.     {
  98.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  99.         $systemConfig $this->getAssignedConfig($salechannelId);
  100.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  101.     }
  102.     //on payment method
  103.     public function onAccountPaymentMethodPageLoadedEvent(AccountPaymentMethodPageLoadedEvent $event): void
  104.     {
  105.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  106.         $systemConfig $this->getAssignedConfig($salechannelId);
  107.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  108.     }
  109.     //on account overview page
  110.     public function onAccountOverviewPageLoadedEvent(AccountOverviewPageLoadedEvent $event): void
  111.     {
  112.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  113.         $systemConfig $this->getAssignedConfig($salechannelId);
  114.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  115.     }
  116.     //on account order page
  117.     public function onAccountOrderPageLoadedEvent(AccountOrderPageLoadedEvent $event): void
  118.     {
  119.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  120.         $systemConfig $this->getAssignedConfig($salechannelId);
  121.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  122.     }
  123.     //on account login page
  124.     public function onAccountLoginPageLoadedEventt(AccountLoginPageLoadedEvent $event): void
  125.     {
  126.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  127.         $systemConfig $this->getAssignedConfig($salechannelId);
  128.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  129.     }
  130.     //on address detail page
  131.     public function onAddressDetailPageLoadedEvent(AddressDetailPageLoadedEvent $event): void
  132.     {
  133.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  134.         $systemConfig $this->getAssignedConfig($salechannelId);
  135.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  136.     }
  137.     //on address listing page
  138.     public function onAddressListingPageLoadedEvent(AddressListingPageLoadedEvent $event): void
  139.     {
  140.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  141.         $systemConfig $this->getAssignedConfig($salechannelId);
  142.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  143.     }
  144.     //on cart page
  145.     public function onCheckoutCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event): void
  146.     {
  147.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  148.         $systemConfig $this->getAssignedConfig($salechannelId);
  149.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  150.     }
  151.     //on checkout confirm page
  152.     public function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): void
  153.     {
  154.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  155.         $systemConfig $this->getAssignedConfig($salechannelId);
  156.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  157.     }
  158.     //on checkout finish page
  159.     public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event): void
  160.     {
  161.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  162.         $systemConfig $this->getAssignedConfig($salechannelId);
  163.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  164.     }
  165.     // on checkout register page
  166.     public function onCheckoutRegisterPageLoadedEvent(CheckoutRegisterPageLoadedEvent $event): void
  167.     {
  168.         $salechannelId  $event->getSalesChannelContext()->getSalesChannel()->getId();
  169.         $systemConfig $this->getAssignedConfig($salechannelId);
  170.         $event->getPage()->addExtension('BcWhatsApp', new ArrayEntity(['config' => $systemConfig]));
  171.     }
  172.     //get plugin configuration
  173.     private function getAssignedConfig($salechannelId)
  174.     {
  175.         $systemConfig $this->systemConfigService->get('BrandCrockWhatsappChat.config'$salechannelId);
  176.         $imagepath 'bundles/brandcrockwhatsappchat/storefront/assets/img/';
  177.         if ((array_key_exists('avatar'$systemConfig) && $systemConfig['avatar'])) {
  178.             $systemConfig['avatar'] = $imagepath.$systemConfig['avatar'];
  179.         }
  180.         if ((array_key_exists('salesAvatar'$systemConfig) && $systemConfig['salesAvatar'])) {
  181.             $systemConfig['salesAvatar'] = $imagepath.$systemConfig['salesAvatar'];
  182.         }
  183.         if(isset($systemConfig['openingHours']) && isset($systemConfig['closingsHours'])){
  184.             $systemConfig['from'] = $systemConfig['openingHours'] ;
  185.             $systemConfig['untill'] = $systemConfig['closingsHours'] ;
  186.         }
  187.         
  188.         $context Context::createDefaultContext();
  189.         $criteria = new Criteria();
  190.         $criteria->addFilter(new EqualsFilter('admin'1));
  191.         $data $this->userRespository->search($criteria$context)->first();
  192.         if($this->shopVersion '6.4.5.0'){
  193.             $adminTimeZone = (array) $data->getCreatedat();
  194.             $systemConfig['timeZone'] =  $adminTimeZone['timezone'];
  195.         }else{
  196.             $systemConfig['timeZone'] =  $data->timeZone;
  197.         }
  198.         return $systemConfig;
  199.     }
  200. }