Search by Category in Shopp

Today I saw a thread on the Shopp forums asking how to limit a search to a particular category. As this was on my big list, I decided to tackle it.

It turns out to not be very difficult, once you understand the way Shopp handles searches.

The trick is that the category template (i.e., theme/shopp/category.php) is used to display searches. This allows you to easily jump in and decide how it is displayed, based on your own criteria.

So, you will want to create a copy of your category.php file named something like category-default.php. Then create a separate file named category-search.php. The former file will handle the default category page functionality. The second will handle searches only.

In category.php, do something like this:

if(isset($_GET['s']) && isset($_GET['catalog']))
{
	include_once('category-search.php');
}
else
{
	include_once('category-default.php');
}

Basically, if the URL parameters for a search exist, use the search template, otherwise, stick to the normal game plan.

Then in category-search.php, you’re going to want to use your own loop to exclude every category but the one you’re interested in:

<?php
if(isset($_GET['category']))
{
	$category = $_GET['category'];
}
?>
<?php if (shopp('category','has-products')): ?>
	<?php while(shopp('category','products')): ?>
	 	<?php if(shopp('product','in-category',"name=$category") || (!isset($category) || $category == "Everything")): ?>
		// your code to display in your own html using the product tags (model it after the default category template, for example)
 
		<?php endif; ?>
	<?php endwhile; ?>

Lastly, you’ll want to render the search form yourself instead of using the default widget. For example:

			<form id="search" action="<?php bloginfo('url'); ?>" method="get" accept-charset="utf-8">
				<div class="search-perimeter">
					<input autocomplete="off" class="search" type="text" name="s" value="" id="find" />
					<select size=1 name="category" id="category-dropdown" title="Search in">
						<option value="Category 1 Name">Category 1 Name</option>
						<option value="Category 2 Name">Category 2 Name</option>
						<option value="Everything">Everything</option>
					</select>
					<input type="image" class="search-button" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" />
				</div>
				<input type="hidden" name="catalog" value="true" id="catalog" />
			</form>

That’s about all there is to it. You’ll have to get in deep with the templates, but I think you’ll agree this is conceptually pretty simple.

Lastly, you should note: Category names seem to be case sensitive. “Bags” != “bags”

Good luck,
Clif

  • WIlbert van Beek

    For a long time I have been looking for a solution similar to this one. I would like to have the Shopp seach option to search all categories except 1.
    Any suggestions ??

  • http://www.friv3.co/ friv 3

    Very useful and supportive article. I wish I can do all of
    that in a short period of time.