gpt4 book ai didi

Magento - 如何将分层导航添加到高级搜索?

转载 作者:行者123 更新时间:2023-12-02 09:13:22 24 4
gpt4 key购买 nike

如何将分层导航添加到高级搜索结果页面?

Magento 版本 1.7。

最佳答案

下面的补丁将在高级搜索结果中显示分层导航,并且可以与分层导航配合良好。分层导航和搜索结果基于两个单独的产品集合显示,一个由catalogsearch/Model/Layer.php创建,另一个由catalogsearch/Model/Advanced.php创建。因此,我们需要重写这两个模型的一些功能,以使分层导航在高级搜索中工作。

1- 在 local.xml 中的 catalogsearch_advanced_result 标记下添加。

 <reference name="left">
<block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>

覆盖catalogsearch/model/Layer.php的prepareProductCollection函数
public function prepareProductCollection($collection){

if(Mage::helper('catalogsearch')->getQuery()->getQueryText())//for normal search we get the value from query string q=searchtext
return parent::prepareProductCollection($collection);
else{

$collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());
/**
* make sure you cross check the $_REQUEST with $attributes
*/
$attributes = Mage::getSingleton('catalog/product')->getAttributes();

Mage::log(print_r($_REQUEST,1));
foreach($attributes as $attribute){
$attribute_code = $attribute->getAttributeCode();
//Mage::log("--->>". $attribute_code);
if($attribute_code == "price")//since i am not using price attribute
continue;

if (empty($_REQUEST[$attribute_code])){
//Mage::log("nothing found--> $attribute_code");
continue;
}
if(!empty($_REQUEST[$attribute_code]) && is_array($_REQUEST[$attribute_code]))
$collection->addAttributeToFilter($attribute_code, array('in' => $_REQUEST[$attribute_code]));
else
if(!empty($_REQUEST[$attribute_code]))
$collection->addAttributeToFilter($attribute_code, array('like' => "%" . $_REQUEST[$attribute_code] . "%"));
}

$collection->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite();

//Mage::log($collection->getSelect()->__toString());

Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection($collection);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
}

return $this;
}

覆盖catalogsearch/model/Advanced.php的getProductCollection、getSearchCriterias函数
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);

if(isset($_GET['cat']) && is_numeric($_GET['cat']))
$this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['cat']),true);
}
return $this->_productCollection;
}

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

关于Magento - 如何将分层导航添加到高级搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10503447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com