$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - Redirect to checkout woocommerce failed|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - Redirect to checkout woocommerce failed

matteradmin7PV0评论

Bonsouir, I'm trying to redirect to checkout when a user add to cart a product just from the single product page, to be very specific from this page where everyone can see just the simple product

Now, everythings works just fine when I add this code to functions.php

function my_custom_add_to_cart_redirect( $url ) {
        $url = wc_get_checkout_url();
        return $url;
}

add_filter( 'woocommerce_add_to_cart_redirect','my_custom_add_to_cart_redirect');

But when i modify the code adding the condition to discriminate between the pages that are not the single product page with this code it doesnt work

function my_custom_add_to_cart_redirect( $url ) {   

    if ( is_product() ){
        $url = wc_get_checkout_url(); 
        return $url;
    }
}

add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

I put inside the condition an echo "it works" and prints quite well but the redirect is not working.

Where do you think is the problem, this should works, is just an easy condition but dont work for me, Im using Avada Theme and Woocommerce updated

EDIT : It looks like when I add to cart, the links is generated as

[www.domain]/product/[name-of-product]/?add-to-cart=[PRODUCTID]

The path

[www.domain]/product/[name-of-product]/ 

IS considered for woocommmerce as product but the other one that includes ?add-to-cart=[PRODUCTID] is not considered as product but it should be because it still is a product, is just sending a variable via GET

Do you know how to make recognize it as a product page ?

Bonsouir, I'm trying to redirect to checkout when a user add to cart a product just from the single product page, to be very specific from this page where everyone can see just the simple product

Now, everythings works just fine when I add this code to functions.php

function my_custom_add_to_cart_redirect( $url ) {
        $url = wc_get_checkout_url();
        return $url;
}

add_filter( 'woocommerce_add_to_cart_redirect','my_custom_add_to_cart_redirect');

But when i modify the code adding the condition to discriminate between the pages that are not the single product page with this code it doesnt work

function my_custom_add_to_cart_redirect( $url ) {   

    if ( is_product() ){
        $url = wc_get_checkout_url(); 
        return $url;
    }
}

add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

I put inside the condition an echo "it works" and prints quite well but the redirect is not working.

Where do you think is the problem, this should works, is just an easy condition but dont work for me, Im using Avada Theme and Woocommerce updated

EDIT : It looks like when I add to cart, the links is generated as

[www.domain]/product/[name-of-product]/?add-to-cart=[PRODUCTID]

The path

[www.domain]/product/[name-of-product]/ 

IS considered for woocommmerce as product but the other one that includes ?add-to-cart=[PRODUCTID] is not considered as product but it should be because it still is a product, is just sending a variable via GET

Do you know how to make recognize it as a product page ?

Share Improve this question edited Feb 18, 2018 at 20:02 Johanna Ferreira asked Feb 17, 2018 at 23:43 Johanna FerreiraJohanna Ferreira 693 silver badges8 bronze badges 2
  • What is the value of $url inside the if statement? – sandrodz Commented Feb 18, 2018 at 8:41
  • @sandrodz The url from the checkout page, but that's not the problem, I also try with get_permalink and it doesnt redirect either – Johanna Ferreira Commented Feb 18, 2018 at 15:21
Add a comment  | 

3 Answers 3

Reset to default 1

Well, what it works for me in this case was this code

add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_add_to_cart' );

function redirect_add_to_cart() {       
    if ( isset( $_POST['add-to-cart'] ) ) {
        $url = wc_get_checkout_url();
        return $url;
    }       
}

It redirects if it is a product but if you are in the shop page it wont redirect, of course if you want to redirect in another page but in single product page the code should be if ( !isset( $_POST['add-to-cart'] ) ) {

I hope my question and answer helps somebody who visits this post

The function is_product() and/or is_single() can only identify the page after wp_query() is ready. Apparently, at the time your code is running, the said wp_query() has nothing and can not identify whether it is a single page or a category page etc. Thus, in your code the is_product() function is returning (bool) false.

On reproducing your code, I printed the wp_query from functions file and it yielded the result below.

What you want to achieve, might be possible, if you add your action after wp_query() is ready, in that case the second function to change redirect page might work.

WP_Query Object ( [query] => [query_vars] => Array ( )

[tax_query] => 
[meta_query] => 
[date_query] => 
[queried_object] => 
[queried_object_id] => 
[request] => 
[posts] => 
[post_count] => 0
[current_post] => -1
[in_the_loop] => 
[post] => 
[comments] => 
[comment_count] => 0
[current_comment] => -1
[comment] => 
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => 
[is_preview] => 
[is_page] => 
[is_archive] => 
[is_date] => 
[is_year] => 
[is_month] => 
[is_day] => 
[is_time] => 
[is_author] => 
[is_category] => 
[is_tag] => 
[is_tax] => 
[is_search] => 
[is_feed] => 
[is_comment_feed] => 
[is_trackback] => 
[is_home] => 
[is_404] => 
[is_embed] => 
[is_paged] => 
[is_admin] => 
[is_attachment] => 
[is_singular] => 
[is_robots] => 
[is_posts_page] => 
[is_post_type_archive] => 
[query_vars_hash:WP_Query:private] => 
[query_vars_changed:WP_Query:private] => 1
[thumbnails_cached] => 
[stopwords:WP_Query:private] => 
[compat_fields:WP_Query:private] => Array
    (
        [0] => query_vars_hash
        [1] => query_vars_changed
    )

[compat_methods:WP_Query:private] => Array
    (
        [0] => init_query_flags
        [1] => parse_tax_query
    )

) IS PRODUCT ? bool(false)

I just want to add to this question, that when you perform the checkout redirect, you probably want to change add to cart button text, it is possible with woocommerce_product_add_to_cart_text for product archives, and with woocommerce_product_single_add_to_cart_text for single product pages.

The next issue is the "The {product name} has been added to your cart" notice. Probably you want to hide it too, because it is shown on checkout page. It is possible too with wc_add_to_cart_message_html hook.

Code example you can find in this article.

Post a comment

comment list (0)

  1. No comments so far