如何在 Woocommerce 中向自定义添加变量谢谢 url

How to add variables to custom thank you url in Woocommerce

我能够按照重定向教程进行操作 https://businessbloomer.com/resolved-woocommerce-redirect-custom-thank-page/

但似乎无法将 woocommerce 变量添加到 url。

我想要这样的东西 > http://example.com?EO_ID=M180924-678922&Product_Code=vssx2&Quantity=1&Email=billing_email@gmail.com'

谢谢。

链接代码已过时,不处理任何 URL 查询字符串。您应该需要进行更改并调整代码。

Note: Orders can have many items, so to make it work for your case we use the first item only.

试试这个(您需要为 $path 变量定义 URL 路径:

add_action( 'woocommerce_thankyou', 'thankyou_custom_redirect', 5, 1 );
function thankyou_custom_redirect( $order_id ){
    // Get the WC_Order object instance
    $order         = wc_get_order( $order_id );

    // Order data
    $order_key     = $order->get_order_key(); // Get order key (if needed)
    $transaction_id= $order->get_transaction_id(); // Get order key (if needed)
    $billing_email = $order->get_billing_email(); // Get billing email
    $order_num     = $order->get_order_number(); // Get order number
    $order_date    = $order->get_date_created(); // Get order creation date

    // Order item data (first item)
    $order_items = $order->get_items(); // Get order items
    $first_item  = reset($order_items); // Keep the first Item
    $item_qty    = $first_item->get_quantity(); // Item quantity
    $product     = $first_item->get_product(); // Get the WC_Product object instance
    $sku         = $product->get_sku(); // Get the product code (SKU)

    // Build your query string
    $query_string  = '?EO_ID=' . $order_date->date('ymd') . -rand(pow(10, 5), pow(10, 6)-1);
    $query_string .= '&Product_Code=' . $sku;
    $query_string .= '&Quantity=' . $item_qty;
    $query_string .= '&Email=' . $billing_email;

    $path = '/custom-path/'; // <=== HERE define the url path (without the domain)
    $url  = home_url( $path . $query_string );

    // Not for failed orders
    if ( ! $order->has_status( 'failed' ) ) {
        wp_redirect( $url );
        exit();
    }
}

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