【问题标题】:WooCommerce Add to cart products via co-cart pluginWooCommerce 通过共同购物车插件添加到购物车产品
【发布时间】:2019-10-02 09:34:45
【问题描述】:

我是 WooCommerce REST API 的新手。我想将产品添加到购物车并列出购物车的所有产品。我已将 Co-Cart 插件添加到我的服务器,并且我正在使用 Co-Cart API 来实现与购物车相关的功能。

我面临的问题是,我无法在我的 react 本机应用程序中查看购物车产品。以下是我现在使用的 API:

1.将产品添加到购物车:

方法:POST

网址:https://www.myhost.com/wp-json/wc/v2/cart/add?token=user_token

参数:{ "product_id": "9168", “数量”:“1” }

回应:{ “关键”:“af086cdab7954f11a518e3af68XXXX”, "product_id": 111, “variation_id”:0, “变化”: [], “数量”:1, “数据”: {}, "data_hash": "b5c1d5ca8bae6d4896cf1807cdfXXXX", “line_tax_data”:{ “小计”:[], “全部的”: [] }, “line_subtotal”:50000, “line_subtotal_tax”:0, “line_total”:50000, “line_tax”:0 }

在将产品从应用程序添加到购物车时获得状态码 200 的正确响应。

2。查看购物车内容

方法:获取

网址:https://www.myhost.com/wp-json/wc/v2/cart?token=user_token

响应:[](空白 JSON 数组)

我不知道使用同一用户添加到购物车的产品在哪里。我不知道它如何管理来自移动应用程序的会话或获取购物车商品所需的额外参数。

请告诉我哪里出错了。我只使用 Co-Cart 插件 API。

**

编辑

**

fetch(url, {
        method: methodName,
        async: true,
        crossDomain: true,
        headers: new Headers({
          "Content-Type": "application/json",
          "cache-control": "no-cache",
        }),
        body:data
      }).then((response) => {
        const statusCode = response.status;
        console.log("fetchData StatusCode : "+statusCode);
        console.log("fetchData response1 : " + JSON.stringify(response));
        console.log("fetchData response2 : " + response);
        return response.json();
      }).then((responseJson) => {
        console.log("fetchData responseJson : "+responseJson);
        var response = JSON.stringify(responseJson);
        console.log("fetchData responseJson stringify : " + response);
        response = JSON.parse(response);
        if (callBack != null) {
          callBack("", response);
        }
      }).catch((error) => {
        console.log("fetchData Api error : "+error);
        if (callBack != null) {
          console.log("fetchData Api error callBack != null 2");
          callBack("Error", "");
        }
      });

url 是要发送的 URL,data 是 JSON 字符串格式的原始数据,方法是 GET 或 POST。

【问题讨论】:

    标签: wordpress react-native woocommerce mobile-application woocommerce-rest-api


    【解决方案1】:

    这是我自己问题的答案。

    如果不事先了解 WooCommerce 及其工作标准,将 WooCommerce 商店集成到移动应用程序中有些困难。

    问题在于它没有将产品添加到我已登录用户的购物车中,这就是为什么它没有为该用户获取所有产品,因为产品没有添加到用户的购物车中。

    它没有将我的购物车与我当前的用户同步的问题是该特定用户的 cookie 会话。登录后,我需要在每个 API 调用标头中设置该用户的 cookie:

    fetch(url1, {
                  method: methodName,
                  async: true,
                  crossDomain: true,
                  headers: new Headers({
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "cookie":"cookie value will goes here"
                  }),
                  body:data1
                })
    

    它刚刚解决了我的问题。希望对您有所帮助!

    【讨论】:

    • 您是如何在 Fetch API 中获取 cookie 的,您能否在此处发布一些示例代码
    • 我也遇到了同样的问题,你是怎么得到用户/客户的完整cookie的?
    【解决方案2】:

    我检查了文档,https://co-cart.github.io/co-cart-docs/#view-cart-contents,但你的没有问题。

    你能检查一下Pretty permalinks in Settings > Permalinks so that the custom endpoints are supported. Default permalinks will not work.

    【讨论】:

    • 嗨@Debabrata Karfa,它将为哪个用户添加产品到购物车?所有 API 都从 Postman 正确响应,我还可以从 postman 中查看购物车内容,但不能从我的应用程序中查看。
    • 正如您提到的All APIs response properly from Postman ,这意味着 WC 和插件工作正常,您需要查看我们的 React Native ... 只是好奇...您是否使用 fetch 或 Super agent 或 Axios是吗?
    • 我正在使用 fetch。奇怪的是,当从邮递员将产品添加到购物车时,它被添加了,我也可以在从邮递员获取购物车内容 API 中看到该产品。但是当我从应用程序将产品添加到购物车并从邮递员检查购物车内容 API 时,从应用程序添加的产品将不存在
    • 你知道吗?
    • 嗨,@Debabrata Karfa,你能告诉我这里有什么问题吗?
    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 2015-11-15
    • 2021-07-26
    • 2021-08-22
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多