3

I have created one module Popular Search. In this module I am adding custom popular keywords like,

  1. Shirt
  2. Jeans
  3. Dress

Now on search result page, I want to filter the result with these keyword. I have added Popular Search on left column for search result page only.

Logic behind filtering product is, when I click on any Popular keyword, it will check product title and list the product with matching.

Let say, I have 10 products on search result page www.demo.com/catalogsearch/result/?q=women. Now when I will click on dress, there is only 2 product that contain dress keyword in product title. So it will only list 2 product. I have added following code, but not getting how to filter with custom text.

<?php
$query = strtolower($_GET['q']);
if($this->getRequest()->getControllerName()=='result' && Mage::app()->getFrontController()->getAction()->getFullActionName()=='catalogsearch_result_index') {
    if($query != ""){
        $filteredList = $this->getLayout()
                ->getBlockSingleton('catalog/product_list')
                ->getLoadedProductCollection();
        foreach ($filteredList as $pro){
            $productNameArray[] = strtolower($pro->getName());
        }

        $collection = Mage::getModel('popularsearch/popularsearch')->getCollection();
        $collection->addFieldToFilter( 'status', '1' );
        if($collection->getSize()){
            foreach ($collection as $item) {
                $keywordName = strtolower($item->getKeyword());
                $keywordArray[] = $keywordName;

                foreach ($productNameArray as $proArray){
                    if( strpos( strtolower($proArray), $keywordName ) !== false ) {
                        $matchedKeywordList[] = strtolower($item->getKeyword());
                    }
                }
            }

            $matchedKeywordList = array_unique($matchedKeywordList);
            $finalLinkarray = array_merge(array_diff($matchedKeywordList, array($query)));
        }
    }
}
?>
<?php if($finalLinkarray):?>
        <dt>Popular Searches</dt>
        <dd>
            <?php foreach ($finalLinkarray as $link):?>
            <?php
                $newlink = $this->getBaseurl()."catalogsearch/result/?".$link;
            ?>
                <ol>
                    <li><a href="<?php echo $link;?>"><?php echo ucfirst($link);?></a></li>
                </ol>    
            <?php endforeach;?>
        </dd>
<?php endif;?>

any help will be appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.