【问题标题】:How to add a customer in Shopify API?如何在 Shopify API 中添加客户?
【发布时间】:2017-06-29 14:28:02
【问题描述】:

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

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

没有用。

【问题讨论】:

  • 请解释“它不起作用”是什么意思。你有例外吗?如果是这样,它是什么?您是否没有例外,并且您的记录根本没有创建?详细说明什么不起作用有助于为读者提供必要的背景信息,让他们知道如何提供帮助。
  • 成功创建客户时返回true或false。
  • 您有可以参考的服务器日志吗?如果没有更多信息,就无法知道您的实际情况。由于名称相同,尝试添加新客户可能会违反唯一约束?目前一切都是猜测。

标签: python shopify


【解决方案1】:

OP 的代码有两个问题,上面的答案都没有解决 两个 问题,所以我将添加我的答案。

第一期: 为了将地址添加到 new_customer,您必须使用“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 试图直接在客户对象 (new_customer) 上设置“公司”字段。公司字段是地址的一部分,而不是直接的客户。如我上面的示例所示,将公司包含为地址字段之一,它将起作用。

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

【讨论】:

    【解决方案2】:

    试试这个。

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

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      试试:

      address = { address1: 'Some address', city: 'Ottawa', province: 'ON', zip: 'K1P 0C2' }
      
      new.customer.addresses = [address]
      

      【讨论】:

      • 请使用edit链接来解释这段代码是如何工作的,不要只给出代码,因为解释更有可能帮助未来的读者。另请参阅How to Answer
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多