简体   繁体   English

如何在Shopify API中添加客户?

[英]How to add a customer in Shopify API?

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. 没用

Try with this. 试试这个。

custo = shopify.Customer()
      custo.first_name = "andres"
      custo.last_name = "cepeda"
      custo.addresses = [{"address1": "123 Oak st", "city": "Ottawa", "phone": "9876543210"}]
      custo.save()

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. OP的代码有两个问题,上面的答案都没有在这里解决这两个问题,因此我将添加我的答案。

First issue: In order to add the address to the new_customer, you have to use "addresses" property, not "address". 第一个问题:为了将地址添加到new_customer中,您必须使用“地址”属性,而不是“地址”。 He was missing the "es" at the end (plural). 他在结尾处缺少“ es”(复数)。 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. 即使您只想添加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()

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). 这是因为该字段总是只有1个地址,因此不需要使用列表格式(如果尝试,则会出错)。

Second Issue: The second issue is that the OP is trying to set "company" field directly on the Customer object (new_customer). 第二个问题:第二个问题是OP试图直接在Customer对象(new_customer)上设置“ company”字段。 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. 如上面的示例所示,将company包括为地址字段之一,它将起作用。

See docs for reference: https://help.shopify.com/en/api/reference/customers/customer#what-you-can-do-with-customer 请参阅文档以供参考: https : //help.shopify.com/zh-CN/api/reference/customers/customer#what-you-can-do-with-customer

Try: 尝试:

address = { address1: 'Some address', city: 'Ottawa', province: 'ON', zip: 'K1P 0C2' }

new.customer.addresses = [address]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM