Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectTotalsObserver.php
Go to the documentation of this file.
1 <?php
7 
9 
11 {
15  private $addressRepository;
16 
20  private $customerSession;
21 
26 
30  protected $customerVat;
31 
35  protected $vatValidator;
36 
41 
47  protected $groupManagement;
48 
60  public function __construct(
61  \Magento\Customer\Helper\Address $customerAddressHelper,
62  \Magento\Customer\Model\Vat $customerVat,
64  \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
65  \Magento\Customer\Api\GroupManagementInterface $groupManagement,
66  \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
67  \Magento\Customer\Model\Session $customerSession
68  ) {
69  $this->customerVat = $customerVat;
70  $this->customerAddressHelper = $customerAddressHelper;
71  $this->vatValidator = $vatValidator;
72  $this->customerDataFactory = $customerDataFactory;
73  $this->groupManagement = $groupManagement;
74  $this->addressRepository = $addressRepository;
75  $this->customerSession = $customerSession;
76  }
77 
85  public function execute(\Magento\Framework\Event\Observer $observer)
86  {
88  $shippingAssignment = $observer->getShippingAssignment();
90  $quote = $observer->getQuote();
92  $address = $shippingAssignment->getShipping()->getAddress();
93 
94  $customer = $quote->getCustomer();
95  $storeId = $customer->getStoreId();
96 
97  if ($customer->getDisableAutoGroupChange()
98  || false == $this->vatValidator->isEnabled($address, $storeId)
99  ) {
100  return;
101  }
102  $customerCountryCode = $address->getCountryId();
103  $customerVatNumber = $address->getVatId();
104 
106  if (empty($customerCountryCode) && empty($customerVatNumber) && $customer->getDefaultShipping()) {
107  $customerAddress = $this->addressRepository->getById($customer->getDefaultShipping());
108 
109  $customerCountryCode = $customerAddress->getCountryId();
110  $customerVatNumber = $customerAddress->getVatId();
111  }
112 
113  $groupId = null;
114  if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) {
115  $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup(
116  $storeId
117  )->getId() : $this->groupManagement->getNotLoggedInGroup()->getId();
118  } else {
119  // Magento always has to emulate group even if customer uses default billing/shipping address
120  $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber(
121  $customerCountryCode,
122  $this->vatValidator->validate($address, $storeId),
123  $storeId
124  );
125  }
126 
127  if ($groupId) {
128  $address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
129  $quote->setCustomerGroupId($groupId);
130  $this->customerSession->setCustomerGroupId($groupId);
131  $customer->setGroupId($groupId);
132  $quote->setCustomer($customer);
133  }
134  }
135 }
$addressRepository
$customer
Definition: customers.php:11
$quote
$address
Definition: customer.php:38
__construct(\Magento\Customer\Helper\Address $customerAddressHelper, \Magento\Customer\Model\Vat $customerVat, VatValidator $vatValidator, \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory, \Magento\Customer\Api\GroupManagementInterface $groupManagement, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Customer\Model\Session $customerSession)
$customerAddress