I am using <preference>
to override CustomerRepository, but
Magento\Customer\Model\ResourceModel\CustomerRepository
contains private functions and it throws me error
Fatal error: Call to private method Magento\Customer\Model\ResourceModel\CustomerRepository::populateCustomerModelWithSecureData() from context 'Custom\Mod\Model\ResourceModel\CustomerRepository' in /var/www/html/WC/M2/store/app/code/Custom/Mod/Model/ResourceModel/CustomerRepository.php on line 60
I want to completely override the save()
function, and add many more functions. The private functions doesn't allow me to do so. It shouldn't require me to define the private functions again due to strict versional maintenance. Any solution for this?
The custom di.xml
has this
<preference for="Magento\Customer\Model\ResourceModel\CustomerRepository"
type="Custom\Mod\Model\ResourceModel\CustomerRepository" />
The customized CustomerRepository is
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Custom\Mod\Model\ResourceModel;
/**
* Customer repository.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CustomerRepository extends \Magento\Customer\Model\ResourceModel\CustomerRepository
{
/**
* {@inheritdoc}
*/
public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null) {
$prevCustomerData = null;
$prevCustomerDataArr = null;
if ($customer->getId()) {
$prevCustomerData = $this->getById($customer->getId());
$prevCustomerDataArr = $prevCustomerData->__toArray();
}
/** @var $customer \Magento\Customer\Model\Data\Customer */
$customerArr = $customer->__toArray();
$customer = $this->imageProcessor->save(
$customer, \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $prevCustomerData
);
$origAddresses = $customer->getAddresses();
$customer->setAddresses([]);
$customerData = $this->extensibleDataObjectConverter->toNestedArray(
$customer, [], \Magento\Customer\Api\Data\CustomerInterface::class
);
$customer->setAddresses($origAddresses);
$customerModel = $this->customerFactory->create(['data' => $customerData]);
$storeId = $customerModel->getStoreId();
if ($storeId === null) {
$customerModel->setStoreId($this->storeManager->getStore()->getId());
}
$customerModel->setId($customer->getId());
// Need to use attribute set or future updates can cause data loss
if (!$customerModel->getAttributeSetId()) {
$customerModel->setAttributeSetId(
\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER
);
}
// Populate model with secure data
$this->populateCustomerModelWithSecureData($customer, $passwordHash, $customerModel);
// If customer email was changed, reset RpToken info
if ($prevCustomerData && $prevCustomerData->getEmail() !== $customerModel->getEmail()
) {
$customerModel->setRpToken(null);
$customerModel->setRpTokenCreatedAt(null);
}
$this->setDefaultBilling($customerArr, $prevCustomerDataArr, $customerModel);
$this->setDefaultShipping($customerArr, $prevCustomerDataArr, $customerModel);
$bo=0;
if(isset($customerArr['custom_attributes']['back_office']['value'])){
if($customerArr['custom_attributes']['back_office']['value']==1)
$bo=1;
}
if($customerArr['custom_attributes']['customer_type']['value']=='abal'||$bo==1){
if($customerArr['custom_attributes']['customer_type']['value']=='abal'){
$customerModel->setGroupId(4);
}
$customerModel->save();
$this->customerRegistry->push($customerModel);
$customerId = $customerModel->getId();
$this->updateAddresses($customer, $customerId);
if($bo=1){
$this->updateft($customer);
}
if(isset($customerArr['id'])&&$customerArr['custom_attributes']['customer_type']['value']=='abal'){
$this->insertcustdetails($customer,$customerId);
}
$savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
$this->eventManager->dispatch(
'customer_save_after_data_object', ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer]
);
return $savedCustomer;
}
else{
$resp = NULL;
if (!isset($customerArr['id'])) {
$customerArr = $customer->__toArray();
$resp = $this->curlCall($customerArr);
if ($resp == "registration_successfull") {
$customerModel->save();
$this->customerRegistry->push($customerModel);
$customerId = $customerModel->getId();
$this->updateAddresses($customer, $customerId);
$savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
$this->eventManager->dispatch(
'customer_save_after_data_object', ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer]
);
return $savedCustomer;
} else {
try{
$number = 5/0;
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer.'));
}
}
}
else{
$customerModel->save();
$this->customerRegistry->push($customerModel);
$customerId = $customerModel->getId();
$this->updateft($customer);
$this->updateaddress($customer);
$this->updateAddresses($customer, $customerId);
$savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
$this->eventManager->dispatch(
'customer_save_after_data_object', ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer]
);
return $savedCustomer;
}
}
}
}