How to change or add Woocommerce thank you page URL key content?

you can edit the return url woocommerce provides to gateways by using the filter

woocommerce_get_return_url

some gateway plugins use a different method to get return url by invoking $order->get_checkout_order_received_url() ; which applies the filter

woocommerce_get_checkout_order_received_url

an example would be like :

add_filter('woocommerce_get_return_url','override_return_url',10,2);

function override_return_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;

  }

result url will be like

http://example.com/index.php/checkout/order-received/161/?key=wc_order_585214b2abb65&product_8=SKU1&product_45=SKU2

Leave a Comment