wordpress - WooCommerce 通过共同购物车插件添加到购物车产品
问题描述
我是 WooCommerce REST API 的新手。我想将产品添加到购物车并列出购物车的所有产品。我已将 Co-Cart 插件添加到我的服务器,并且我正在使用 Co-Cart API 来实现与购物车相关的功能。
我面临的问题是,我无法在我的反应原生应用程序中查看购物车产品。以下是我现在使用的 API:
1. 将产品添加到购物车:
方法:POST
网址:https ://www.myhost.com/wp-json/wc/v2/cart/add?token=user_token
参数:{“product_id”:“9168”,“数量”:“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.查看购物车内容
方法:获取
网址: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。
解决方案
这是我自己的问题的答案。
在不了解 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
})
它刚刚解决了我的问题。希望能帮助到你!
推荐阅读
- sql - 在sybase中加入多个表
- c - 为什么 atmega32 只收到字符串的最后 16 个字符?
- java - UseConcMarkSweepGC 已被弃用,它的替代品是什么?
- mysql - SQL组合2个没有主键的表
- python - Python matplotlib直方图很慢
- java - MongoDB存储随机数据
- c++ - 如何允许通配符模板参数
- python - 我想用 django 在 htm 文件中导入 python 代码
- android - Android 将数据从 IntentService 发送到绑定的 Activity
- r - 尝试在 R 中实现套索时出现“参数 Y 缺失”?