EmulateCustomerObserver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. /**
  9. * Class EmulateCustomer
  10. */
  11. class EmulateCustomerObserver implements ObserverInterface
  12. {
  13. /**
  14. * Customer session
  15. *
  16. * @var \Magento\Customer\Model\Session
  17. */
  18. protected $_customerSession;
  19. /**
  20. * Persistent session
  21. *
  22. * @var \Magento\Persistent\Helper\Session
  23. */
  24. protected $_persistentSession;
  25. /**
  26. * Persistent data
  27. *
  28. * @var \Magento\Persistent\Helper\Data
  29. */
  30. protected $_persistentData;
  31. /**
  32. * Customer repository
  33. *
  34. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  35. */
  36. protected $customerRepository;
  37. /**
  38. * @var \Magento\Customer\Api\AddressRepositoryInterface
  39. */
  40. protected $addressRepository;
  41. /**
  42. * Constructor
  43. *
  44. * @param \Magento\Persistent\Helper\Session $persistentSession
  45. * @param \Magento\Persistent\Helper\Data $persistentData
  46. * @param \Magento\Customer\Model\Session $customerSession
  47. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  48. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  49. */
  50. public function __construct(
  51. \Magento\Persistent\Helper\Session $persistentSession,
  52. \Magento\Persistent\Helper\Data $persistentData,
  53. \Magento\Customer\Model\Session $customerSession,
  54. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  55. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  56. ) {
  57. $this->_persistentSession = $persistentSession;
  58. $this->_persistentData = $persistentData;
  59. $this->_customerSession = $customerSession;
  60. $this->customerRepository = $customerRepository;
  61. $this->addressRepository = $addressRepository;
  62. }
  63. /**
  64. * Set persistent data to customer session
  65. *
  66. * @param \Magento\Framework\Event\Observer $observer
  67. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  68. *
  69. * @return $this
  70. */
  71. public function execute(\Magento\Framework\Event\Observer $observer)
  72. {
  73. if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) {
  74. return $this;
  75. }
  76. if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) {
  77. /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
  78. $customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId());
  79. if ($defaultShipping = $customer->getDefaultShipping()) {
  80. /** @var \Magento\Customer\Model\Data\Address $address */
  81. $address = $this->addressRepository->getById($defaultShipping);
  82. if ($address) {
  83. $this->_customerSession->setDefaultTaxShippingAddress(
  84. [
  85. 'country_id' => $address->getCountryId(),
  86. 'region_id' => $address->getRegion()
  87. ? $address->getRegionId()
  88. : null,
  89. 'postcode' => $address->getPostcode(),
  90. ]
  91. );
  92. }
  93. }
  94. if ($defaultBilling = $customer->getDefaultBilling()) {
  95. $address = $this->addressRepository->getById($defaultBilling);
  96. if ($address) {
  97. $this->_customerSession->setDefaultTaxBillingAddress([
  98. 'country_id' => $address->getCountryId(),
  99. 'region_id' => $address->getRegion() ? $address->getRegionId() : null,
  100. 'postcode' => $address->getPostcode(),
  101. ]);
  102. }
  103. }
  104. $this->_customerSession
  105. ->setCustomerId($customer->getId())
  106. ->setCustomerGroupId($customer->getGroupId())
  107. ->setIsCustomerEmulated(true);
  108. }
  109. return $this;
  110. }
  111. }