简体   繁体   English

Woocommerce在添加到购物车之前重定向到自定义页面

[英]Woocommerce redirect to custom page before adding to cart

I am trying to redirect to a custom page before adding item to cart because I want to authenticate few information before user can really add the item to cart. 我想在将商品添加到购物车之前重定向到自定义页面,因为我想在用户真正将商品添加到购物车之前验证一些信息。

How can set a page where user redirect when click on Add To Cart and give some pre cart information and then click on Proceed to adding to cart? 单击“添加到购物车”并提供一些预先的购物车信息,然后单击“继续添加到购物车”时,如何设置用户重定向的页面?

This is my first Woocommerce site, so I am having some trouble. 这是我的第一个Woocommerce网站,所以我遇到了麻烦。 So far what I have found in tutorial is this code. 到目前为止,我在本教程中发现的是此代码。

function custom_add_to_cart_redirect() { 
    return 'http://www.yourdomain.com/your-page/'; 
}
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );

But it add the cart first then redirect to this custom page. 但是它首先添加购物车,然后重定向到该自定义页面。 But I want to redirect without adding to cart. 但是我想重定向而不添加到购物车。

Please help 请帮忙

add_to_cart_action() is called as an action on 'wp_loaded'. add_to_cart_action()作为对“ wp_loaded”的操作被调用。

add_action( 'wp_loaded', array( __CLASS__, 'add_to_cart_action' ), 20 );

so you can execute your handler before add_to_cart_action() by: 因此,您可以通过以下方式在add_to_cart_action()之前执行处理程序:

add_action( 'wp_loaded', array( __CLASS__, 'your handler' ), 19 );

NB add_to_cart_action() first checks that this is really an add to cart request and just returns if it is not by: 注意:add_to_cart_action()首先检查这是否确实是添加到购物车请求,如果不是,则返回:

if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
    return;
}

See add_to_cart_action() in ...\\woocommerce\\includes\\class-wc-form-handler.php 参见... \\ woocommerce \\ includes \\ class-wc-form-handler.php中的add_to_cart_action()

You need to do this as 'wp_loaded' is called on all requests. 您需要执行此操作,因为在所有请求上都会调用“ wp_loaded”。 After requesting the redirect wp_safe_redirect() you need to exit to prevent add_to_cart_action() from running on this request. 请求重定向wp_safe_redirect()之后,您需要退出以防止add_to_cart_action()在此请求上运行。 After verification the redirect processing needs to invoke the original request again with some indication that the request is now verified. 验证之后,重定向处理需要再次调用原始请求,并带有指示该请求已被验证的指示。

PS I agree with the comment made by cale_b - doing the verification is probably easier done earlier. PS:我同意cale_b的评论-进行验证可能更容易进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM