/**
  * Handle customer VAT number if needed on collect_totals_before event of quote address
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function dispatch(\Magento\Framework\Event\Observer $observer)
 {
     /** @var \Magento\Quote\Model\Quote\Address $quoteAddress */
     $quoteAddress = $observer->getQuoteAddress();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $quoteAddress->getQuote();
     $customer = $quote->getCustomer();
     $storeId = $customer->getStoreId();
     if ($customer->getCustomAttribute('disable_auto_group_change') && $customer->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) {
         return;
     }
     $customerCountryCode = $quoteAddress->getCountryId();
     $customerVatNumber = $quoteAddress->getVatId();
     $groupId = null;
     if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) {
         $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup($storeId)->getId() : $this->groupManagement->getNotLoggedInGroup()->getId();
     } else {
         // Magento always has to emulate group even if customer uses default billing/shipping address
         $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId);
     }
     if ($groupId) {
         $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
         $quote->setCustomerGroupId($groupId);
         $customer->setGroupId($groupId);
         $quote->setCustomer($customer);
     }
 }
 /**
  * Handle customer VAT number if needed on collect_totals_before event of quote address
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
     $shippingAssignment = $observer->getShippingAssignment();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $observer->getQuote();
     /** @var \Magento\Quote\Model\Quote\Address $address */
     $address = $shippingAssignment->getShipping()->getAddress();
     $customer = $quote->getCustomer();
     $storeId = $customer->getStoreId();
     if ($customer->getDisableAutoGroupChange() || false == $this->vatValidator->isEnabled($address, $storeId)) {
         return;
     }
     $customerCountryCode = $address->getCountryId();
     $customerVatNumber = $address->getVatId();
     $groupId = null;
     if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) {
         $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup($storeId)->getId() : $this->groupManagement->getNotLoggedInGroup()->getId();
     } else {
         // Magento always has to emulate group even if customer uses default billing/shipping address
         $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($address, $storeId), $storeId);
     }
     if ($groupId) {
         $address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
         $quote->setCustomerGroupId($groupId);
         $customer->setGroupId($groupId);
         $quote->setCustomer($customer);
     }
 }
示例#3
0
 /**
  * Address after save event handler
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function afterAddressSave($observer)
 {
     /** @var $customerAddress Address */
     $customerAddress = $observer->getCustomerAddress();
     $customer = $customerAddress->getCustomer();
     if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) {
         return;
     }
     try {
         $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true);
         if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) {
             $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId();
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
                 $customer->setGroupId($defaultGroupId);
                 $customer->save();
             }
         } else {
             $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId());
             $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore());
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) {
                 $customer->setGroupId($newGroupId);
                 $customer->save();
             }
             $customerAddress->setVatValidationResult($result);
         }
     } catch (\Exception $e) {
         $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true);
     }
 }
 /**
  * Address after save event handler
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /** @var $customerAddress Address */
     $customerAddress = $observer->getCustomerAddress();
     $customer = $customerAddress->getCustomer();
     if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) {
         return;
     }
     try {
         $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true);
         if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) {
             $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId();
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
                 $customer->setGroupId($defaultGroupId);
                 $customer->save();
                 $this->customerSession->setCustomerGroupId($defaultGroupId);
             }
         } else {
             $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId());
             $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore());
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) {
                 $customer->setGroupId($newGroupId);
                 $customer->save();
                 $this->customerSession->setCustomerGroupId($newGroupId);
             }
             $customerAddress->setVatValidationResult($result);
             if ($this->appState->getAreaCode() == Area::AREA_FRONTEND) {
                 if ($result->getIsValid()) {
                     $this->addValidMessage($customerAddress, $result);
                 } elseif ($result->getRequestSuccess()) {
                     $this->addInvalidMessage($customerAddress);
                 } else {
                     $this->addErrorMessage($customerAddress);
                 }
             }
         }
     } catch (\Exception $e) {
         $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true);
     }
 }