2

I've added a custom attribute to customer address entity which is a backoffice ID. I've synced addresses from clients backoffice. If order maintain a continual sync to check whether address exists I need to load customer address entity by custom attribute. I can't get a clean working solution. Currently use a collection to loop through all addresses stored in magento, match on attribute value and return address entity id. But I'm aware this will use more and more resource as addresses are added. Does anyone have a suggestion?

2 Answers 2

3

Got a solution. I will explain my journey so hopefully helps others.

Essentially I keep reading that using factory methods are not the recommended approach. Trouble is I can't work out how to use interfaces with customisations.

So I read stuff like this Update customer address using address id in magento 2

/**
 * @var \Magento\Customer\Api\AddressRepositoryInterface
 */
protected $addressRepository;

/**
 * Sync constructor.
 * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
 */
public function __construct(
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
) {
    $this->addressRepository = $addressRepository;
}

public function changeAddress($addressId)
{
    /** @var \Magento\Customer\Api\Data\AddressInterface $address */
    $address = $this->addressRepository->getById($addressId);
    // whatever
    $this->addressRepository->save($address);
}

No use here.

So then I give up and think I will try and load via AddressFactory

public function __construct(
    \Magento\Customer\Model\AddressFactory $addressFactory
) {
    $this->addressFactory = $addressFactory;
}

public function changeAddress($addressId)
{
    $address = $this->addressFactory->create()->load('backoffice_id', $addressId);
    // whatever
}

Doesn't work. Because it's a custom attribute? Not sure.

public function __construct(
    \Magento\Customer\Model\AddressFactory $addressFactory
) {
    $this->addressFactory = $addressFactory;
}

// or ->addFieldToFilter('backoffice_id', array('eq' => $addressId))
public function changeAddress($addressId)
{
    $address = $this->addressFactory->create()
        ->getCollection()
        ->addFieldToFilter('backoffice_id', $addressId)
        ->getFirstItem();
    // do stuff
}

This works.

Although products use addAttributeToFilter because they are EAV e.g. >addAttributeToFilter('status', array('eq' => 1)) so that would suggest Customer Address is not EAV. But it is...

In the db you have the following tables

customer_address_entity
customer_address_entity_datetime
customer_address_entity_decimal
customer_address_entity_int
customer_address_entity_text
customer_address_entity_varchar

So I'm confused but I have the answer.

0

Can be done same like with attributes for products I think:

$this->addAttributeToSelect(
    'your_custom_id'
);

Useful links: Magento 2 - Custom Customer attribute in order create select customer grid

for conditional use (examples for products but they can be used for customers): https://fishpig.co.uk/magento/tutorials/addattributetofilter/

1
  • In the end works with addFieldToFilter even though it's EAV May 13, 2019 at 21:28

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.