Logout user after Woocommerce Checkout

2019-05-30 09:40发布

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.

2条回答
我只想做你的唯一
2楼-- · 2019-05-30 09:56

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.

查看更多
别忘想泡老子
3楼-- · 2019-05-30 10:14

You can use logout function after checkout order

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

function custom_process_order() {   
wp_logout();
}
查看更多
登录 后发表回答