How to Add a Custom Parameter in Custom Thank You Page URL

One of our clients, Nordin, got back to us with a request. He wanted to append a custom parameter – user’s billing first name – in the Custom Thank You Page URL in the plugin Custom Thank You Page for WooCommerce.

Custom Thank You Page for WooCommerce plugin comes with a filter wherein you can append any custom parameter in the URL. Here is that filter:

apply_filters( 'modify_ctp_url', $thankyou_page_url );

The following code snippet will append the customer’s billing first name to your Custom Thank You Page URL:

add_filter( 'modify_ctp_url', 'my_custom_ctp_url' );
function my_custom_ctp_url( $thankyou_page_url ) {
	$part_url = parse_url( $thankyou_page_url );

	if ( isset( $part_url ) && isset( $part_url['query'] ) ) {
		$query_params = array();
		parse_str( $part_url['query'], $query_params );

		$order_id = absint( $query_params['ctp_order_id'] );
		$order = wc_get_order( $order_id );

		if ( $order instanceof WC_Order ) {
			$thankyou_page_url = add_query_arg(
						array(
							'ctp_first_name' => $order->get_billing_first_name(),
						), $thankyou_page_url
					);
		}

	}

	return $thankyou_page_url;
}

You can insert the above code snippet on your site by following the steps in this article.