【问题标题】:Add product ids and order number as query strings on WooCommerce Thankyou page在 WooCommerce 感谢页面上添加产品 ID 和订单号作为查询字符串
【发布时间】:2021-02-26 18:17:49
【问题描述】:

url目前是这样的,

site.com/checkout/order-received/827/?key=wc_order_AN9woFNlCBbXM&product_id=211

我知道 827 是订单号,但我需要它作为字符串才能将数据传递给表单。

我可以在 URL 中获取产品 ID,但我似乎无法添加订单号。有更好的解决方案吗?如何添加两个查询字符串?任何帮助表示赞赏。

add_filter( 'woocommerce_get_checkout_order_received_url', 'custom_add_product_id_in_order_url', 10, 2 );
function custom_add_product_id_in_order_url( $return_url, $order ) {

    // Create empty array to store url parameters in 
    $sku_list = array();

    // Retrieve products in order
    foreach($order->get_items() as $key => $item){
        $product = wc_get_product($item['product_id']);
        //get sku of each product and insert it in array 

        $sku_list['product_id'] = $product->get_id();
    }

    // Build query strings out of the SKU array
    $url_extension = http_build_query($sku_list);

    // Append our strings to original url
    $modified_url = $return_url.'&'.$url_extension;

    return $modified_url;
}

【问题讨论】:

    标签: php wordpress woocommerce query-string orders


    【解决方案1】:

    自 WooCommerce3 以来,您的代码有点过时了。要将产品 ID 和订单号添加为 WooCommerce 订单接收页面的查询字符串,请改用以下内容:

    add_filter( 'woocommerce_get_checkout_order_received_url', 'add_product_ids_in_order_received_url', 10, 2 );
    function add_product_ids_in_order_received_url( $return_url, $order ) {
        $product_ids = array(); // Initializing
    
        // Retrieve products in order
        foreach( $order->get_items() as $item ){
            $product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
        }
        return $return_url . '&number=' . $order->get_order_number() . '&ids=' . implode( ',', $product_ids );
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    现在要从 URL 中获取产品 ID 和订单号,您将使用如下内容:

    // Get product Ids in an array
    if( isset($_GET['ids']) && ! empty($_GET['ids']) ){
        $product_ids_array = explode( ',', esc_attr($_GET['ids']) );
    }
    
    // Get order number
    if( isset($_GET['number']) && ! empty($_GET['number']) ){
        $order_number = esc_attr($_GET['number']);
    }
    

    【讨论】:

    • 非常感谢 Loic,您的代码一如既往地像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2021-06-12
    • 2016-10-04
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多