CS-Cart Customization: Product Downloads Counter

Notice: This post is aimed at experienced users. It applies to CS-Cart v.3.0.x.

I have already written posts about the CS-Cart shopping cart, here is a new one!

Here is a scenario:

You sell downloadable digital products in your CS-Cart store.

You want to know how many times a user downloaded a file.

You need to keep records of when the customer first and last downloaded the product.

CS-Cart lets you sell digital and downloadable products very easily. It keeps also track of how many times a user “clicked” on the download link (but does not tell you if the file was correctly sent to the user, which might cause an issue if you want to fully refund not-downloaded orders)

Make these modifications to put things in the right way:

1- Edit: <CS-CART_ROOT>/controllers/customer/orders.php

and replace the ‘get_file’ mode block (lignes 403 to 435, may vary upon versions) with the following block:

} elseif ($mode == 'get_file') {

	$field = empty($_REQUEST['preview']) ? 'file_path' : 'preview_path';

	if (($field == 'file_path' && !empty($_REQUEST['ekey']) || $field == 'preview_path')) {

		if (!empty($_REQUEST['ekey'])) {

			$ekey_info = fn_get_product_edp_info($_REQUEST['product_id'], $_REQUEST['ekey']);

			if (empty($ekey_info) || $ekey_info['file_id'] != @$_REQUEST['file_id']) {
				return array(CONTROLLER_STATUS_DENIED);
			}

			// Increase downloads for this file
			$max_downloads = db_get_field("SELECT max_downloads FROM ?:product_files WHERE file_id = ?i", $_REQUEST['file_id']);
			$file_downloads = db_get_field("SELECT downloads FROM ?:product_file_ekeys WHERE ekey = ?s AND file_id = ?i", $_REQUEST['ekey'], $_REQUEST['file_id']);

			if (!empty($max_downloads)) {
				if ($file_downloads >= $max_downloads) {
					return array(CONTROLLER_STATUS_DENIED);
				}
			}

		}

		$file = db_get_row("SELECT $field, file_name, product_id FROM ?:product_files LEFT JOIN ?:product_file_descriptions ON ?:product_file_descriptions.file_id = ?:product_files.file_id AND ?:product_file_descriptions.lang_code = ?s WHERE ?:product_files.file_id = ?i", CART_LANGUAGE, $_REQUEST['file_id']);

		if (!empty($file)) {
			if(fn_get_file(DIR_DOWNLOADS . $file['product_id'] . '/' . $file[$field]) != false)
			{
				db_query('UPDATE ?:product_file_ekeys SET ?u WHERE file_id = ?i AND product_id = ?i AND order_id = ?i', array('downloads' => $file_downloads + 1), $_REQUEST['file_id'], $ekey_info['product_id'], $ekey_info['order_id']);

			}
		}
	}

	return array(CONTROLLER_STATUS_DENIED);

2- Edit <CS-CART_ROOT>/core/fs.fs.php and change the line 332 (where the fn_get_file function calls the “exit;”) to the following:

			return false;

These modifications, make sure the file is sent to the user before increasing the download counter.

Now, lets make the download counter visible in your administration panel.

1- Edit <CS-CART_ROOT>/skins/<ACTIVE_ADMIN_SKIN>/admin/views/orders/details.tpl

After line 343, inside the TR tag, add the column header:

<th>{$lang.downloads_count}</th>

Now, after line 354, add the download counter value:

<td>{$file.downloads}</td>

Save your file and refresh your CS-Cart cache by opening your store’s administration panel and then adding &cc to the end of the page url (if you are on the admin index, also add a “?”)

example: https://www.examplestore.com/administration.php?&cc

Now, go to a confirmed/processed order and on the downloads tab you will have:

CS-Cart download counter in administration panel

In the next post, I will write about how to keep track of first and last download times to let you know more about your customers.