SetQuotePersistentDataObserver.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 SetQuotePersistentDataObserver 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\Persistent\Model\QuoteManager $quoteManager
  37. * @param \Magento\Customer\Model\Session $customerSession
  38. */
  39. public function __construct(
  40. \Magento\Persistent\Helper\Session $persistentSession,
  41. \Magento\Persistent\Helper\Data $persistentData,
  42. \Magento\Persistent\Model\QuoteManager $quoteManager,
  43. \Magento\Customer\Model\Session $customerSession
  44. ) {
  45. $this->_persistentSession = $persistentSession;
  46. $this->quoteManager = $quoteManager;
  47. $this->_customerSession = $customerSession;
  48. $this->_persistentData = $persistentData;
  49. }
  50. /**
  51. * Set persistent data into quote
  52. *
  53. * @param \Magento\Framework\Event\Observer $observer
  54. * @return void
  55. */
  56. public function execute(\Magento\Framework\Event\Observer $observer)
  57. {
  58. if (!$this->_persistentSession->isPersistent()) {
  59. return;
  60. }
  61. /** @var $quote \Magento\Quote\Model\Quote */
  62. $quote = $observer->getEvent()->getQuote();
  63. if (!$quote) {
  64. return;
  65. }
  66. if ((
  67. ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn())
  68. && !$this->_persistentData->isShoppingCartPersist()
  69. )
  70. && $this->quoteManager->isPersistent()
  71. ) {
  72. //Quote is not actual customer's quote, just persistent
  73. $quote->setIsPersistent(true);
  74. }
  75. }
  76. }