【发布时间】:2015-06-28 12:46:36
【问题描述】:
是否有任何方法或黑客或扩展或任何可能的解决方法来使类别名称可搜索?当有人使用类别名称进行搜索时,我只是想要一些结果。
【问题讨论】:
标签: php magento e-commerce
是否有任何方法或黑客或扩展或任何可能的解决方法来使类别名称可搜索?当有人使用类别名称进行搜索时,我只是想要一些结果。
【问题讨论】:
标签: php magento e-commerce
你可以试试:
app/code/core/Mage/CatalogSearch/Block/Advanced/Form.phpapp/code/core/Mage/CatalogSearch/Model/Advanced.phpapp/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'));
}
现在您不会在仅按类别或任何单一属性搜索的高级搜索中遇到任何问题。
【讨论】:
getEscapedQueryText() => $searchTerm 并加载 $categories。 // 2*/ 检查 category_name 是否包含 $searchTerm