1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tax\Observer;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Module\Manager;
use Magento\PageCache\Model\Config;
use Magento\Tax\Api\TaxAddressManagerInterface;
use Magento\Tax\Helper\Data;
class CustomerLoggedInObserver implements ObserverInterface
{
/**
* @var Session
*/
protected $customerSession;
/**
* @var Data
*/
protected $taxHelper;
/**
* Module manager
*
* @var Manager
*/
private $moduleManager;
/**
* Cache config
*
* @var Config
*/
private $cacheConfig;
/**
* @var GroupRepositoryInterface
*/
private $groupRepository;
/**
* Manager to save data in customer session.
*
* @var TaxAddressManagerInterface
*/
private $addressManager;
/**
* @param GroupRepositoryInterface $groupRepository
* @param Session $customerSession
* @param Data $taxHelper
* @param Manager $moduleManager
* @param Config $cacheConfig
* @param TaxAddressManagerInterface $addressManager
*/
public function __construct(
GroupRepositoryInterface $groupRepository,
Session $customerSession,
Data $taxHelper,
Manager $moduleManager,
Config $cacheConfig,
TaxAddressManagerInterface $addressManager
) {
$this->groupRepository = $groupRepository;
$this->customerSession = $customerSession;
$this->taxHelper = $taxHelper;
$this->moduleManager = $moduleManager;
$this->cacheConfig = $cacheConfig;
$this->addressManager = $addressManager;
}
/**
* @param Observer $observer
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute(Observer $observer)
{
if ($this->moduleManager->isEnabled('Magento_PageCache')
&& $this->cacheConfig->isEnabled()
&& $this->taxHelper->isCatalogPriceDisplayAffectedByTax()
) {
/** @var \Magento\Customer\Model\Data\Customer $customer */
$customer = $observer->getData('customer');
$customerGroupId = $customer->getGroupId();
$customerGroup = $this->groupRepository->getById($customerGroupId);
$customerTaxClassId = $customerGroup->getTaxClassId();
$this->customerSession->setCustomerTaxClassId($customerTaxClassId);
$addresses = $customer->getAddresses();
if (isset($addresses)) {
$this->addressManager->setDefaultAddressAfterLogIn($addresses);
}
}
}
}