I want guest checkout to only enable if user click from my-account
-
Here’s the corrected version of your text:
“I created a script that restricts access to the checkout for users who are not logged in. They will be redirected to the ‘My Account’ page. However, I also created a ‘Checkout as a Guest’ button. My logic dictates that only if a user clicks on that button, they should be able to access the checkout.
The flow for this is as follows: if you are not logged in,
- Add products to the cart.
- Proceed to the cart.
- Attempt to go to the checkout.
- You will be redirected to the ‘My Account’ page.
- If you have created an account, there is no problem. However, if you choose to ‘Checkout as a guest,’ you will be redirected to the checkout.
I have successfully created this script. However, there is a problem: after a user successfully checks out, they are redirected to ‘My Account.’ I want to redirect them to the ‘Order Received’ page instead.
There is only Problem. My Following Code is this// Guest Checkout function add_checkout_as_guest_link() { if ( is_user_logged_in() ) { // User is logged in, show nothing. return; } $checkout_url = wc_get_checkout_url(); echo '<div style="text-align: center; display: flex; flex-direction: column; align-items: center; margin:0px 0px 20px 0px;"> <a href="' . esc_url( add_query_arg( 'guest_checkout', '1', $checkout_url ) ) . '" style="border: 1px solid #000; padding: 10px 30px; "> <span style="font-size: 16px; vertical-align: top;">Checkout as Guest</span> </a> </div>'; } add_action( 'woocommerce_before_customer_login_form', 'add_checkout_as_guest_link' ); add_action('template_redirect', 'custom_checkout_redirect'); function custom_checkout_redirect() { // Check if user is not logged in and the "Checkout as Guest" parameter is set. if (!is_user_logged_in() && isset($_GET['guest_checkout']) && $_GET['guest_checkout'] === '1') { // Allow access to the checkout page. return; } // Check if user is not logged in and trying to access the checkout page. if (!is_user_logged_in() && is_checkout()) { // Redirect to the 'my-account' page or any other page you prefer. wp_redirect(get_permalink(wc_get_page_id('myaccount'))); exit; } }
The page I need help with: [log in to see the link]
- The topic ‘I want guest checkout to only enable if user click from my-account’ is closed to new replies.