RemovePersistentCookieOnRegisterObserver.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Observer;
  8. use Magento\Framework\Event\ObserverInterface;
  9. /**
  10. * Plugin to change persistent session cart to guest cart on new customer register success.
  11. */
  12. class RemovePersistentCookieOnRegisterObserver implements ObserverInterface
  13. {
  14. /**
  15. * Customer session
  16. *
  17. * @var \Magento\Customer\Model\Session
  18. */
  19. protected $_customerSession;
  20. /**
  21. * Persistent session
  22. *
  23. * @var \Magento\Persistent\Helper\Session
  24. */
  25. protected $_persistentSession = null;
  26. /**
  27. * Persistent data
  28. *
  29. * @var \Magento\Persistent\Helper\Data
  30. */
  31. protected $_persistentData = null;
  32. /**
  33. * @var \Magento\Persistent\Model\QuoteManager
  34. */
  35. protected $quoteManager;
  36. /**
  37. * @param \Magento\Persistent\Helper\Session $persistentSession
  38. * @param \Magento\Persistent\Helper\Data $persistentData
  39. * @param \Magento\Customer\Model\Session $customerSession
  40. * @param \Magento\Persistent\Model\QuoteManager $quoteManager
  41. */
  42. public function __construct(
  43. \Magento\Persistent\Helper\Session $persistentSession,
  44. \Magento\Persistent\Helper\Data $persistentData,
  45. \Magento\Customer\Model\Session $customerSession,
  46. \Magento\Persistent\Model\QuoteManager $quoteManager
  47. ) {
  48. $this->quoteManager = $quoteManager;
  49. $this->_persistentSession = $persistentSession;
  50. $this->_persistentData = $persistentData;
  51. $this->_customerSession = $customerSession;
  52. }
  53. /**
  54. * Unset persistent cookie and make customer's quote as a guest
  55. *
  56. * @param \Magento\Framework\Event\Observer $observer
  57. * @return void
  58. */
  59. public function execute(\Magento\Framework\Event\Observer $observer)
  60. {
  61. if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent()) {
  62. return;
  63. }
  64. $this->_persistentSession->getSession()->removePersistentCookie();
  65. if (!$this->_customerSession->isLoggedIn()) {
  66. $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
  67. }
  68. $this->quoteManager->setGuest();
  69. }
  70. }