WooCommerce 通过联合购物车插件添加到购物车产品

WooCommerce Add to cart products via co-cart plugin

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

我面临的问题是,我无法在我的 React Native 应用程序中查看购物车产品。这是我现在正在使用的 APIs:

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

方法:POST

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

参数:{ "product_id": "9168", "quantity": "1" }

响应:{ "key": "af086cdab7954f11a518e3af68XXXXX", "product_id": 111, "variation_id": 0, "variation": [], "quantity": 1, "data":{}, "data_hash": "b5c1d5ca8bae6d4896cf1807cdfXXXX", "line_tax_data":{ "subtotal": [], "total": [] }, "line_subtotal": 50000, "line_subtotal_tax": 0, "line_total": 50000, "line_tax": 0 }

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

2。查看购物车内容

方法:获取

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

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

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

请让我知道哪里出错了。我只使用 Co-Cart 插件 API。

**

EDIT

**

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,数据是 JSON 字符串格式的原始数据,方法是 GET 或 POST。

我检查了文档,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.

这是我自己问题的答案。

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

问题是它没有将产品添加到我的 logged-in 用户的购物车,这就是为什么它没有为该用户获取所有产品,因为产品没有添加到用户的购物车。

为什么它没有将我的购物车与我当前的用户同步的问题是针对该特定用户的 cookie session。我需要在登录后的每个 API 调用 header 中设置该用户的 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
            })

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