Single Breadcrumb based on HTTP_REFERER

This mod will create a single breadcrumb based on HTTP_REFERER, the actual path used to get to the product page.

And it will also maintain all the breadcrumbs for Similar Products. So if the product is filed under multiple categories, the Similar Products list will continue showing all associated categories.

In /classes/class.product.php, replace the whole _BuildBreadCrumbs() with below code:

public function _BuildBreadCrumbs(){
	$count = 0;
	$GLOBALS['BreadCrumbs'] = "";
	$GLOBALS['FindByCategory'] = "";
	$trailCategories = array();
	$crumbList = array();
	$query = sprintf("
		SELECT c.categoryid, c.catparentlist
		FROM [|PREFIX|]categoryassociations ca
		INNER JOIN [|PREFIX|]categories c ON (c.categoryid=ca.categoryid)
		WHERE ca.productid='%d'",
		$GLOBALS['ISC_CLASS_DB']->Quote($this->GetProductId())
	);
	$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
	while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
		if ($row['catparentlist'] == '') {
			$row['catparentlist'] = $row['categoryid'];
		}
		$cats = explode(",", $row['catparentlist']);
		$trailCategories = array_merge($trailCategories, $cats);
		$crumbList[$row['categoryid']] = $row['catparentlist'];
	}

	$trailCategories = implode(",", array_unique($trailCategories));
	$categories = array();
	if ($trailCategories != '') {
		// Now load the names for the parent categories from the database
		$query = sprintf("SELECT categoryid, catname FROM [|PREFIX|]categories WHERE categoryid IN (%s)", $trailCategories);
		$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
		while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
			$categories[$row['categoryid']] = $row['catname'];
		}
	}
	
	$wasCatBrowsing = false;
	$catDirectory = "/categories/";
	$referrer = getenv("HTTP_REFERER");
	$wasCatBrowsing = strpos($referrer, $catDirectory);
	if ($wasCatBrowsing !== false) { 
		$serverName = getenv("SERVER_NAME");
		$httpStuff = array($serverName, "http://", "https://"); 
		$referrer = str_replace($httpStuff, "", $referrer); 
		$argPosition = strpos($referrer, "?");
		if ($argPosition !== false) { $referrer = substr($referrer, 0, $argPosition); }
	}

	$breadBlock = 0;
	foreach ($crumbList as $productcatid => $trail) {
		$GLOBALS['CatTrailLink'] = $GLOBALS['ShopPath'];
		$GLOBALS['CatTrailName'] = GetLang('Home');
		$GLOBALS['BreadcrumbItems'] = $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("BreadcrumbItem");
		$GLOBALS['FindByCategoryItems'] = "";

		$cats = explode(",", $trail);
		$breadcrumbitems = "";
		$findbycategoryitems = "";
		$hasAccess = true;

		foreach ($cats as $categoryid) {
			if(!CustomerGroupHasAccessToCategory($categoryid)) {
				if ($categoryid == $productcatid) {
					$hasAccess = false;
					break;
				}
				continue;
			}
			if (isset($categories[$categoryid])) {
				$catname = $categories[$categoryid];
				$GLOBALS['CatTrailLink'] = CatLink($categoryid, $catname);
				$GLOBALS['CatTrailName'] = isc_html_escape($catname);
				$GLOBALS['BreadcrumbItems'] .= $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("BreadcrumbItem");
				$GLOBALS['FindByCategoryItems'] .= $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("ProductFindByCategoryItem");
			}
		}

		if ($hasAccess) {
			//$GLOBALS['CatTrailName'] = isc_html_escape($this->GetProductName());
			$GLOBALS['CatTrailName'] = isc_html_escape($this->_prodsku);
			$GLOBALS['BreadcrumbItems'] .= $breadcrumbitems . $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("BreadcrumbItemCurrent");

			if($breadBlock == 0){
				$GLOBALS['BreadCrumbs'] = $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("ProductBreadCrumb");
			}
			$GLOBALS['FindByCategory'] .= $findbycategoryitems . $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("ProductFindByCategory");

			if ($wasCatBrowsing !== false) {
				$refMatch = strpos($GLOBALS['BreadCrumbs'], $referrer);
				if ($refMatch !== false) {
					 $breadBlock = 1;
				}
			}
		}
	}
}

Leave a Reply

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