Search by name

When I try to search by category name, it returns nothing. For example, I have Organic, Unique, Sprots, etc., And in the search I type Unique. But I do not get results.

+3
source share
3 answers

Unfortunately, the default search feature of Magento is product search and is limited to this area. When searching for β€œUnique,” ​​he searches for the product name and possibly a description depending on your configuration.

A quick fix would be to display a list of relevant categories along with product results.

<?php
    $searchTerm = $this->helper('catalogSearch')->getEscapedQueryText();
    $categories = $this->helper('catalog/category')->getStoreCategories(false, true);
    $count = 0;
    foreach ($categories as $count_category) {
        if ($this->helper('catalog/category')->canShow($count_category) && stripos($count_category->getName(), $searchTerm) !== false) 
               $count++;
    }

    if ($count > 0):

    echo "<div class=\"search-term-notice\">";
    echo "The following product categories matched your search:";

    foreach ($categories as $category) {
        if ($this->helper('catalog/category')->canShow($category) && stripos($category->getName(), $searchTerm) !== false) 
            echo "<h3> > <a href='".$category->getUrl()."'>".$category->getName()."</a></h3></p>";
    }
    echo "</div>";
    endif;?>

Source: http://www.magentocommerce.com/boards/viewthread/74632/

+6
source

, LIKE,

 $categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('url')
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('name',array(array('like' => '%'. $searchvariable.'%')));

foreach ($categories as $cat) {
  echo '<div><a href="'.$cat->getUrl().'">' . $cat->getName() . '</a></div>';
}
0

You may be looking for the addAttributeToFilter method. eg.

$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToFilter('name',$name);

Then you can work with the returned collection, for example

foreach ($categories as $cat) {
  echo 'Name: ' . $cat->getName() . "<br />";
  echo 'Category ID: ' . $cat->getId() . "<br />";
}

This works in Magento CE 1.7.0.1, at least.

-1
source

Source: https://habr.com/ru/post/1761813/


All Articles