Jump to content

How can I allow an empty address field?


safius

Recommended Posts

I am trying to create a temporary default address manually and giving it certain specific property values (specific city, country, etc). I expect customers to edit it eventually during checkout with a non-empty address, which should work out given how PS handles address editing. (It deletes the previous address and creates a new one which is validated from scratch.)

The code below seems to have no effect whatsoever. No new address can be found in the back-office.

$address = new Address();
$address->firstname = $this->context->customer->firstname;
$address->lastname = $this->context->customer->lastname;
$address->address1 = "";
$address->postcode = <postcode>;
$address->city = <city>;
$address->country = <country>;
$address->state = <state>;
$address->phone = $phone;
$address->phone_mobile = $phone;
$address->alias = "My address";
$address->save();
Edited by safius (see edit history)
Link to comment
Share on other sites

If you look at the Address class the following are required fields, and are validated:

 

id_country    integer

alias         isGenericName

lastname      isName

firstname     isName

address1      isAddress

city          isCityName

 

I'm guessing you're getting a failed return code from the save, 1 or 0 depending on whether successful.  First thing to check is that you are setting id_country with the country id, and not the country name as it appears in your code above?

Link to comment
Share on other sites

Thanks for pointing that out, I had already worked around my issue but I forgot to come by and edit the question (BTW, yes, I replaced country with id_country and state with id_state). With that said, I couldn't get PS to do exactly what I intended. Instead, I created the address with "-" as the address1 field, which I replace with an empty string in the AddressFormat::getFormattedAddressFieldsValues method so it can be delivered to the calling controller as if it was empty.

It works, but if anyone can figure out a proper solution, I'm still curious to know.

Edited by safius (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...