Jump to content

Separate Checkout Thanks Page for Different Products/Services for Analytics Tracking


Recommended Posts

  • WHMCS Technical Analyst

At times, you may wish to have a separate Checkout Thanks Page, depending on the product/service that was just purchased.  It could be a special page, that asks your customers to sign up to a specific Newsletter, as well as providing you with the opportunity to cross-sell something related.  Additionally, it can form the basis of a more granular means of goal tracking for use in analytics.  

In both our examples (below), we use the order number stored in the $ordernumber string, that is available to us at the time of ordering.  Utilising, GetOrders API, we can then retrieve further information about the order, such as the product name, etc.  This can then been coded as a conditional, to simply redirect or to execute analytics tracking code, as you will see in the examples below:

1. Place your tracking code script in "complete.tpl".

In the example below, we are using the Standard Cart, order form template.  The code is placed in between the {if $ispaid} and the closing {/if} statement. 

E.G: /templates/orderforms/standard_cart/complete.tpl [line 51]:

{if $ispaid}
	<!-- Enter any HTML code which should be displayed when a user has completed checkout -->
	<!-- Common uses of this include conversion and affiliate tracking scripts -->
        
	// We are using GetOrders API, providing the order number placed, so that we can retrieve the name of the Product that was ordered. 
        $command = 'GetOrders';
        $postData = array(
            'id' => $ordernumber,
        );

        $adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later
        $results = localAPI($command, $postData, $adminUsername);

        // Below we are retrieving the productname value for the order. However, we can just as easily use another factor, e.g., product type, etc.
        // In the case of Product Name; this is retrieved from the results array, as follows:

        $productname = $results["orders"]["order"]["0"]["lineitems"]["lineitem"]["0"]["product"];

        // The switch statement below will run code, depending on the Product Name as the condition.
        // For this example, we are simply redirecting to another URL, or web page.
        // Modify the links, or replace with your tracking code as needed.

        switch($productname){
            case "Shared Hosting - Bronze":  // This would need to be one of your own Product Names
            // In this example, we are redirecing when the above condition is met.
            // Or you can replace this with your own tracking code script here.
                header("Location: https://example.com/myOtherPage.php"); die(); 
                break;
            case "Shared Hosting - Silver":
                header("Location: https://example.com/myOtherPage.php"); die();
                break;
            case "Shared Hosting - Gold":
                header("Location: https://example.com/myOtherPage.php"); die();
                break;
            default:
                // default forwarding URL, if above conditions are not met:
                header("Location: https://example.com/myOtherPage.php"); die();
                break;
        }
{/if}

The above script, obtains the product name ordered, then based on this criteria, once the order has completed, forwards the user to a specified page. 

Alternatively, you could add your own tracking script, between the {if $ispaid} and the closing {/if} statement, shown above. The script would still of course need to use the {$ordernumber} to retrieve data about the order, e.g., product name, etc. Then based on this, you would run the desired tracking script.

2. Place your tracking code script in a Hook.

Another option would be to utilise a hook.  The hook point to use would be the ShoppingCartCheckoutCompletePage that executes when the Complete Page is displayed on checkout.  Similar to the example above, the hook code could simply be a redirect, or run your tracking script as desired.

Example Code:

<?php
require_once 'init.php';
add_hook('ShoppingCartCheckoutCompletePage', 1, function($vars) {

    // We are using GetOrders API, providing the order number placed, so that we can retrieve the name of the Product that was ordered.

    $command = 'GetOrders';
    $postData = array(
        'id' => $ordernumber,
    );

    $adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later
    $results = localAPI($command, $postData, $adminUsername);

    // Below we are retrieving the productname value for the order. However, we can just as easily use another factor, e.g., product type, etc.
    // In the case of Product Name; this is retrieved from the results array, as follows:

    $productname = $results["orders"]["order"]["0"]["lineitems"]["lineitem"]["0"]["product"];

    // The switch statement below will run code, depending on the Product Name as the condition.
    // For this example, we are simply redirecting to another URL, or web page.
    // Modify the links, or replace with your tracking code as needed.

    switch($productname){
        case "Shared Hosting - Bronze":  // This would need to be one of your own Product Names
        // In this example, we are redirecing when the above condition is met.
        // Or you can replace this with your own tracking code script here.
            header("Location: https://example.com/myOtherPage.php"); die(); 
            break;
        case "Shared Hosting - Silver":
            header("Location: https://example.com/myOtherPage.php"); die();
            break;
        case "Shared Hosting - Gold":
            header("Location: https://example.com/myOtherPage.php"); die();
            break;
        default:
            // default forwarding URL, if above conditions are not met:
            header("Location: https://example.com/myOtherPage.php"); die();
            break;
    }
});

Both examples above are based on three product names which we wish to track, and a fallback (default) option.  However, you can adjust the switch statement according to your needs.  Please see the PHP manual for switch statements.

For further information:
https://developers.whmcs.com/hooks-reference/shopping-cart/#shoppingcartcheckoutcompletepage

3. GetOrders API: Locating an Item within the $results Array

You may be wondering how we constructed the $productname string, in the examples above.  For the purposes of completeness, we shall further elaborate.

As you have seen, using the GetOrders API, we utilise the $ordernumber available to us at the time of ordering, in order to retrieve the product name.  However, the results array that is produced, can be a little daunting to navigate, especially when you simply need one value from it.

For instance, for this to be useful in our examples above, we first need to be able to locate the respective product name, from the GetOrders API results array.  To do this, you can feed the JSON $results, sample output provided, into a JSON to PHP Array Converter to get the following PHP array:

php_array.jpg.5012affb0ae3ae4074fb183c9ff573dd.jpg

As we previously noted, the $results (variable) is actually in the form of an array  (shown above).  Here we are depicting how we locate the product name, within the array.  This can then be used to construct the statement below, allowing us to store this single value ("product") into a variable called $productname:

$productname = $results["orders"]["order"]["0"]["lineitems"]["lineitem"]["0"]["product"];

This $productname (variable) can then be used as the criteria for a condition in an {if ...} ... {/if} statement, or another conditional, such as the switch we use in our examples above.  Alternatively, this can just as easily be based on another criteria such as product type ("producttype"), etc.  

As such, this hopefully provides us with a very powerful and flexible means of analytics tracking, upon order placement.

For further information:
https://developers.whmcs.com/api-reference/getorders/

Further Reading

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated