custom/plugins/ImnxxCartRestorer/src/Storefront/Subscriber/CartRestorerSubscriber.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ImnxxCartRestorer\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  5. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Doctrine\DBAL\Connection;
  8. class CartRestorerSubscriber implements EventSubscriberInterface {
  9.     private CartService $cartService;
  10.     private Connection $connection;
  11.     public function __construct(CartService $cartServiceConnection $connection) {
  12.         $this->cartService $cartService;
  13.         $this->connection $connection;
  14.     }
  15.     public static function getSubscribedEvents(): array {
  16.         return [
  17.             GenericPageLoadedEvent::class => 'onPageLoaded',
  18.             CustomerLoginEvent::class => ['onCustomerLogin'],
  19.         ];
  20.     }
  21.     public function onPageLoaded(GenericPageLoadedEvent $event) {
  22.         if (isset($_GET['cartId']) && !empty($_GET['cartId'])) {
  23.             $cartOld $this->cartService->getCart($_GET['cartId'], $event->getSalesChannelContext());
  24.             $cartCurrent $this->cartService->getCart(
  25.                 $event->getSalesChannelContext()->getToken(),
  26.                 $event->getSalesChannelContext(),
  27.             );
  28.             foreach ($cartOld->getLineItems() as $lineItem) {
  29.                 $this->cartService->add($cartCurrent$lineItem$event->getSalesChannelContext());
  30.             }
  31.         }
  32.     }
  33.     public function onCustomerLogin(CustomerLoginEvent $event) {
  34.         $customerId $event->getCustomerId();
  35.         $latestCartTokenFromCustomer =
  36.             $this->connection
  37.                 ->executeQuery(
  38.                     'SELECT token FROM cart WHERE customer_id = UNHEX(:customerId) ORDER BY created_at DESC LIMIT 1',
  39.                     [
  40.                         'customerId' => $customerId,
  41.                     ],
  42.                 )
  43.                 ->fetchAllAssociative()[0]['token'] ?? null;
  44.         if ($latestCartTokenFromCustomer != null) {
  45.             $currentCart $this->cartService->getCart(
  46.                 $event->getSalesChannelContext()->getToken(),
  47.                 $event->getSalesChannelContext(),
  48.             );
  49.             $lastCart $this->cartService->getCart(
  50.                 $latestCartTokenFromCustomer,
  51.                 $event->getSalesChannelContext(),
  52.             );
  53.             foreach ($lastCart->getLineItems() as $lineItem) {
  54.                 $this->cartService->add($currentCart$lineItem$event->getSalesChannelContext());
  55.             }
  56.         }
  57.         if (
  58.             isset($_COOKIE['DokumentenId_productnumber_for_login']) &&
  59.             $_COOKIE['DokumentenId_productnumber_for_login'] !== ''
  60.         ) {
  61.             setcookie('docoumentIdProductNumberAfterLogin'$_COOKIE['DokumentenId_productnumber_for_login']);
  62.             unset($_COOKIE['DokumentenId_productnumber_for_login']);
  63.         }
  64.         header('Location: ' $_SERVER['REQUEST_URI']);
  65.         die();
  66.     }
  67. }