Со страницы регистрации клиента я получаю адрес, введенный клиентом.Я хочу сохранить этот адрес дважды в таблице адресов клиентов: один для доставки по умолчанию и один биллинг по умолчанию.Я расширяю контроллер creatPost и пытаюсь изменить функцию extractAddress.
Я сделал это, но не работает, как ожидалось
protected function extractAddress()
{
if (!$this->getRequest()->getPost('create_address')) {
return null;
}
$addressForm = $this->formFactory->create('customer_address', 'customer_register_address');
$allowedAttributes = $addressForm->getAllowedAttributes();
$addressData = [];
$regionDataObject = $this->regionDataFactory->create();
foreach ($allowedAttributes as $attribute) {
$attributeCode = $attribute->getAttributeCode();
$value = $this->getRequest()->getParam($attributeCode);
if ($value === null) {
continue;
}
switch ($attributeCode) {
case 'region_id':
$regionDataObject->setRegionId($value);
break;
case 'region':
$regionDataObject->setRegion($value);
break;
default:
$addressData[$attributeCode] = $value;
}
}
$arr = [];
$arr[] = $addressData;
$arr[] = $addressData;
for($i=0;$i<2;$i++){
$addressDataObject = $this->addressDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$addressDataObject,
$arr[$i],
\Magento\Customer\Api\Data\AddressInterface::class
);
$addressDataObject->setRegion($regionDataObject);
if ($i==0){
$addressDataObject->setIsDefaultBilling(
$this->getRequest()->getParam('default_billing', false)
);
} else {
$addressDataObject->setIsDefaultShipping(
$this->getRequest()->getParam('default_shipping', false)
);
}
}
return $addressDataObject;
}
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$this->session->regenerateId();
try {
$address = $this->extractAddress();
$addresses = $address === null ? [] : [$address];
$customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
$customer->setAddresses($addresses);
}
}