Logout user after Woocommerce Checkout

2019-05-30 09:51发布

问题:

using Woocommerce, is it possible to Force a user to be Logged out directly after Checkout (IMPORTANT: I need to have 'Allow customers to create an account during checkout' ticked.)

I ask because by default a user purchases an online course and they get direct access to it despite not being verified by an Admin user.

回答1:

The following will logout customer after checkout redirecting customer to shop page:

// Logourt after checkout and redirect to shop
add_action( 'template_redirect', 'order_received_logout_redirect' );
function order_received_logout_redirect() {
    // Only on "Order received" page
    if( is_wc_endpoint_url('order-received') ) {
        wp_logout(); // Logout

        // Shop redirection url
        $redirect_url = get_permalink( get_option('woocommerce_shop_page_id') );

        wp_redirect($redirect_url); // Redirect to shop

        exit(); // Always exit
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

Note: When a customer is logged out, it can't access anymore the order summary (Order received) after checkout, so a redirection is required.



回答2:

You can use logout function after checkout order

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);

function custom_process_order() {   
wp_logout();
}