WooCommerce: add Product ID in Order Received URL

webroomtech.com
Nov 13, 2019

--

Here is a quick, easy way to add the Product ID in the URL when an order is placed in your WooCommerce store.

We will modify the WooCommerce checkout order received (Thank you page) url using the woocommerce_get_checkout_order_received_url hook.

PHP snippet: add Product ID to Thank you page url

add_filter(‘woocommerce_get_checkout_order_received_url’,’webroom_add_prduct_id_in_order_url’,10,2);

function webroom_add_prduct_id_in_order_url($return_url,$order){

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

// retrive 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_’.$item[‘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;
}

Now, all you have to do is to paste the above snippet in your child theme’s functions.php file. Once you do that and place an order, you will notice that the url is now changed as:

https://www.example.com/checkout/order-received/147492/?key=wc_order_cHWfLlT0lhpaQ&product_id_146979=146979

If there are more than one product in the order, the url will have them all:

https://www.example.com/checkout/order-received/147492/?key=wc_order_cHWfLlT0lhpaQ&product_id_146979=146979&product_id_1167=1167

That’s it! So simple. You can also add the product SKU if you want, just use the following code.

PHP snippet: add Product ID and SKU to Thank you page url

To view the snippet, go to the original article, here.

--

--

webroomtech.com
webroomtech.com

Written by webroomtech.com

0 Followers

webroom.tech is created and written with one purpose: to help anyone developing their awesome WordPress and WooCommerce website.

No responses yet