我有一个 woocommerce 网站并使用 PayU 支付系统。截至目前,当客户订单失败时,重定向发生在订单支付端点,当订单成功时,页面重定向到订单接收端点。我需要客户在订单失败和成功订单时重定向到特定的自定义 URL,而不是重定向到订单接收端点,我想显示订单摘要详细信息并防止用户重定向到主页。
我在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 结帐端点,但有人可以帮我找到解决方法吗?
任何帮助将不胜感激。
提前致谢。
答案 0 :(得分:3)
我已经为您的问题准备了解决方案。我已经测试了你的代码,看起来 woocommerce 已经改变了一些东西,或者我不知道,这种代码不再工作了。不过不用担心。我已经在互联网上搜索并为您找到了一个很好的解决方案,我已经在我的临时网站上准备并测试了该解决方案,并且效果很好。
您需要使用 template_redirect
并将其设置为结帐端点 url(您的情况 - order-received
)。然后,您需要使用正确的功能完成剩下的工作,以使其按预期工作。把我的代码放在你主题的functions.php文件中,你的问题应该可以解决。不要忘记将端点 url 更改为您网站的端点 url (order-received
),然后更改订单失败状态 (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 模板。