按类别名称搜索

发布于 2024-09-16 07:59:23 字数 71 浏览 3 评论 0原文

当我尝试按类别名称进行搜索时,它什么也没有返回。例如,我有有机、独特、体育等作为类别,在搜索中我输入独特。但我没有得到任何结果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眸 2024-09-23 07:59:23

不幸的是,Magento 的默认搜索功能是产品搜索,并且仅限于该范围。当您搜索“Unique”时,它会查找产品名称,可能还会查找说明,具体取决于您的配置。

一个快速的解决方案是显示匹配类别的列表以及产品结果。

<?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;?>

来源:http://www.magentocommerce.com/boards/viewthread/74632/

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/

妥活 2024-09-23 07:59:23

您可以使用 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>';
}

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>';
}
仄言 2024-09-23 07:59:23

您可能正在寻找 addAttributeToFilter 方法。例如,

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

您可以使用返回的集合,例如,

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

这至少适用于 Magento CE 1.7.0.1。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文