1

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

enter image description here

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.com]/product/[name-of-product]/?add-to-cart=[PRODUCTID]

The path

[www.domain.com]/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 ?

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 Commented Feb 18, 2018 at 15:21

3 Answers 3

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

0

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)

3
  • That would be perfect, thanks but I dont know how to add the action after wp_query() is ready, can you tell me how to do that ? Commented Feb 19, 2018 at 15:35
  • I haven't tried this but assuming it can be done, your code above, should be hooked into the wordpress hook using add_action() (Ref - codex.wordpress.org/Plugin_API/Action_Reference); so when your code is called, it is able to identify whether its a single page or single product page.
    – Aadil P.
    Commented Feb 19, 2018 at 18:56
  • So, instead of add_filter I should use add_action ? Commented Feb 19, 2018 at 19:04
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.