我正在编写一个控制器,旨在将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
Run Code Online (Sandbox Code Playgroud)
从这个SO问题修改的方法似乎也不起作用:
$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
}
Run Code Online (Sandbox Code Playgroud)
成功调用Magento的目录搜索模块并获取结果集合后,我缺少哪些步骤?
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')
);
Run Code Online (Sandbox Code Playgroud)
$ 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9225 次 |
| 最近记录: |