• Resolved qadriuvesh76

    (@qadriuvesh76)


    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,

    1. Add products to the cart.
    2. Proceed to the cart.
    3. Attempt to go to the checkout.
    4. You will be redirected to the ‘My Account’ page.
    5. 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;
        }
    }

    • This topic was modified 1 year, 2 months ago by qadriuvesh76.

    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.