custom/plugins/b2bsellerscore/src/Components/CustomerActivity/Subscriber/OrderPlacedSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\CustomerActivity\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use B2bSellersCore\Components\CustomerActivity\Aggregate\CustomerActivityType\ActivityTypes;
  5. use B2bSellersCore\Components\CustomerActivity\Service\ActivityServiceInterface;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Shopware\Core\Checkout\Order\OrderDefinition;
  8. use Shopware\Core\PlatformRequest;
  9. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  10. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class OrderPlacedSubscriber implements EventSubscriberInterface
  14. {
  15.     use B2bContextTrait;
  16.     private ActivityServiceInterface $activityService;
  17.     private SalesChannelContextServiceInterface $contextService;
  18.     private RequestStack $requestStack;
  19.     public const ACTIVITY_CREATE_ORDER_PLACED_SNIPPET 'b2bPlatform.activity.order.placed';
  20.     public function __construct(
  21.         ActivityServiceInterface            $activityService,
  22.         SalesChannelContextServiceInterface $channelContextService,
  23.         RequestStack                        $requestStack
  24.     )
  25.     {
  26.         $this->activityService $activityService;
  27.         $this->contextService $channelContextService;
  28.         $this->requestStack $requestStack;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             CheckoutOrderPlacedEvent::class => 'addOrderPlacedActivity'
  34.         ];
  35.     }
  36.     public function addOrderPlacedActivity(CheckoutOrderPlacedEvent $event): void
  37.     {
  38.         $request $this->requestStack->getCurrentRequest();
  39.         if (!isset($request)) {
  40.             return;
  41.         }
  42.         if (!$request->headers->has(PlatformRequest::HEADER_CONTEXT_TOKEN)) {
  43.             return;
  44.         }
  45.         $contextToken $request->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN);
  46.         $salesChannelContext $this->contextService->get(
  47.             new SalesChannelContextServiceParameters(
  48.                 $event->getSalesChannelId(),
  49.                 $contextToken,
  50.                 $event->getContext()->getLanguageId(),
  51.                 $event->getContext()->getCurrencyId()
  52.             )
  53.         );
  54.         if (!$this->hasB2bPlatformContext($salesChannelContext)) {
  55.             return;
  56.         }
  57.         if ($this->isLoggedInAsSalesRepresentative($salesChannelContext)) {
  58.             return;
  59.         }
  60.         try {
  61.             $this->activityService->addActivity(
  62.                 ActivityTypes::ACTIVITY_TYPE_ORDER,
  63.                 '',
  64.                 self::ACTIVITY_CREATE_ORDER_PLACED_SNIPPET,
  65.                 OrderDefinition::ENTITY_NAME,
  66.                 $event->getOrder()->getId(),
  67.                 $salesChannelContext
  68.             );
  69.         } catch (\Exception $exception) {
  70.         }
  71.     }
  72. }