moein Posted October 29, 2019 Share Posted October 29, 2019 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 pageOtherwise 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'); Quote Link to comment Share on other sites More sharing options...
gw1500se Posted October 29, 2019 Share Posted October 29, 2019 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. 1 Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 30, 2019 Share Posted October 30, 2019 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(). 1 Quote Link to comment Share on other sites More sharing options...
moein Posted October 30, 2019 Author Share Posted October 30, 2019 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 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 Quote Link to comment Share on other sites More sharing options...
moein Posted October 30, 2019 Author Share Posted October 30, 2019 for security issue, use this $location = $_SERVER['HTTP_REFERER']; wp_safe_redirect($location); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.