Jump to content

redirected to the "checkout" page


moein

Recommended Posts

Hello

I use this code to display popup login when user to clicked on a "proceed to checkout" button in the cart page (woocomerce)

<?php
function woocommerce_button_proceed_to_checkout() {
       $checkout_url = WC()->cart->get_checkout_url();
       ?>
       <div <?php echo mf_get_option( 'login_reg_popup' ) ? 'data-toggle="modal" data-target="#login-modal"' : null; ?>>
       <a class="checkout-button button alt wc-forward" href="<?php echo ! mf_get_option( 'login_reg_popup' ) ? wc_get_page_permalink( 'myaccount' ) : '#'; ?>" class="checkout-button button alt wc-forward"><?php _e( 'اقدام به پرداخت', 'woocommerce' ); ?></a>
       <?php
}

 

But after login, I want the user to be redirected to the "checkout" page if it is on the cart page
Otherwise redirect to your current page wherever you are logged in

I found the following code but I couldn't put it in the code above
Thank you for helping me with your kindness

<?php

//redirect to your "current" page
wp_redirect( $_SERVER["HTTP_REFERER"] );


// redirect to the "checkout" page
function wpse_131562_redirect() {
    if (! is_user_logged_in()
        && (is_woocommerce() || is_cart())
    ) {
        // feel free to customize the following line to suit your needs
        wp_redirect(site_url('checkout/'));
        exit;
    }
}
add_action('template_redirect', 'wpse_131562_redirect');

 

Link to comment
Share on other sites

You can't just copy code and expect it to work without understanding what it does. That you are using 'wp_redirect' indicates you are writing a WordPress application. What error are you getting? Also your coding is vary hard to read and not good programming practice. You should modularlize your PHP code. A process section and presentation section. Don't embed PHP into HTML like that, build the output string in  then echo it.

  • Thanks 1
Link to comment
Share on other sites

The two chunks of code you've posted are completely unrelated. I don't know what mf_get_option('login_reg_popup') should or could return, but if it's falsey it'll set the action parameter of your checkout button link to the login page. If it's truthy it'll set it to ... nothing, really.

Your second chunk of code redirects everything to $_SERVER['HTTP_REFERRER'] (which I'm pretty sure is a massive security issue, not to mention the fact that it applies to everything). Then, later - assuming the code gets there after the ill-advised redirect - it sets up an action handler for template_redirect that again calls the wpse_131562_redirect() function on every page load.

Assuming mf_get_option('login_reg_popup') returns false if the user is not logged in, I think what you want to do is replace the '#' in your first code chunk with wc_get_checkout_url().

  • Thanks 1
Link to comment
Share on other sites

thanks for the reply

I have no knowledge of programming, and with a great deal of effort I obtained these codes

Yes it's about WordPress

My theme uses a login pop-up Modal [this cod: mf_get_option('login_reg_popup')] , I use that pop-up instead of a redirect to the login page

 

x06666_.gif

The following code is for displaying popup login templates when you click the checkout button

<?php
function woocommerce_button_proceed_to_checkout() {
       $checkout_url = WC()->cart->get_checkout_url();
       ?>
       <div <?php echo mf_get_option( 'login_reg_popup' ) ? 'data-toggle="modal" data-target="#login-modal"' : null; ?>>
       <a class="checkout-button button alt wc-forward" href="<?php echo ! mf_get_option( 'login_reg_popup' ) ? wc_get_page_permalink( 'myaccount' ) : '#'; ?>" class="checkout-button button alt wc-forward"><?php _e( 'اقدام به پرداخت', 'woocommerce' ); ?></a>
       <?php
}

 

I would like the following terms to be done by user clicking on that checkout button
1. If not logged-in and it was in the cart page then display popup and login=> redirect to the checkout page
2. If logged-in and it was in the cart page then after clicking the checkout button=> redirect to the checkout page

3. Except cart page, the user logged in everywhere site => redirect to the current page user

I hope you guide me

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.