CollectTotalsObserver.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Observer\Frontend\Quote\Address;
  7. use Magento\Framework\Event\ObserverInterface;
  8. class CollectTotalsObserver implements ObserverInterface
  9. {
  10. /**
  11. * @var \Magento\Customer\Api\AddressRepositoryInterface
  12. */
  13. private $addressRepository;
  14. /**
  15. * @var \Magento\Customer\Model\Session
  16. */
  17. private $customerSession;
  18. /**
  19. * @var \Magento\Customer\Helper\Address
  20. */
  21. protected $customerAddressHelper;
  22. /**
  23. * @var \Magento\Customer\Model\Vat
  24. */
  25. protected $customerVat;
  26. /**
  27. * @var VatValidator
  28. */
  29. protected $vatValidator;
  30. /**
  31. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
  32. */
  33. protected $customerDataFactory;
  34. /**
  35. * Group Management
  36. *
  37. * @var \Magento\Customer\Api\GroupManagementInterface
  38. */
  39. protected $groupManagement;
  40. /**
  41. * Initialize dependencies.
  42. *
  43. * @param \Magento\Customer\Helper\Address $customerAddressHelper
  44. * @param \Magento\Customer\Model\Vat $customerVat
  45. * @param VatValidator $vatValidator
  46. * @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
  47. * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
  48. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  49. * @param \Magento\Customer\Model\Session $customerSession
  50. */
  51. public function __construct(
  52. \Magento\Customer\Helper\Address $customerAddressHelper,
  53. \Magento\Customer\Model\Vat $customerVat,
  54. VatValidator $vatValidator,
  55. \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
  56. \Magento\Customer\Api\GroupManagementInterface $groupManagement,
  57. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
  58. \Magento\Customer\Model\Session $customerSession
  59. ) {
  60. $this->customerVat = $customerVat;
  61. $this->customerAddressHelper = $customerAddressHelper;
  62. $this->vatValidator = $vatValidator;
  63. $this->customerDataFactory = $customerDataFactory;
  64. $this->groupManagement = $groupManagement;
  65. $this->addressRepository = $addressRepository;
  66. $this->customerSession = $customerSession;
  67. }
  68. /**
  69. * Handle customer VAT number if needed on collect_totals_before event of quote address
  70. *
  71. * @param \Magento\Framework\Event\Observer $observer
  72. * @return void
  73. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  74. */
  75. public function execute(\Magento\Framework\Event\Observer $observer)
  76. {
  77. /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
  78. $shippingAssignment = $observer->getShippingAssignment();
  79. /** @var \Magento\Quote\Model\Quote $quote */
  80. $quote = $observer->getQuote();
  81. /** @var \Magento\Quote\Model\Quote\Address $address */
  82. $address = $shippingAssignment->getShipping()->getAddress();
  83. $customer = $quote->getCustomer();
  84. $storeId = $customer->getStoreId();
  85. if ($customer->getDisableAutoGroupChange()
  86. || false == $this->vatValidator->isEnabled($address, $storeId)
  87. ) {
  88. return;
  89. }
  90. $customerCountryCode = $address->getCountryId();
  91. $customerVatNumber = $address->getVatId();
  92. /** try to get data from customer if quote address needed data is empty */
  93. if (empty($customerCountryCode) && empty($customerVatNumber) && $customer->getDefaultShipping()) {
  94. $customerAddress = $this->addressRepository->getById($customer->getDefaultShipping());
  95. $customerCountryCode = $customerAddress->getCountryId();
  96. $customerVatNumber = $customerAddress->getVatId();
  97. }
  98. $groupId = null;
  99. if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) {
  100. $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup(
  101. $storeId
  102. )->getId() : $this->groupManagement->getNotLoggedInGroup()->getId();
  103. } else {
  104. // Magento always has to emulate group even if customer uses default billing/shipping address
  105. $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber(
  106. $customerCountryCode,
  107. $this->vatValidator->validate($address, $storeId),
  108. $storeId
  109. );
  110. }
  111. if ($groupId) {
  112. $address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
  113. $quote->setCustomerGroupId($groupId);
  114. $this->customerSession->setCustomerGroupId($groupId);
  115. $customer->setGroupId($groupId);
  116. $quote->setCustomer($customer);
  117. }
  118. }
  119. }