im trying to add a product to cart with Woocommerce API with this url /wp-json/wc/store/v1/cart/add-item
.
i use postman to send request but it returs 403 Forbidden
error:
{
"code": "woocommerce_rest_invalid_nonce",
"message": "کلید سرّی نامعتبر است.",
"data": {
"status": 403
}
}
Also i use swagger plugin in wordpress for API.
how can fix this problem.
2
Answers
you need the nonce key.
you can GET site.com/wp-json/wc/store/v1/cart
which will have the nonce in a header, and pass it back for your future requests.
You need to create a nonce. You can achieve that by making some changes in your
function.php
file which you can find in themes folder.In wp you can achieve like this
and if you are working with CMS or Rest APIs of Woocommerce then you will need an api to create a nonce every time you make a call to add product in the cart which you can do like this
Now, your api will be something like this
wp-json/my-nonce/v1/get-nonce
to use response nonce in frontend for passing in/wp-json/wc/store/cart/add-item