EmulateQuoteObserver.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * Class EmulateQuote
  11. */
  12. class EmulateQuoteObserver implements ObserverInterface
  13. {
  14. /**
  15. * Customer account service
  16. *
  17. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  18. */
  19. protected $customerRepository;
  20. /**
  21. * Customer session
  22. *
  23. * @var \Magento\Customer\Model\Session
  24. */
  25. protected $_customerSession;
  26. /**
  27. * Checkout session
  28. *
  29. * @var \Magento\Checkout\Model\Session
  30. */
  31. protected $_checkoutSession;
  32. /**
  33. * Persistent session
  34. *
  35. * @var \Magento\Persistent\Helper\Session
  36. */
  37. protected $_persistentSession = null;
  38. /**
  39. * Persistent data
  40. *
  41. * @var \Magento\Persistent\Helper\Data
  42. */
  43. protected $_persistentData = null;
  44. /**
  45. * @param \Magento\Persistent\Helper\Session $persistentSession
  46. * @param \Magento\Persistent\Helper\Data $persistentData
  47. * @param \Magento\Checkout\Model\Session $checkoutSession
  48. * @param \Magento\Customer\Model\Session $customerSession
  49. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  50. */
  51. public function __construct(
  52. \Magento\Persistent\Helper\Session $persistentSession,
  53. \Magento\Persistent\Helper\Data $persistentData,
  54. \Magento\Checkout\Model\Session $checkoutSession,
  55. \Magento\Customer\Model\Session $customerSession,
  56. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  57. ) {
  58. $this->_persistentSession = $persistentSession;
  59. $this->_persistentData = $persistentData;
  60. $this->_checkoutSession = $checkoutSession;
  61. $this->_customerSession = $customerSession;
  62. $this->customerRepository = $customerRepository;
  63. }
  64. /**
  65. * Emulate quote by persistent data
  66. *
  67. * @param \Magento\Framework\Event\Observer $observer
  68. * @return void
  69. */
  70. public function execute(\Magento\Framework\Event\Observer $observer)
  71. {
  72. $stopActions = ['persistent_index_saveMethod', 'customer_account_createpost'];
  73. if (!$this->_persistentData->canProcess($observer)
  74. || !$this->_persistentSession->isPersistent()
  75. || $this->_customerSession->isLoggedIn()
  76. ) {
  77. return;
  78. }
  79. $actionName = $observer->getEvent()->getRequest()->getFullActionName();
  80. if (in_array($actionName, $stopActions)) {
  81. return;
  82. }
  83. if ($this->_persistentData->isShoppingCartPersist()) {
  84. $this->_checkoutSession->setCustomerData(
  85. $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId())
  86. );
  87. if (!$this->_checkoutSession->hasQuote()) {
  88. $this->_checkoutSession->getQuote();
  89. }
  90. }
  91. }
  92. }