PreventClearCheckoutSessionObserver.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 PreventClearCheckoutSessionObserver 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. * @param \Magento\Persistent\Helper\Session $persistentSession
  31. * @param \Magento\Persistent\Helper\Data $persistentData
  32. * @param \Magento\Customer\Model\Session $customerSession
  33. */
  34. public function __construct(
  35. \Magento\Persistent\Helper\Session $persistentSession,
  36. \Magento\Persistent\Helper\Data $persistentData,
  37. \Magento\Customer\Model\Session $customerSession
  38. ) {
  39. $this->_persistentSession = $persistentSession;
  40. $this->_persistentData = $persistentData;
  41. $this->_customerSession = $customerSession;
  42. }
  43. /**
  44. * Prevent clear checkout session
  45. *
  46. * @param \Magento\Framework\Event\Observer $observer
  47. * @return void
  48. */
  49. public function execute(\Magento\Framework\Event\Observer $observer)
  50. {
  51. /** @var $action \Magento\Persistent\Controller\Index */
  52. $action = $observer->getEvent()->getControllerAction();
  53. if ($action instanceof \Magento\Persistent\Controller\Index) {
  54. if (($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn())
  55. || !$this->_persistentData->isShoppingCartPersist()
  56. ) {
  57. $action->setClearCheckoutSession(false);
  58. }
  59. }
  60. }
  61. }