从Magento中的自定义块调用CatalogSearch的正确方法是什么?

时间:2014-01-07 18:18:39

标签: magento search

我正在编写一个控制器,旨在将Magento的目录搜索功能暴露给API调用。使用具有默认REST api的过滤器不会返回与Magento站点上可用的搜索功能相同的质量结果。

我现在已经挖掘了几天了,我在Stack Overflow和Magento讨论板上看到的各种方法似乎都没有用,我是否缺少一步?

这两种方法都返回null,我似乎无法找出原因:

 $query = Mage::helper('catalogSearch')->getQuery();

 $searcher = Mage::getSingleton('catalogsearch/advanced')->addFilters(
             array('name'=> $query->getQueryText(), 
             'description' => $query->getQueryText()));



 $obj = new stdClass();
 $obj->query = $query->getQueryText();
 $obj->results = $searcher->getProductCollection(); //nothing returned

this SO question修改的方法似乎也不起作用:

 $query = Mage::helper('catalogSearch')->getQuery();

 $obj = new stdClass();
 $obj->query = $query->getQueryText();

 if ($query->getQueryText()) {
      if (Mage::helper('catalogSearch')->isMinQueryLength()) {
           $query->setId(0)->setIsActive(1)->setIsProcessed(1);
      } else { 
           if ($query->getId()) { 
                $query->setPopularity($query->getPopularity()+1);
           } else { 
                $query->setPopularity(1);   
           }
           $query->prepare();
       }
       $obj->results = $query->getProductCollection(); //null
}

成功调用Magento的目录搜索模块并获得一系列结果后我缺少哪些步骤?

2 个答案:

答案 0 :(得分:9)

Magento的搜索模型有点复杂,因为它们构建了一种机制来保存缓存和统计信息的查询和结果。因此,您需要准备查询对象,然后准备结果,然后您可以将产品集合与搜索结果表一起加入。以下是如何在代码中执行此操作:

$searchText = 'ipad';
$query = Mage::getModel('catalogsearch/query')->setQueryText($searchText)->prepare();
$fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
        Mage::getModel('catalogsearch/fulltext'), 
        $searchText, 
        $query
        );

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getSelect()->joinInner(
            array('search_result' => $collection->getTable('catalogsearch/result')),
            $collection->getConnection()->quoteInto(
                'search_result.product_id=e.entity_id AND search_result.query_id=?',
                $query->getId()
            ),
            array('relevance' => 'relevance')
        );

$ collection是一个经过筛选的目录产品集合,您可以根据需要执行任何其他操作:

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

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

答案 1 :(得分:1)

你可以做这样的事情

$searchQuery = "your search query";
$storeId = "your store id";
$collection = Mage::getResourceModel("catalog/product_collection")->addAttributeToSelect("*");
$query = Mage::helper("catalogSearch")->getQuery();
$query->setStoreId($storeId);
$query->setQueryText($searchQuery);
$collection = $query->getSearchCollection();
$collection->addSearchFilter($searchQuery);
$collection->addAttributeToSelect("*");
$collection->addAttributeToFilter("status", 1);
foreach($collection as $_product) {
    //do your iteration
}

或在Ron的回答中,只是在执行代码之前将存储设置在环境中,并在代码之后取消设置。

$appEmulation = Mage::getSingleton("core/app_emulation");
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

//Ron's Code

$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);