Create default billing/shipping address
-
03-10-2020 - |
题
I need to create default billing address for new user. To create billing/shipping address I'm using the next code:
protected function createAddress($isDefaultBilling = false, $isDefaultShipping = false) {
$address = $this->addressFactory->create()
->setCustomerId($this->customerObject->getId())
->setIsDefaultBilling($isDefaultBilling)
->setIsDefaultShipping($isDefaultShipping)
->setCity('n/a')
->setCountryId('UA')
->setCompany('n/a')
->setFax('n/a')
->setFirstname($this->customerObject->getFirstname())
->setLastname($this->customerObject->getLastname())
->setPostcode('n/a')
->setStreet(['n/a'])
->setTelephone('n/a');
return $this->addressRepository->save($address);
// die('testing'); // wut???
// return $address;
}
$this->addressFactory
is an instance of \Magento\Customer\Api\Data\AddressInterfaceFactory
and $this->addressRepository
is \Magento\Customer\Api\CustomerRepositoryInterface
The code looks properly, and in var/debug/db.log I see the query:
## 2016-10-14 10:52:27
## 2909 ## QUERY
SQL: INSERT INTO `mg_customer_address_entity` (`parent_id`, `created_at`, `updated_at`, `city`, `company`, `country_id`, `fax`, `firstname`, `lastname`, `postcode`, `street`, `telephone`) VALUES (?, '2016-10-14 10:52:27', '2016-10-14 10:52:27', ?, ?, ?, ?, ?, ?, ?, ?, ?)
BIND: array (
0 => 12,
1 => 'n/a',
2 => 'n/a',
3 => 'UA',
4 => 'n/a',
5 => 'fname',
6 => 'lname',
7 => 'n/a',
8 => 'n/a',
9 => 'n/a',
)
AFF: 1
TIME: 0.0015
BUT there is NO data in database!
For debug, I saw in /home/serf/www/magento/lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php, and in function save
the call $this->isModified($object)
returns false.
I tried to use \Magento\Customer\Model\AddressFactory:
$model = $this->addressModelFactory->create();
$model->setDataChanges(true);
$address = $model->getDataModel()
->.....
But the result was not changed.
And, finally, I tried $model->getResource()->save($model)
. And got an error like "City is required field".
I tried more ways, but each time the behaviour is different: it may assign new ID to billing/shipping (but the data was not saved in database!), or die with message like "No such entity with addressId = 122", when I try to edit the new billing.
In the last I've uncommented the string die('testing');
, and the address was created. Even if I don't change the billing after creating, it appears in database only if die(..)
is uncommented.
So, what am I doing wrong?
UPD. How am I using the code:
$this->defaultBilling = $this->createAddress(true, false);
$this->customerObject->setDefaultBilling($this->defaultBilling->getId());
$this->customerRepository->save($this->customerObject);
where $this->customerObject
is instance of Magento\Customer\Api\Data\CustomerInterface
, and $this->customerRepository
-- Magento\Customer\Api\CustomerRepositoryInterface
解决方案
Saving customer is not required. On string $this->customerRepository->save($this->customerObject);
the transaction (where billing is saving to database) was rolled back.