Search by category name

后端 未结 3 585
春和景丽
春和景丽 2021-01-14 05:19

When I try to do search by Category Name it reurns nothing. For Eg, I have Organic, Unique, Sprots etc.as categories and in search I type Unique. But I get no results.

相关标签:
3条回答
  • 2021-01-14 05:25

    You can search categories using LIKE filter as below

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

    Results Output

    foreach ($categories as $cat) {
      echo '<div><a href="'.$cat->getUrl().'">' . $cat->getName() . '</a></div>';
    }
    
    0 讨论(0)
  • 2021-01-14 05:36

    You may be looking for the addAttributeToFilter method. e.g.

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

    You can then work with the collection returned, e.g.

    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.

    0 讨论(0)
  • 2021-01-14 05:42

    Unfortunately, Magento's default search function is a product search and is limited to that scope. When you search "Unique" it's looking in the products name and perhaps the description depending on your configuration.

    A quick solution would be to display a listing of matching categories along with the 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/

    0 讨论(0)
提交回复
热议问题