6

POST : xyz.com/wp-json/wc/store/cart/add-item I'm Developing a mobile woocommerce application and try to add-item in cart but getting some error please help to fix this problem thanks.

In Body:
  
{
"id": "8924",
"quantity":1,
"variation": [
             {
              "items" :{
              "attribute": "hj",
              "value": "knk"
              }
           }
        
           ]
        }

Error:
{
 "code": "woocommerce_rest_missing_nonce",
 "message": "Missing the X-WC-Store-API-Nonce header. This endpoint requires a valid nonce.",
 "data": {
 "status": 401
        }
}

3 Answers3

2

You need to pass back a nonce as a header, previously X-WC-Store-API-Nonce, now just Nonce.

A nonce is required on all POST requests and all requests to store/checkout. You can get a nonce on every response you get from the server, so if you hit store/cart, you will get a nonce, save that nonce and use it for future requests, but make sure to update it after each response.

N. Seghir
  • 21
  • 2
0

The reason why you are seeing the error is due to API requiring not just any nonce, but a specific one.

Don’t just pass the value return from wp_create_nonce('my-string'), the value has to come from wp_create_nonce('wc_store_api')

you can read more about it here:

https://digitalapps.com/woocommerce-rest-api-cart-endpoint-error/

Seb33300
  • 7,464
  • 2
  • 40
  • 57
user30899
  • 27
  • 6
  • So would this be a value that you generate in `wp_localize_script()` and then include in your Javascript that calls the API? – helgatheviking Dec 17 '22 at 20:28
0

You can disable nonce check by adding this filter in your WordPress functions.php file or in a snippet using code snippets plugin

add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' );
Prince Hamza
  • 1,090
  • 12
  • 21
user1773030
  • 1
  • 1
  • 1