When I try to add a new customer with the following code:
new_customer = shopify.Customer()
new_customer.first_name = "andres"
new_customer.last_name = "cepeda"
success = new_customer.save()
And it works. However when I tried to add another fields such a address or company,
new_customer = shopify.Customer()
new_customer.first_name = "andres"
new_customer.last_name = "cepeda"
new_customer.company = "my company"
new_customer.address = "cll 25 - 27"
success = new_customer.save()
It didn’t work.
3
Answers
Try:
Try with this.
Hope this helps.
There are two issues with the OP’s code and neither of the answers above address both issues here so I’ll add my answer.
First issue:
In order to add the address to the new_customer, you have to use “addresses” property, not “address”. He was missing the “es” at the end (plural). This is because the addresses property is a list. Even if you only want to add 1 address, you must include it in list brackets as shown below.
You can set the default address by using a dictionary directly. This is because this field is always going to be only 1 address so there is no need to use list format (and will error out if you try).
Second Issue:
The second issue is that the OP is trying to set “company” field directly on the Customer object (new_customer). The company field is part of the address, not the customer directly. As shown in my example above, include company as one of the address fields and it will work.
See docs for reference: https://help.shopify.com/en/api/reference/customers/customer#what-you-can-do-with-customer