1

How to point all customer's addresses to one specific address? When a new customer is created, the address ID is assigned to the customer's shipping and billing, but it seems that the address' parent_id is set to the latest created customer. In other words I want to change the 1-to-1 --> 1-to-many in address-customer relationship with minimum impact on everything else.

I've done a trace in \Magento\Customer\Model\Address::setCustomerId($id) by

$e = new \Exception();
var_dump(explode("\n", $e->getTraceAsString()));die;

and got (first lines)

array(57) {
  [0]=>
  string(131) "#0 /var/www/magento/magento2/sandbox/vendor/magento/framework/DataObject.php(207): Magento\Customer\Model\Address->setCustomerId(5)"
  [1]=>
  string(158) "#1 /var/www/magento/magento2/sandbox/vendor/magento/module-customer/Model/Address.php(150): Magento\Framework\DataObject->setDataUsingMethod('customer_id', 5)"
  [2]=>
  string(203) "#2 /var/www/magento/magento2/sandbox/vendor/magento/module-customer/Model/ResourceModel/AddressRepository.php(115): Magento\Customer\Model\Address->updateData(Object(Magento\Customer\Model\Data\Address))"
  [3]=>
  string(222) "#3 /var/www/magento/magento2/sandbox/vendor/magento/module-customer/Model/ResourceModel/CustomerRepository.php(212): Magento\Customer\Model\ResourceModel\AddressRepository->save(Object(Magento\Customer\Model\Data\Address))"
  [4]=>
  string(202) "#4 /var/www/magento/magento2/sandbox/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Customer\Model\ResourceModel\CustomerRepository->save(Object(Magento\Customer\Model\Data\Customer), NULL)"

where would be the appropriate place, should I skip the setCustomer() or will it affect the address integrity?

Note:
I've also had to override \Sqli\User\Model\Quote\QuoteAddressValidator::validate($addressData) to skip the verification of if a cusomter's addressId references an address that have that customer in it's parent_id.

2
  • 1
    I tried to change some things, please need advice in the way of posting questions
    – medmek
    May 15, 2017 at 8:52
  • I had to override setCustomer() in \Magento\Customer\Model\Address, and getAddressesCollection() in \Magento\Customer\Model\Customer in order to get that specific address instead of the customer's own.
    – medmek
    May 23, 2017 at 15:49

1 Answer 1

0

in customer Model, I had to override here, from the source of the problem:

public function getAddressesCollection()
{
    if ($this->_addressesCollection === null) {
        $this->_addressesCollection = $this->getAddressCollection()->setCustomerFilter(
            $this->getParentCustomer()
        )->addAttributeToSelect(
            '*'
        );
    }
    return $this->_addressesCollection;
}

then I had to override address Model's setCustomer with something like

public function setCustomer(Customer $customer)
{
    if($customer->getParentCustomer() !== null){
        $this->_customer = $customer->getParentCustomer();
        $this->setCustomerId($this->_customer->getId());
        return $this;
    }
    return parent::setCustomer($customer);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.