admin管理员组文章数量:1130349
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 |3 Answers
Reset to default 1Well, 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.
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
$urlinside 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
3 Answers
Reset to default 1Well, 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.
本文标签: filtersRedirect to checkout woocommerce failed
版权声明:本文标题:filters - Redirect to checkout woocommerce failed 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749180185a2328472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$urlinside the if statement? – sandrodz Commented Feb 18, 2018 at 8:41