Home / WooCommerce / WooCommerce: add Product ID in Order Received URL

WooCommerce: add Product ID in Order Received URL

Published On: November 12th, 2019|Categories: WooCommerce|Tags: , , , , |3 min read|

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

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_'.$item['product_id'] . '_sku'] = $product->get_sku();
    }
    //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, the url will be:

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

How to get Product id in WooCommerce?

Go ahead and edit a product in WooCommerce. Now take a look at the url, it will look like:

https://www.example.com/wp-admin/post.php?post=12345&action=edit

You will find the product id in ?post=12345.

Use case

Using Product ID / SKUs in the Order Received URL is very handy when you want to set a Google Analytics Goal, for example. Just point to the destination page below as a regular expression:

\/checkout\/order-received\/\d+\/\?key=\w+&product_ID_HERE_sku=SKU_HERE

In case the Order Received URL contains only the Product ID use this:

\/checkout\/order-received\/\d+\/\?key=\w+&product_id_12345=12345

What actually is the WooCommerce Thank you page url?

The WooCommerce thank you page url is directly correlated with the WooCommerce Checkout Endpoints.

The following endpoints are used for checkout-related functionality and are appended to the URL of the /checkout page:

  • Pay page – /order-pay/{ORDER_ID}
  • Order received (thanks) – /order-received/
  • Add payment method – /add-payment-method/
  • Delete payment method – /delete-payment-method/
  • Set default payment method – /set-default-payment-method/

Learn more about WooCommerce Endpoints here.




Related Articles

If you enjoyed reading this, then please explore our other articles below:

More Articles

If you enjoyed reading this, then please explore our other articles below: