我是WooCommerce REST API的新手。我想将产品添加到购物车并列出购物车的所有产品。我已将Co-Cart插件添加到服务器中,并且正在使用Co-Cart API来实现购物车相关功能。
我面临的问题是,我无法在React Native应用程序中查看购物车产品。这是我现在正在使用的API:
1.将产品添加到购物车:
方法:开机自检
网址: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”:{“小计”:[],“总计”:[]},“ line_subtotal”:50000,“ line_subtotal_tax”:0,“ line_total”:50000,“ line_tax”:0}
从应用程序将产品添加到购物车时,获得状态码200的正确响应。
2.查看购物车内容
方法:GET
网址: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,数据是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
})
它解决了我的问题。希望能帮助到你!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句