MakePersistentQuoteGuestObserver.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. class MakePersistentQuoteGuestObserver implements ObserverInterface
  10. {
  11. /**
  12. * Customer session
  13. *
  14. * @var \Magento\Customer\Model\Session
  15. */
  16. protected $_customerSession;
  17. /**
  18. * Persistent session
  19. *
  20. * @var \Magento\Persistent\Helper\Session
  21. */
  22. protected $_persistentSession = null;
  23. /**
  24. * Persistent data
  25. *
  26. * @var \Magento\Persistent\Helper\Data
  27. */
  28. protected $_persistentData = null;
  29. /**
  30. * @var \Magento\Persistent\Model\QuoteManager
  31. */
  32. protected $quoteManager;
  33. /**
  34. * @param \Magento\Persistent\Helper\Session $persistentSession
  35. * @param \Magento\Persistent\Helper\Data $persistentData
  36. * @param \Magento\Customer\Model\Session $customerSession
  37. * @param \Magento\Persistent\Model\QuoteManager $quoteManager
  38. */
  39. public function __construct(
  40. \Magento\Persistent\Helper\Session $persistentSession,
  41. \Magento\Persistent\Helper\Data $persistentData,
  42. \Magento\Customer\Model\Session $customerSession,
  43. \Magento\Persistent\Model\QuoteManager $quoteManager
  44. ) {
  45. $this->_persistentSession = $persistentSession;
  46. $this->_persistentData = $persistentData;
  47. $this->_customerSession = $customerSession;
  48. $this->quoteManager = $quoteManager;
  49. }
  50. /**
  51. * Make persistent quote to be guest
  52. *
  53. * @param \Magento\Framework\Event\Observer $observer
  54. * @return void
  55. */
  56. public function execute(\Magento\Framework\Event\Observer $observer)
  57. {
  58. /** @var $action \Magento\Persistent\Controller\Index */
  59. $action = $observer->getEvent()->getControllerAction();
  60. if ($action instanceof \Magento\Persistent\Controller\Index) {
  61. if ((($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn())
  62. || $this->_persistentData->isShoppingCartPersist())
  63. ) {
  64. $this->quoteManager->setGuest(true);
  65. }
  66. }
  67. }
  68. }