How to show more categories of a linked product

In osCommerce, there is a mechanism by which a product can exist in multiple categories. It’s the “copy to” functionality on the product adding/editing page in admin. Obviously, the “copied” product needs to be LINKED (not DUPLICATED) as a linked product has the same ID as the original product whereas a duplicated product gets a new ID…

So, assuming we have 1 linked product across a number of categories we might want to show the customer the other categories in which it resides. There is no in-built way to do this, so we need to make some code to enable it.

Step 1. Add the code to get (and display) the other categories in which the product resides

Open up product_info.php, add:

$catQuery = tep_db_query("SELECT categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and categories_id NOT IN ('" . $current_category_id . "')");
while ($cat = tep_db_fetch_array($catQuery)) {
echo sprintf(ALSO_SEEN, $product_info['products_name'], tep_get_path($cat['categories_id']), clubosc_get_category_name($cat['categories_id']));

Here we are making a call to the database to get all the categories the product is in, other than the category we are presently looking at. There is no point in letting the customer know it exists “here” where he is looking.

Step 2: Add the language define

Open /includes/languages/english/product_info.php, add

define('ALSO_SEEN', '
%s can also be seen in < a href="' . tep_href_link(FILENAME_DEFAULT, '%s') . '" >%s< /a >');

Here we are passing 3 parameters to the text definition. These 3 parameters are; product name, category path, category name. These are passed by the code in Step 1.

Step 3: Add a new function to get and display the category name

Open /includes/functions/general.php, add

function clubosc_get_category_name($cat, $language = '') {
global $languages_id;

if (empty($language)) $language = $languages_id;

$categories_query = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$cat . "' and language_id = '" . (int)$languages_id . "'");
$categories = tep_db_fetch_array($categories_query);

return $categories['categories_name'];

Here we pass through the category id (in Step 1 code), and use it to get the name of the other category/categories the product is in.

I have covered making functions in the past in this blog and I have also covered in quite some detail how “sprintf” works.

The result of this will be a line (per category the linked product is in) of text like this;

Of course, you can pretty up the text as you see fit, using .css. Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *