You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce自定义跳转URL传订单商品ID为空,求助解决方案

Fixing Empty Product ID in WooCommerce Post-Purchase Redirect

Hey Mario, I see the issue with your code—let's get that product ID showing up in your redirect URL properly!

The Problem with Your Current Code

You're trying to use the global $product variable on the order-received checkout page, but this variable isn't initialized here. WooCommerce doesn't load a global product object on the order confirmation page because the page is focused on the order, not a single product. That's why your $id is coming up empty.

The Solution: Get Product IDs from the Order Object

Instead of relying on a global product, we need to fetch the completed order first, then pull the product IDs directly from that order. Here's the revised code that will work:

add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
    global $wp;

    // Check if we're on the order-received checkout page
    if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
        // Grab the order ID from the URL query vars
        $order_id = absint( $wp->query_vars['order-received'] );
        // Fetch the full order object using WooCommerce's built-in function
        $order = wc_get_order( $order_id );

        if ( $order ) {
            $product_ids = array();
            // Loop through each item in the order to get product IDs
            foreach ( $order->get_items() as $item ) {
                $product_ids[] = $item->get_product_id();
            }

            // Handle single or multiple products: join IDs with commas
            $product_id_string = implode( ',', $product_ids );

            // Redirect to your custom thank you page with product IDs
            wp_redirect( 'http://example.com/success.php?pid=' . $product_id_string );
            exit;
        }
    }
}

Key Details About This Code:

  • wc_get_order(): This is the official WooCommerce way to retrieve an order object from an order ID—it's reliable and handles edge cases like invalid orders.
  • get_items(): Loops through every product in the order, so even if the customer bought multiple items, all their IDs will be included in the URL (e.g., pid=123,456).
  • Single Product Alternative: If you only ever expect customers to buy one product at a time, you can simplify the ID retrieval to:
    $product_id = reset( $product_ids ); // Gets the first product ID in the array
    

Quick Test Tip

After adding this code, place a test order and check the redirect URL—you should see the product ID(s) in the pid parameter now. If you run into issues, double-check that the order ID is being pulled correctly (you can add var_dump($order_id); temporarily to verify).

内容的提问来源于stack exchange,提问作者Mario

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:40:19