【问题标题】:Shopify get Customer MetafieldsShopify 获取客户元字段
【发布时间】:2018-03-01 18:55:40
【问题描述】:

更新:我有这个私有 API 请求来获取向 shopify 管理员发送请求的客户元字段数据。

const {getAccessToken} = require('./auth')
    const request = require('request')
    const {shopFromPermanentDomain} = require('./helpers')

const getCustomerMetafields = ({accessToken, id, shop}) => new Promise((resolve, reject) => {
  request({
    url:`https://${shop}.myshopify.com/admin/customers/${id}/metafields.json',
    headers: {
      'X-Shopify-Access-Token': accessToken
    }
  }, (error, response, body) => {
    const {errors} = JSON.parse(body)

    if (response.statusCode === 200) {
      const { metafields } = JSON.parse(body)
      resolve(metafields)
    }

    reject({error: errors, status: 500})
  })
})

const getCustomerMetafieldsResponse = (req, res) => {
  const {id, permanentDomain} = req.body

  if (id && permanentDomain) {
    const shop = shopFromPermanentDomain(permanentDomain)

    getAccessToken({shop})
      .then(accessToken => getCustomerMetafields({accessToken, id, shop})
        .then(meta => res.json({
          meta,
          status: 200
        }))
      )
      .catch(({error, status}) => res.json({error, status}))
  } else {
    res.json({error: 'Missing params', status: 500})
  }
}

module.exports = getCustomerMetafieldsResponse

我使用以下请求从前端向我的 API 发出此请求。

const getCustomerMeta = ({
   id,
   permanentDomain
}) => new Promise((resolve, reject) => {
  post({
    params: { email, permanentDomain, task: 'get-customer-meta' },
    then: ({ error, id, state, status }) => {
      if (status === 200) {
        resolve({ id, state })
      }

      reject(error)
    },
    url: '/apps/spoke'
  })
})
    getCustomerMeta({
       id, // 98303739294 (Customer ID)
       permanentDomain // "store.myshopify.com"
    })

发出此请求时,我收到以下请求错误:

 500 (Internal Server Error)

VM706781:8 Uncaught SyntaxError: Unexpected token < in JSON at position 7
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.l.onload

然后我想获取客户元字段数据,以便我可以使用收集的数据填充前端。

谢谢!

【问题讨论】:

    标签: shopify shopify-app


    【解决方案1】:

    您不能在不使用应用代理的情况下从前端调用后端,即 /admin。在前端,您编写的代码将使用客户 ID 对您的应用程序进行代理 XHR 调用。使用该 ID,您将获得 Customer 资源的元字段,而不是您的示例中的 Shop。使用客户的 Metafields 资源,您可以查找您感兴趣的资源,并将它们绘制在前端。

    或者只使用 Liquid 为登录的客户呈现元字段,因为这样更简单。

    【讨论】:

    • 谢谢,这个脚本从一个私有应用程序运行,并从后端触发事件到前端,反之亦然。我想知道是否可以通过 API 请求获取客户的元数据,如果可以,它的端点是什么?
    【解决方案2】:

    自从您在 2018 年提出这个问题以来,api 可能已经发生了巨大变化,但现在您可以通过客户或元字段端点为客户获取元字段。

    例如

    /admin/api/{api-version-number}/customers/{shopify-customer-id}/metafields.json
    

    指向元字段端点的方向是:

    /admin/api/{api-version-number}/metafields.json?metafield[owner_id]={shopify-customer-id}&metafield[owner_resource]=customers
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多