2024-10-02 22:36:23 发布
网友
当我尝试使用以下代码添加新客户时:
new_customer = shopify.Customer() new_customer.first_name = "andres" new_customer.last_name = "cepeda" success = new_customer.save()
而且很管用。但是当我试图添加其他字段如地址或公司时
没用。在
OP的代码有两个问题,上面的答案都没有解决这两个问题,所以我将添加我的答案。在
第一期: 要将地址添加到新的客户,必须使用“addresses”属性,而不是“address”。他在结尾处漏掉了“es”(复数)。这是因为addresses属性是一个列表。即使您只想添加1个地址,也必须将其包含在列表括号中,如下所示。在
new_customer = shopify.Customer() new_customer.first_name = "andres" new_customer.last_name = "cepeda" new_customer.addresses = [{"address1": "123 Oak st", "city": "Ottawa", "phone": "9876543210", "company": "Apple"}] new_customer.default_address = {"address1": "123 Oak st", "city": "Ottawa", "phone": "9876543210", "company": "Apple"} new_customer.save()
您可以直接使用字典来设置默认地址。这是因为这个字段总是只有1个地址,所以不需要使用列表格式(如果您尝试的话会出错)。在
第二期: 第二个问题是OP试图直接在Customer对象(newüCustomer)上设置“company”字段。公司字段是地址的一部分,而不是客户。如我上面的例子所示,将company作为地址字段之一,它将起作用。在
参考文档:https://help.shopify.com/en/api/reference/customers/customer#what-you-can-do-with-customer
尝试:
address = { address1: 'Some address', city: 'Ottawa', province: 'ON', zip: 'K1P 0C2' } new.customer.addresses = [address]
试试这个。在
custo = shopify.Customer() custo.first_name = "andres" custo.last_name = "cepeda" custo.addresses = [{"address1": "123 Oak st", "city": "Ottawa", "phone": "9876543210"}] custo.save()
希望这有帮助。在
OP的代码有两个问题,上面的答案都没有解决这两个问题,所以我将添加我的答案。在
第一期: 要将地址添加到新的客户,必须使用“addresses”属性,而不是“address”。他在结尾处漏掉了“es”(复数)。这是因为addresses属性是一个列表。即使您只想添加1个地址,也必须将其包含在列表括号中,如下所示。在
您可以直接使用字典来设置默认地址。这是因为这个字段总是只有1个地址,所以不需要使用列表格式(如果您尝试的话会出错)。在
第二期: 第二个问题是OP试图直接在Customer对象(newüCustomer)上设置“company”字段。公司字段是地址的一部分,而不是客户。如我上面的例子所示,将company作为地址字段之一,它将起作用。在
参考文档:https://help.shopify.com/en/api/reference/customers/customer#what-you-can-do-with-customer
尝试:
试试这个。在
希望这有帮助。在
相关问题 更多 >
编程相关推荐