Magento: 如何对产品进行分目录的产品搜索

Magento: 如何对产品进行分目录的产品搜索


In "advanced search" feature, searching product by category is not the default setting.  But we can do this by modifying the foloowing 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 bottom of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php we can add following codes:

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

 

In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() by following functions:

 

 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 getProductCollection() by following function:

 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 page app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, find the codes as following:

 

    echo $_code ?>" id=" echo $_code ?>" value=" echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title=" echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text  echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />
             endswitch; ?>
        
         endforeach; ?>

 

at the bottom of this piece of codes, add following codes:

 

         
  • 你可能感兴趣的:(magento)