Generate CSS Body Classes in Interspire Shopping Cart

Interspire CSS Body Class

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']->GetId();
		$bodyClass[] = "brandName-".$GLOBALS['ISC_CLASS_BRANDS']->GetBrandName();
	}

	if(isset($GLOBALS['ISC_CLASS_CATEGORY']) && isset($GLOBALS['CatId'])) {
		$bodyClass[] = 'category';
		$bodyClass[] = "catID-{$GLOBALS['CatId']}";
		$bodyClass[] = "catName-".$GLOBALS['ISC_CLASS_CATEGORY']->GetName();
	}

	if(isset($GLOBALS['ISC_CLASS_INDEX'])) {
		$bodyClass[] = "home";
	}

	if(isset($GLOBALS['ISC_CLASS_PRODUCT']) && $GLOBALS['ISC_CLASS_PRODUCT']->GetProductId() > 0) {
		$bodyClass[] = 'product';
		$bodyClass[] = "productID-".$GLOBALS['ISC_CLASS_PRODUCT']->GetProductId();
		$bodyClass[] = "productName-".$GLOBALS['ISC_CLASS_PRODUCT']->GetProductName();
	}

	if(isset($GLOBALS['ISC_CLASS_PAGE']) && !isset($GLOBALS['ISC_CLASS_INDEX']) && $GLOBALS['ISC_CLASS_PAGE']->GetPageId() > 0) {
		$bodyClass[] = 'page';
		$bodyClass[] = "pageID-".$GLOBALS['ISC_CLASS_PAGE']->GetPageId();
		$bodyClass[] = "pageName-".$GLOBALS['ISC_CLASS_PAGE']->GetPageTitle();
	}

	if(isset($GLOBALS['PriceMin']) && isset($GLOBALS['PriceMax'])) {
		$bodyClass[] = 'price';
		$bodyClass[] = "shopByPrice";
	}

	if(isset($GLOBALS['ISC_CLASS_TAGS'])) {
		$bodyClass[] = 'tag';
		$bodyClass[] = "productTags";
	}

	if(isset($_REQUEST['page']) && !empty($_REQUEST['page'])) {
		$bodyClass[] = 'paginated';
		$bodyClass[] = 'page'.(int)$_REQUEST['page'];
	}

	if(isset($_REQUEST['sort']) && !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"] >= 1) && ($_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 &count]])
		$class = str_replace(array('+','\'',' ','<','>','&'),'', $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?

Related posts:

  1. Add ‘Customers Also Purchased’ to the Cart Page on Interspire Shopping Cart
  2. How to Add an `Edit Product` Link for Interspire Shopping Cart
  3. Preview Hidden Products in Interspire Shopping Cart

Comments are closed.