php 按类别搜索

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 按类别搜索相关的知识,希望对你有一定的参考价值。

How to add search by category to advanced search
Last modified by Rasel on Fri, June 25, 2010 10:30
Source|Old Revisions  

On the advanced search page, “search by category” is not an option by default, but can be very helpful. To add this, you will need to edit the following files:

app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php
app/code/core/Mage/CatalogSearch/Model/Advanced.php
app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml

At the very end of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php (before the closing brace), add:

public function getStoreCategories()
{
  $helper = Mage::helper('catalog/category');
  return $helper->getStoreCategories();
}

In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() function (line 157) with the code below:

public function getSearchCriterias()
{
  $search = $this->_searchCriterias;
  /* display category filtering criteria */
  if(isset($_GET['category']) && is_numeric($_GET['category'])) {
      $category = Mage::getModel('catalog/category')->load($_GET['category']);
      $search[] = array('name'=>'Category','value'=>$category->getName());
  }
  return $search;
}

replace the next function, getProductCollection(), with:

public function getProductCollection(){
if (is_null($this->_productCollection)) {
    $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addStoreFilter();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
    /* include category filtering */
    if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
}

return $this->_productCollection;
}

In app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, after this code:

<?php foreach ($this->getSearchableAttributes() as $_attribute): ?>
<?php $_code = $_attribute->getAttributeCode() ?>
<li>
  <label for="<?php echo $_code ?>"><?php echo $this->getAttributeLabel($_attribute) ?></label>
  <?php switch($this->getAttributeInputType($_attribute)):
      case 'number': ?>
      <div class="range field-row">
          <input name="<?php echo $_code ?>[from]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'from')) ?>" id="<?php echo $_code ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text" />
          <input name="<?php echo $_code ?>[to]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'to')) ?>" id="<?php echo $_code ?>_to" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text"/>
      </div>
      <?php break;
      case 'select': ?>
          <?php echo $this->getAttributeSelectElement($_attribute) ?>
      <?php break;
      case 'yesno': ?>
          <?php echo $this->getAttributeYesNoElement($_attribute) ?>
      <?php break;
      case 'date': ?>
          <?php echo $this->getDateInput($_attribute, 'from') ?>
          -
          <?php echo $this->getDateInput($_attribute, 'to') ?>
      <?php break;
      default: ?>
      <input name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />
  <?php endswitch; ?>
</li>
<?php endforeach; ?>

add:

<li>
  <label for="category_search_field">Search by Category:</label>
  <select name="category" id="category_search_field">
      <option value="">-- Any Category --</option>
      <?php foreach ($this->getStoreCategories() as $_category): ?>
      <?php if($_category->hasChildren()): ?>
      <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
      <?php foreach ($_category->getChildren() as $subcategory):
      if($subcategory->getIsActive()) : ?>
          <option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
      <?php endif; endforeach; ?>
      <?php elseif($_category->getIsActive()): ?>
      <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
      <?php endif; ?>
      <?php endforeach ?>

  </select>
</li>

Now, If you are going to search only by Category then you will face an error message like “You have to specify at least one search term”. To solve it you need to modify following file:

    app/code/core/Mage/CatalogSearch/Model/Advanced.php

Open the file and search this function addFilters. In this function you can see following codes, replace this

if ($allConditions) {
  $this->getProductCollection()->addFieldsToFilter($allConditions);
} else if (!count($filteredAttributes)) {
  Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
}

with following codes:

if (($allConditions) || (isset($values['category']) && is_numeric($values['category']))) {
  $this->getProductCollection()->addFieldsToFilter($allConditions);
} else if (!count($filteredAttributes)) {
  Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
}

Now you won’t face any problem in advanced search only by Category or any single attribute search.

以上是关于php 按类别搜索的主要内容,如果未能解决你的问题,请参考以下文章

php 按类别搜索

php 按类别筛选搜索 - woocommerce产品

php WooCommerce搜索产品按类别下拉列表

php 按类别搜索类别搜索поискпокатегориям

Firebase 按类别名称搜索

使用 PHP 按类别过滤帖子