使用 URL 中的订单 ID 从 Woocommerce 重定向到 Php 核心

Redirect from Woocommerce to Php core with the Order ID in the URL

我在两个网站上工作(WordPress/Woocommerce 和 PHP 核心)。我不知道我想做的事情是否可行,但我需要你的建议。

我想在从 Woocommerce 购买后将客户重定向到 PHP 核心网站,包括 URL 中的订单 ID。

这是否可能或有任何其他方法可以完成此任务?

您可以使用 Wordpress 专用 template_redirect 钩子在购买后进行重定向,并通过 url 传递任何订单数据,如本例所示:

add_action( 'template_redirect', 'order_received_redirection' );
function order_received_redirection() {
    // Only on "Order received" page
    if( is_wc_endpoint_url('order-received') ) {
        global $wp;
        $order_id  = absint( $wp->query_vars['order-received'] );
        $order = wc_get_order($order_id);
        $key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : $order->get_order_key();
        $total = $order->get_total();

        // HERE BELOW Set your redirection url
        $url_redirect = 'http://www.my-domain.com/?orderid='.$order_id . '&key=' . $key . '&total=' . $total;

        // Redirect 
        wp_redirect( $url_redirect );
        exit(); // Always exit
    }
}

代码进入您的活动子主题(活动主题)的 functions.php 文件。已测试并有效。

相关: