如何在Shopify API中添加客户?

时间:2017-06-29 14:28:03

标签: python shopify

当我尝试使用以下代码添加新客户时:

new_customer = shopify.Customer()
new_customer.first_name = "andres"
new_customer.last_name = "cepeda"
success = new_customer.save()

它有效。但是,当我尝试添加其他字段,如地址或公司时,

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()

它不起作用。

3 个答案:

答案 0 :(得分:0)

试试这个。

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

希望这有帮助。

答案 1 :(得分:0)

OP的代码有两个问题,上面的答案都没有在此同时解决两个问题,因此我将添加我的答案。

第一个问题: 为了将地址添加到new_customer中,您必须使用“ addresses”属性,而不是“ address”。他在结尾处缺少“ es”(复数)。这是因为地址属性是一个列表。即使您只想添加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

答案 2 :(得分:-1)

尝试:

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

new.customer.addresses = [address]
相关问题