custom/plugins/CogiAffiliate/src/Subscriber/StorefrontSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\Affiliate\Subscriber;
  3. use Cogi\Affiliate\Core\Service\AffiliateService;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Storefront\Page\PageLoadedEvent;
  6. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  7. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. class StorefrontSubscriber implements EventSubscriberInterface
  13. {
  14.     protected $affiliateService;
  15.     protected $systemConfigService;
  16.     public function __construct(
  17.         AffiliateService $affiliateService,
  18.         SystemConfigService $systemConfigService
  19.     )
  20.     {
  21.         $this->affiliateService $affiliateService;
  22.         $this->systemConfigService $systemConfigService;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  28.             ProductPageCriteriaEvent::class => 'onProductCriteriaLoaded',
  29.             PageLoadedEvent::class => 'onPageLoaded',
  30.             HeaderPageletLoadedEvent::class => 'onPageLoaded',
  31.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded'
  32.         ];
  33.     }
  34.     public function onPageLoaded($event): void
  35.     {
  36.         $urlParameter = empty($this->systemConfigService->get('CogiAffiliate.config.urlParameter')) ? 'affiliateCode' $this->systemConfigService->get('CogiAffiliate.config.urlParameter');
  37.         $affiliateCode $event->getRequest()->query->get($urlParameter);
  38.         if ($affiliateCode) {
  39.             $session = new Session();
  40.             $session->set('cogi-affiliate-code'$affiliateCode);
  41.         }
  42.     }
  43.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  44.     {
  45.         $order $event->getOrder();
  46.         $context $event->getContext();
  47.         try {
  48.             $this->affiliateService->calculateAffiliateProvision($context$order);
  49.         } catch (\Exception $e) {}
  50.     }
  51.     public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
  52.     {
  53.         // If current user is affiliate, add productGroups association
  54.         try {
  55.             if ($event->getSalesChannelContext()->getCustomer() && $event->getSalesChannelContext()->getCustomer()->getCustomFields() && $event->getSalesChannelContext()->getCustomer()->getCustomFields()['ca_active']) {
  56.                 $event->getCriteria()->addAssociation('productGroups');
  57.             }
  58.         } catch (\Exception $e) {}
  59.     }
  60.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  61.     {
  62.         if ($this->systemConfigService->get('CogiAffiliate.config.enableCheckoutAffiliateSelection'$event->getSalesChannelContext()->getSalesChannelId())) {
  63.             $event->getContext()->addExtension('cogi-affiliate-affiliates'$this->affiliateService->getAffiliates($event->getContext()));
  64.         }
  65.     }
  66. }