How to logout woocommerce user from api?

Member

by dedrick , in category: PHP CMS , 5 months ago

How to logout woocommerce user from api?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 5 months ago

@dedrick 

To log out a user from WooCommerce using the API, you can make a POST request to the /wp/v2/logout endpoint with the user's authentication token included in the headers. Here's an example code snippet in PHP:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$api_url = 'https://example.com/wp-json/wp/v2/logout';

$headers = array(
    'Authorization' => 'Bearer YOUR_AUTH_TOKEN',
);

$response = wp_remote_post( $api_url, array(
    'headers'   => $headers,
    'method'    => 'POST',
    'data_format' => 'body'
) );

$body = wp_remote_retrieve_body( $response );
$status_code = wp_remote_retrieve_response_code( $response );

if ( $status_code === 200 ) {
    echo 'User logged out successfully.';
} else {
    echo 'Error logging out user.';
}


Make sure to replace YOUR_AUTH_TOKEN with the actual authentication token of the user you want to log out. This code will send a POST request to the WooCommerce API to log out the user, and it will return a success message if the user is successfully logged out.