Generate CSS Body Classes in Interspire Shopping Cart

This article is about the Interspire Shopping Cart. Don't use it? You probably would want to if you knew more.
Learn About the Cart at Interspire.com

If you’re used to WordPress’ body_class() function, this post for you.

Working with WordPress, it’s easy to get accustomed to some of its nice features, like the body_class() function introduced in WP 2.8. When working with Interspire Shopping Cart, I wanted the same level of information added to the page’s <body> tag.

Let’s do this in three stages:

1. Generate the body class

Starting from Dave Beck’s great canonical link module, I modified the code to create a list of classes based on what page is being viewed. This provides a lot of great information:

  • Brand
    • Brand ID
    • Brand name
  • Category
    • Category ID
    • Category Name
  • Home
  • Page
    • Page ID
    • Page Name
  • Sorted/Filtered/Paginated
    • Shop by Price
    • Tag
    • Sort type
    • Page number
  • Checkout/Account
    • Cart
    • Gift Certificate
    • Login
    • Search
    • SSL
    • Wishlist

This is very helpful stuff for custom styling using CSS.

Add to the bottom of the /lib/general.php file:

function BodyClass() {
	$bodyClass = array();
 
	if(isset($GLOBALS['ISC_CLASS_BRANDS'])) {
		$bodyClass[] = 'brand';
		$bodyClass[] = "brandID-".$GLOBALS['ISC_CLASS_BRANDS']-&gt;GetId();
		$bodyClass[] = "brandName-".$GLOBALS['ISC_CLASS_BRANDS']-&gt;GetBrandName();
	}
 
	if(isset($GLOBALS['ISC_CLASS_CATEGORY']) &amp;&amp; isset($GLOBALS['CatId'])) {
		$bodyClass[] = 'category';
		$bodyClass[] = "catID-{$GLOBALS['CatId']}";
		$bodyClass[] = "catName-".$GLOBALS['ISC_CLASS_CATEGORY']-&gt;GetName();
	}
 
	if(isset($GLOBALS['ISC_CLASS_INDEX'])) {
		$bodyClass[] = "home";
	}
 
	if(isset($GLOBALS['ISC_CLASS_PRODUCT']) &amp;&amp; $GLOBALS['ISC_CLASS_PRODUCT']-&gt;GetProductId() &gt; 0) {
		$bodyClass[] = 'product';
		$bodyClass[] = "productID-".$GLOBALS['ISC_CLASS_PRODUCT']-&gt;GetProductId();
		$bodyClass[] = "productName-".$GLOBALS['ISC_CLASS_PRODUCT']-&gt;GetProductName();
	}
 
	if(isset($GLOBALS['ISC_CLASS_PAGE']) &amp;&amp; !isset($GLOBALS['ISC_CLASS_INDEX']) &amp;&amp; $GLOBALS['ISC_CLASS_PAGE']-&gt;GetPageId() &gt; 0) {
		$bodyClass[] = 'page';
		$bodyClass[] = "pageID-".$GLOBALS['ISC_CLASS_PAGE']-&gt;GetPageId();
		$bodyClass[] = "pageName-".$GLOBALS['ISC_CLASS_PAGE']-&gt;GetPageTitle();
	}
 
	if(isset($GLOBALS['PriceMin']) &amp;&amp; isset($GLOBALS['PriceMax'])) {
		$bodyClass[] = 'price';
		$bodyClass[] = "shopByPrice";
	}
 
	if(isset($GLOBALS['ISC_CLASS_TAGS'])) {
		$bodyClass[] = 'tag';
		$bodyClass[] = "productTags";
	}
 
	if(isset($_REQUEST['page']) &amp;&amp; !empty($_REQUEST['page'])) {
		$bodyClass[] = 'paginated';
		$bodyClass[] = 'page'.(int)$_REQUEST['page'];
	}
 
	if(isset($_REQUEST['sort']) &amp;&amp; !empty($_REQUEST['sort'])) {
		$bodyClass[] = 'sort';
		$bodyClass[] = 'sort'.ucfirst($_REQUEST['sort']);
	}
 
	$current_url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 
		$noindex_url = str_replace("http://", "", $GLOBALS['ShopPathNormal'].'/cart.php');
		if($current_url == $noindex_url) {
			$bodyClass[] = "cart";
		}
 
		$noindex_url = str_replace("http://", "", $GLOBALS['ShopPathNormal'].'/giftcertificates.php');
		if($current_url == $noindex_url) {
			$bodyClass[] = "giftCertificates";
		}
 
		$noindex_url = str_replace("http://", "", $GLOBALS['ShopPathNormal'].'/login.php');
		if($current_url == $noindex_url) {
			$bodyClass[] = "login";
		}
 
		$noindex_url = str_replace("http://", "", $GLOBALS['ShopPathNormal'].'/search.php');
		$split_url = explode('?', $current_url);
		$split_url = $split_url[0];
		if($split_url == $noindex_url) {
			$bodyClass[] = "search";
		}
 
		$noindex_url = str_replace("http://", "", $GLOBALS['ShopPathNormal'].'/login.php?from=wishlist.php%3F');
		if($current_url == $noindex_url) {
			$bodyClass[] = "wishList";
		}
 
		if(($GLOBALS['ISC_CFG']["UseSSL"] &gt;= 1) &amp;&amp; ($_SERVER['HTTPS'] == 'on')) {
			$bodyClass[] = "ssl";
		}
	$i = 0;
	foreach($bodyClass as $class) {
		$class = preg_replace('/%[0-9]{2}/', '', $class); //mixed pattern, mixed replacement, mixed subject [, int limit [, int &amp;count]])
		$class = str_replace(array('+',''',' ','&lt;','&gt;','&amp;'),'', $class);
		$bodyClass[$i] = $class;
		$i++;
	}
	$bodyClass = implode(' ', $bodyClass);
 
	// WordPress integration
	if(function_exists('get_body_class')) {
		$wpBodyClass = get_body_class();
		$wpBodyClass = implode(' ', $wpBodyClass);
		$wpBodyClass = str_replace('home', '', $wpBodyClass);
		$wpBodyClass = trim($wpBodyClass);
		if(!empty($WPBodyClass)) {
			$bodyClass .= ' blog wordpress '.$wpBodyClass;
		}
	}
 
	return $bodyClass;
}

2. Generate the global variable `BodyClass`

Since code placed in general.php is available to every script in the cart, we can use the result of the function in the script that generates the website’s <head>, HTMLHead.php. We’re going to create a global variable named BodyClass, and add it to our template.

Add to the bottom of the /includes/display/HTMLHead.php file:

$GLOBALS['BodyClass'] = ' class="'.BodyClass().'"';

3. Add to your template

Now the not-terribly-fun part: in every template file, find <body> and replace with <body%%GLOBAL_BodyClass%%> — you may need to go into the __master folder to do this, depending on your setup.

This will generate CSS classes like this: <body class="category catID-123 catName-Example">, and will give you microscopic control over your template’s styling.

How would you do it?

Manually adding the `BodyClass` variable into each template is cumbersome, but the only other way to do it is to use ob_start() and process the page before it’s displayed to the browser, and that would add load time. Any other ideas on how to achieve this?

Author: Zack Katz

Zack Katz is the President of Katz Web Services and the developer of WordPress plugins with over 700,000 downloads. He lives in Southwest Colorado with his wife and two cats.

5 thoughts on “Generate CSS Body Classes in Interspire Shopping Cart”

  1. Needless to say – Impressive post. Many thanks for sharing this useful information. I am sure it is going to contribute alot for web professionals.

  2. I don’t know if this is due to automatic post formatting, but your function has no braces in it, those who expect a “plug and play” solution and do not understand much in the way of PHP will encounter a number of errors.

    The function and all IF statements should be wrapped in braces, also, the line:
    $bodyClass[] = “catID-$GLOBALS[‘CatId’]”;
    caused an error(possibly my server configuration) – solved by changing it to:
    $bodyClass[] = “catID-“.$GLOBALS[‘CatId’];

    Thank you for the resource – this definitely got me going in the right direction very quickly

  3. Any idea as to how I can get the product category name into the body class? The products will only ever have 1 category. It’s doing my head in trying to figure it out haha.

Leave a Reply

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