0

So practically, i'm importing data over API and saving it like this :

(not all the code but only the relevant)

$addressarray
    ->setCustomAttribute('exact_address_id', $contactaddress['ID'])
    ->setCustomerId($customerrep->getId())
    ->setFirstname($contact['firstname'])
    ->setLastname($contact['lastname'])
    ->setCountryId($contactaddress['Country'])
    ->setRegionId(0)
    ->setPostcode($contactaddress['Postcode'])
    ->setCity($contactaddress['City'])
    ->setTelephone($contact['account']['Phone'])
    ->setFax($contact['account']['Fax'])
    ->setCompany($contactaddress['AccountName'])
    ->setStreet(array($contactaddressstreet))
    ->setVatId($contact['account']['VATNumber']);

    if ($contact['account']['AddressLine1'] == $contactaddress['AddressLine1']) {
    $addressarray->setIsDefaultBilling(true);
    $addressarray->setIsDefaultShipping(true);
    } else {
    $addressarray->setIsDefaultBilling(false);
    $addressarray->setIsDefaultShipping(false);
    }
    try {
    $address->save($addressarray);

And everything seems to work fine , everything is filled in and in the database addresses are created.

BUT

Even though my database table customer_address_entity says 'BE' (which is id for Belgium) under country_id the field itself is empty.

Even more so when i save the customer myself with the address nothing really changes. Its still BE.

address example

Anyone any idea?

Github : Github magento 2 report

1 Answer 1

0

Add below code after $address->save and check

$address->setCountryId('BR');
$address->getResource()->saveAttribute($address, 'country_id'); 
5
  • Which address repository/interface/model are you using? Because i have ->save in addressRepository and i have setcountryID in addressinterface and get resource in address model. But you are using the same variable for all 3? Sep 22, 2017 at 20:48
  • So you would set the country ID after the save? Sep 22, 2017 at 20:51
  • yes, you should add this two lines after save() function
    – Amit Bera
    Sep 23, 2017 at 7:19
  • those don't work because $addres komes from $address = $this->_addressRepository; --> Magento\Customer\Api\AddressRepositoryInterface; and that class does not have ->getResource() function. Sep 23, 2017 at 22:03
  • So from which class do you advice me to extend from? Sep 24, 2017 at 16:07

Your Answer

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

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