/** * Check customer scope, email and confirmation key before saving * * @param \Magento\Framework\Object $customer * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function _beforeSave(\Magento\Framework\Object $customer) { /** @var \Magento\Customer\Model\Customer $customer */ parent::_beforeSave($customer); if (!$customer->getEmail()) { throw new ValidatorException(__('Please enter a customer email.')); } $adapter = $this->_getWriteAdapter(); $bind = ['email' => $customer->getEmail()]; $select = $adapter->select()->from($this->getEntityTable(), [$this->getEntityIdField()])->where('email = :email'); if ($customer->getSharingConfig()->isWebsiteScope()) { $bind['website_id'] = (int) $customer->getWebsiteId(); $select->where('website_id = :website_id'); } if ($customer->getId()) { $bind['entity_id'] = (int) $customer->getId(); $select->where('entity_id != :entity_id'); } $result = $adapter->fetchOne($select, $bind); if ($result) { throw new AlreadyExistsException(__('A customer with the same email already exists in an associated website.')); } // set confirmation key logic if ($customer->getForceConfirmed()) { $customer->setConfirmation(null); } elseif (!$customer->getId() && $customer->isConfirmationRequired()) { $customer->setConfirmation($customer->getRandomConfirmationKey()); } // remove customer confirmation key from database, if empty if (!$customer->getConfirmation()) { $customer->setConfirmation(null); } $this->_validate($customer); return $this; }
/** * Check customer address before saving * * @param \Magento\Framework\DataObject $address * @return $this */ protected function _beforeSave(\Magento\Framework\DataObject $address) { parent::_beforeSave($address); $this->_validate($address); return $this; }