是否有任何方式或黑客或扩展或任何可能的解决方法使类别名称可搜索?当任何人搜索类别名称时,我只想要一些结果。
答案 0 :(得分:0)
您可以尝试:
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
在app/code/local/Mynamespace/OverrideCatalogSearch/Block/Advanced/Form.php
(在大括号之前),添加:
public function getStoreCategories()
{
$helper = Mage::helper('catalog/category');
return $helper->getStoreCategories();
}
在app/code/core/Mynamespace/OverrideCatalogSearch/Model/Advanced.php
中,使用以下代码覆盖(替换)getSearchCriterias()
函数:
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;
}
使用:
覆盖(替换)getProductCollection()
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;
}
在app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml
中,在此代码之后:
<?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; ?>
添加:
<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>
现在,如果您只想按类别搜索,那么您将面临一条错误消息,例如“您必须指定至少一个搜索字词”。要解决此问题,您需要修改以下文件:
app/code/core/Mynamespace/OverrideCatalogSearch/Model/Advanced.php
打开文件并搜索此功能addFilters
。在此功能中,您可以看到以下代码,替换此
if ($allConditions) {
$this->getProductCollection()->addFieldsToFilter($allConditions);
} else if (!count($filteredAttributes)) {
Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
}
使用以下代码:
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'));
}
现在,您不会仅通过类别或任何单个属性搜索在高级搜索中遇到任何问题。