我有一个伍德商务网站&使用PayU支付系统。现在,当客户订单失败时,将重定向到订单支付端点&当订单成功时,页面将重定向到收到订单的端点。我需要客户重定向到一个特定的自定义网址时,订单失败和成功的订单,而不是重定向到收到订单的端点,我想显示订单摘要详细信息,防止用户重定向到主页。
我在functions.php中尝试了下面的
add_action( 'woocommerce_thankyou', 'test_func');
function test_func( $order_id ) {
$order = wc_get_order( $order_id );
$url1 = 'https://yoursite.com/custom-url-1';
$url2 = 'https://yoursite.com/custom-url-2';
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( $url1 );
exit;
} else {
wp_safe_redirect( $url2 );
exit;
}
}但它仍然会重定向到前面提到的结帐端点。

我知道它是从高级部分中提到的woocommerce结帐端点进行的,但是有人能帮我找到解决这个问题的方法吗?
任何帮助都将不胜感激。
提前谢谢。
发布于 2021-04-10 19:10:48
我已经为你的问题准备了一个解决方案。我测试了你的代码,看起来woocommerce已经改变了一些东西,或者我不知道,这种代码不再工作了。但不用担心。我已经在互联网上搜索并为你找到了一个很好的解决方案,我已经准备好并在我的临时网站上进行了测试,它工作得很好。
您需要使用template_redirect并将其设置为结帐端点url (您的case - order-received)。然后,您需要使用正确的功能来完成其余工作,以使其按您预期的方式工作。把我的代码放到你的主题的functions.php文件中,你的问题就解决了。不要忘记使用您网站的端点url (order-received)更改端点url,然后更改订单失败状态(google.com)和其他状态(yahoo.com)的url
add_action( 'template_redirect', 'wc_thank_you_redirect' );
function wc_thank_you_redirect(){
if( isset( $_GET['key'] ) && is_wc_endpoint_url( 'order-received' ) ) { //change order-received to your endpoint if you will change it in the future
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
if ( ! empty( $order_id ) ) {
$order = wc_get_order( $order_id );
$order_status = $order->get_status();
if ('failed' == $order_status ) { //failed order status
wp_redirect( 'https://google.com' ); //change url here
exit;
} else {
wp_redirect( 'https://yahoo.com' ); //change url here for other statuses redirect
exit;
}
}
}
}要使用order-pay实现这一点,您可以很容易地修改我的函数,将端点url更改为order-pay,并从函数中删除else。要显示订单摘要,您需要为新URL设置模板。也许更好的做法是只重定向失败的订单,并为感谢页面修改现有的woocommerce模板。
https://stackoverflow.com/questions/67030636
复制相似问题