I’m trying to prevent a function redirecting to the previous page after logging in to execute on my WooCommerce /checkout
page, as it causes an error.
Here is the function (added to the functions.php file of my theme via the Code Snippet plugin):
/* * Add a hidden field to our WooCommerce login form - passing in the refering page URL * Note: the input (hidden) field doesn't actually get created unless the user was directed * to this page from a single product */ function redirect_user_back_to_product() { // check for a referer $referer = wp_get_referer(); // if there was a referer.. if( $referer ) { $post_id = url_to_postid( $referer ); $post_data = get_post( $post_id ); if( $post_data ) { // if the refering page was a single product, let's append a hidden field to redirect the user to if( isset( $post_data->post_type ) && $post_data->post_type == 'product' ) { ?> <input type="hidden" name="redirect-user" value="<?php echo $referer; ?>"> <?php } } } } add_action( 'woocommerce_login_form', 'redirect_user_back_to_product' ); /* * Redirect the user back to the passed in referer page * - Which should be the URL to the last viewed product before logging */ function custom_woocommerce_login_redirect_back_to_product_page( $redirect, $user ) { if( isset( $_POST['redirect-user'] ) ) { $redirect = esc_url( $_POST['redirect-user'] ); } return $redirect; } add_filter( 'woocommerce_login_redirect', 'custom_woocommerce_login_redirect_back_to_product_page' );
When logging in on the WooCommerce /checkout
page, I get this error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function custom_woocommerce_login_redirect_back_to_product_page(), 1 passed in /homepages/8/d123456789/htdocs/wp-includes/class-wp-hook.php on line 294 and exactly 2 expected in /homepages/8/d123456789/htdocs/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()'d code:36 Stack trace: #0 /homepages/8/d123456789/htdocs/wp-includes/class-wp-hook.php(294): custom_woocommerce_login_redirect_back_to_product_page('https://www.web...') #1 /homepages/8/d123456789/htdocs/wp-includes/plugin.php(212): WP_Hook->apply_filters('https://www.web...', Array) #2 /homepages/8/d123456789/htdocs/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php(979): apply_filters('woocommerce_log...', 'https://www.web...', Object(WP_User)) #3 /homepages/8/d123456789/htdocs/wp-includes/class-wp-hook.php(292): WC_Form_Handler::process_login('') #4 /homepages/8/d123456789/htdocs/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Array) #5 /homepages/8/d1234 in /homepages/8/d123456789/htdocs/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()'d code on line 36
I tried adding a if ( is_page('checkout') return;
condition, but it doesn’t work.
Advertisement
Answer
The function is getting called with to few parameters you can resolve the error by adding the amount of parameters that the filter should pass as follows.
add_filter( 'woocommerce_login_redirect', 'custom_woocommerce_login_redirect_back_to_product_page', 10, 2 );
edit:
you can use something like this to compare the post_id
function redirect_user_back_to_product() { // check for a referer $referer = wp_get_referer(); // if there was a referer.. if( $referer ) { $post_id = url_to_postid( $referer ); $checout = wc_get_page_id( 'checkout' ); if($post_id === $checout) return; $post_data = get_post( $post_id ); if( $post_data ) { // if the refering page was a single product, let's append a hidden field to redirect the user to if( isset( $post_data->post_type ) && $post_data->post_type == 'product' ) { ?> <input type="hidden" name="redirect-user" value="<?php echo $referer; ?>"> <?php } } } }