custom/plugins/CoeWishlistSw6/src/Core/Content/Wishlist/Subscriber/TransferWishlistSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeWishlistSw6\Core\Content\Wishlist\Subscriber;
  3. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistService;
  4. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. /**
  11.  * Class TransferWishlistSubscriber
  12.  * The Database is able to save a wishlist with an sessionId or a customerId.
  13.  * If a user is not logged in, the lists are bound to the session ID.
  14.  * Once the user logs in, we need to change the storage methode from "sessionId" to "userId".
  15.  * This basically means, that after a user logs in, the lists which has been created previously will be bound
  16.  * to the customer itself. The main goal is to persist the lists. After the user logs out again, there should be
  17.  * no notes associated anymore.
  18.  * @package CoeWishlistSw6\Core\Content\Wishlist\Subscriber
  19.  * @author Jeffry Block <[email protected]>
  20.  */
  21. Class TransferWishlistSubscriber implements EventSubscriberInterface {
  22.     /**
  23.      * @var WishlistServiceInterface
  24.      */
  25.     private $wishlistService;
  26.     /**
  27.      * @var Session
  28.      */
  29.     private $session;
  30.     /**
  31.      * CustomerSubscriber constructor.
  32.      * @param WishlistServiceInterface $wishlistService
  33.      * @param Session $session
  34.      */
  35.     public function __construct(WishlistServiceInterface $wishlistServiceSession $session)
  36.     {
  37.         $this->wishlistService $wishlistService;
  38.         $this->session $session;
  39.     }
  40.     /**
  41.      * @return array
  42.      * @author Jeffry Block <[email protected]>
  43.      */
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             CustomerLoginEvent::class => 'onCustomerLoggedIn',
  48.             CustomerBeforeLoginEvent::class => 'onCustomerBeforeLogin',
  49.             CustomerRegisterEvent::class => 'onCustomerRegister'
  50.         ];
  51.     }
  52.     /**
  53.      * When a user logs in, a new session ID will be generated.
  54.      * As we need the "old" ID to find the lists associated to it, we need to
  55.      * save it temporary.
  56.      * @param CustomerBeforeLoginEvent $event
  57.      * @author Jeffry Block <[email protected]>
  58.      */
  59.     public function onCustomerBeforeLogin(CustomerBeforeLoginEvent $event){
  60.         $this->session->set("tmp_SessionId"$this->session->getId());
  61.     }
  62.     /**
  63.      * When a user registers, a new session ID will be generated.
  64.      * As we need the "old" ID to find the lists associated to it, we need to
  65.      * save it temporary.
  66.      * @param CustomerRegisterEvent $event
  67.      * @author Jeffry Block <[email protected]>
  68.      */
  69.     public function onCustomerRegister(CustomerRegisterEvent $event){
  70.         $this->session->set("tmp_SessionId"$this->session->getId());
  71.     }
  72.     /**
  73.      * As mentioned above, we can not use the current Session ID to transfer the lists.
  74.      * So we need to get the "old" session ID and transfer the lists by using the wishlistService.
  75.      * After the lists has been transfered, the "old" session ID will be deleted.
  76.      * @param CustomerLoginEvent|null $event
  77.      * @author Jeffry Block <[email protected]>
  78.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  79.      */
  80.     public function onCustomerLoggedIn(CustomerLoginEvent $event){
  81.         if(is_null($this->session->get("tmp_SessionId"))){
  82.             return;
  83.         }
  84.         $this->wishlistService->transferList(
  85.             $this->session->get("tmp_SessionId"),
  86.             $event->getCustomer()->getId(),
  87.             $event->getSalesChannelContext()
  88.         );
  89.         $this->wishlistService->resetDefault(
  90.             $event->getCustomer()->getId(),
  91.             $event->getSalesChannelContext()->getContext()
  92.         );
  93.         $this->session->remove("tmp_SessionId");
  94.     }
  95. }