Pull Magento's Most Common Search Queries

Submitted by deeperdata - 2 years ago

I used the following script to provide a "feed" for a customer who would later make use of this data to populate a "popular search" block. You could go one step further and combine it with some frontend library to populate an "auto-suggest" search. NOTE: The redirect field might not show for everyone. The popularity field will show the number of times the term has been searched for.

// Run this from your Magento root (in a file e.g. topsearches.php)

<?php
        // Path to Mage.php;
        require_once('app/Mage.php');
        Mage::app();

        $searchCollection = Mage::getModel('catalogsearch/query')->getCollection()
         ->setOrder('popularity', 'DESC');
        // Choose how many results to show
        $searchCollection->getSelect()->limit(20);
        foreach ($searchCollection as $item)
        {
            echo "Redirect: " . $item->getData('redirect') . "<br />";
            echo "Query: " . $item->getData('query_text') . "<br />";
            echo "Popularity: " . $item->getData('popularity') . "<br /><br />";
        }
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean