SetCheckoutSessionPersistentDataObserver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. declare(strict_types=1);
  8. namespace Magento\Persistent\Observer;
  9. use Magento\Framework\Event\ObserverInterface;
  10. /**
  11. * Class SetCheckoutSessionPersistentDataObserver
  12. */
  13. class SetCheckoutSessionPersistentDataObserver implements ObserverInterface
  14. {
  15. /**
  16. * Persistent session
  17. *
  18. * @var \Magento\Persistent\Helper\Session
  19. */
  20. private $persistentSession = null;
  21. /**
  22. * Customer session
  23. *
  24. * @var \Magento\Customer\Model\Session
  25. */
  26. private $customerSession;
  27. /**
  28. * Persistent data
  29. *
  30. * @var \Magento\Persistent\Helper\Data
  31. */
  32. private $persistentData = null;
  33. /**
  34. * Customer Repository
  35. *
  36. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  37. */
  38. private $customerRepository = null;
  39. /**
  40. * @param \Magento\Persistent\Helper\Session $persistentSession
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Persistent\Helper\Data $persistentData
  43. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  44. */
  45. public function __construct(
  46. \Magento\Persistent\Helper\Session $persistentSession,
  47. \Magento\Customer\Model\Session $customerSession,
  48. \Magento\Persistent\Helper\Data $persistentData,
  49. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  50. ) {
  51. $this->persistentSession = $persistentSession;
  52. $this->customerSession = $customerSession;
  53. $this->persistentData = $persistentData;
  54. $this->customerRepository = $customerRepository;
  55. }
  56. /**
  57. * Pass customer data from persistent session to checkout session and set quote to be loaded even if not active
  58. *
  59. * @param \Magento\Framework\Event\Observer $observer
  60. * @return void
  61. * @throws \Magento\Framework\Exception\NoSuchEntityException
  62. * @throws \Magento\Framework\Exception\LocalizedException
  63. */
  64. public function execute(\Magento\Framework\Event\Observer $observer)
  65. {
  66. /** @var $checkoutSession \Magento\Checkout\Model\Session */
  67. $checkoutSession = $observer->getEvent()->getData('checkout_session');
  68. if ($this->persistentData->isShoppingCartPersist() && $this->persistentSession->isPersistent()) {
  69. $checkoutSession->setCustomerData(
  70. $this->customerRepository->getById($this->persistentSession->getSession()->getCustomerId())
  71. );
  72. }
  73. if (!(($this->persistentSession->isPersistent() && !$this->customerSession->isLoggedIn())
  74. && !$this->persistentData->isShoppingCartPersist()
  75. )) {
  76. return;
  77. }
  78. if ($checkoutSession) {
  79. $checkoutSession->setLoadInactive();
  80. }
  81. }
  82. }