Magento search

18.Jan.2011

It is easy to use Magento search, even if outside of your Magento installation.

First, we shall write a function to use Magento search:

1
2
3
4
5
6
7
8
9
10
11
function getCatalogSearch($term) {
	$mageFilename = realpath('../shop/app/Mage.php');
	require_once( $mageFilename );
	umask(0);
 
	// Initialize Magento
	Mage::app();
	$search = Mage::getSingleton('catalogsearch/advanced')->addFilters(array('name' => $term));
 
	return $search->getProductCollection();
}

Second, lets use function to present users with our search results:

1
2
3
4
5
$term = $_REQUEST['s'];
foreach(getCatalogSearch($term) as $product) {
	echo '<a href="' . $product->getProductUrl() . '">' . $product->getName() . '<br/>';
	echo '<img src="' . $product->getSmallImageUrl() . '" /></a>';
}

Notice: If you are using this function from WordPress in order to integrate WordPress and Magento search make sure to read how to integrate Magento and WordPress.